-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.R
73 lines (66 loc) · 1.97 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
## --------
## server.R
## --------
library("shiny")
library("ggplot2")
library("plotly")
source("R/word_count.R")
server <- function(input, output) {
count <- reactive({
infile <- input$infile
if (is.null(infile)) {
return(NULL)
}
pages <- if (is.null(input$pages)) {
NULL
} else {
# try to parse pages
try_pages <- try(eval(parse(text = paste0("c(", input$pages, ")"))), silent = TRUE)
if (!inherits(try_pages, "try-error")) {
pages <- try_pages
} else {
pages <- NULL
}
}
# count words
word_count(infile$datapath,
pages = pages,
count_numbers = input$count_numbers,
count_captions = input$count_captions,
count_equations = input$count_equations,
split_hyphenated = input$split_hyphenated,
split_urls = input$split_urls,
verbose = FALSE)
})
# total word count
output$grand_total <- renderText({
counts <- count()
if (is.null(counts)) {
return("")
}
paste("Total Word Count:", sum(counts$words, na.rm = TRUE))
})
# text to display when showing per-page word counts
output$page_counts <- renderText({
counts <- count()
if (is.null(counts)) {
return("")
}
paste("Word Count by Page:")
})
# graph of word counts by page
output$barplot <- renderPlotly({
counts <- count()
if (is.null(counts)) {
return(NULL)
}
# plot
ggplot(counts, aes(x = page, y = words)) +
xlab("Page") +
ylab("Words per page") +
scale_x_reverse(breaks = seq_len(max(counts$page, na.rm = TRUE))) +
geom_col(na.rm = TRUE) +
coord_flip() +
theme_minimal()
})
}