Skip to content

Instantly share code, notes, and snippets.

@kxzk
Created October 18, 2017 17:35
Show Gist options
  • Save kxzk/99a8282965f0c528708e75bdf84fd1b6 to your computer and use it in GitHub Desktop.
Save kxzk/99a8282965f0c528708e75bdf84fd1b6 to your computer and use it in GitHub Desktop.
server <- function(input, output) {
# defining a reactive function
# reacts to changes in the ngram slider
# by taking it out of the output$wordcloud function
# we have more control over when our dplyr statement
# will get updated
ngrams <- reactive({
# the $<name> is used to reference the name
# we gave our ngram sliderInput earlier
input$ngramCount
})
# the $<name> here is referencing the name
# we defined in the plotOutput('wordcloud')
# earlier in the mainPanel
output$wordcloud <- renderPlot({
# our text is coming from the janeaustenr package
austen_books() %>%
# the %>% is a pipe operator
# will take left side and pass it through
# same as linux piping
# selecting our column
select(text) %>%
# turning text into an ngram with count selected by user
unnest_tokens(ngram, text, token="ngrams", n=ngrams()) %>%
# getting occurence count of each ngram
count(ngram)
# setting up our wordcloud to use ngrams, sorted by count (n)
# user can adjust max.words shown with slider
with(wordcloud(ngram, n, max.words=input$cloudCount, rot.per=.2,
colors=c("#b4b3b3", "#969696", "#484848", "#8dc7d3")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment