-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
64 lines (51 loc) · 1.45 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
library(shiny)
library(rvest)
library(dplyr)
library(tm)
library(text2vec)
library(tagcloud)
shinyServer(function(input, output) {
# text import
web_results <- eventReactive(input$button_scrape,{
page <- read_html(input$input_url)
css <- input$input_css
if (css!='') {
text <- page %>%
html_nodes(css) %>%
html_text()
} else {
text <- page %>%
html_text()
}
return(text)
})
output$scrape_results <- renderText({
text <- web_results()
return(
paste0(length(text),' element(s) downloaded')
)
})
# text analysis
text_results <- reactive({
text <- web_results()
prep_fun = tolower
tok_fun = word_tokenizer
it = itoken(text,preprocessor=prep_fun,tokenizer = tok_fun)
sw <- stopwords()
vocab = create_vocabulary(it, ngram = c(input$input_min_ngram, input$input_ngram),stopwords = sw,sep_ngram = ' ')
pruned_vocab = prune_vocabulary(vocabulary = vocab, term_count_min = input$input_minwords)
vectorizer = vocab_vectorizer(pruned_vocab)
dtm = create_dtm(it, vectorizer)
return(dtm)
})
output$analysis_results <- renderText({
dtm <- text_results()
freq <- sort(apply(dtm,2,sum))
return(paste0(length(freq),' ngram(s) created'))
})
output$wordcloud <- renderPlot({
dtm <- text_results()
freq <- sort(apply(dtm,2,sum))
tagcloud(tags = names(freq),weights = freq,fvert = 0.3)
})
})