rstudio - The output Wordcloud does not update in Shiny -


i trying build shiny app reactive word cloud. of right now, generating static word cloud not change when select different input.

these packages using:

library(shiny) library(tm) library(wordcloud) library(snowballc) library(memoise) 

ui.r

ui <- fluidpage(  # application title titlepanel("word cloud"), sidebarlayout(  # sidebar slider , selection inputs sidebarpanel( selectinput("selection", "choose agency:", choices = agencies), actionbutton("update", "change"), hr(), sliderinput("freq", "minimum frequency:", min = 1,  max = 50, value = 15), sliderinput("max", "maximum number of words:", min = 1,  max = 300,  value = 100)),  # show word cloud mainpanel( plotoutput("plot")))) 

server.r

server <- function(input, output) {   # define reactive expression document term matrix   terms <- reactive({     input$update     # ...but not else      agencies <<- list("nasa" = "nasa", "dod" = "dod")      gettermmatrix <- function(agency) {       if(!(agency %in% agencies))         stop("unknown agency")        propcorpus <- corpus(vectorsource(x$proposal.title))       propcorpus <- tm_map(propcorpus, plaintextdocument)       mycorpus = corpus(vectorsource(propcorpus))       mycorpus = tm_map(mycorpus, content_transformer(tolower))       mycorpus = tm_map(mycorpus, removepunctuation)       mycorpus = tm_map(mycorpus, removenumbers)       mydtm = termdocumentmatrix(mycorpus, control = list(minwordlength = 1))       m = as.matrix(mydtm)       sort(rowsums(m), decreasing = true)     }         gettermmatrix(input$selection)   })    # make wordcloud drawing predictable during session   wordcloud_rep <- repeatable(wordcloud)    output$plot <- renderplot({     v <- terms()     wordcloud_rep(names(v), v, scale=c(4,0.5),                   min.freq = input$freq, max.words=input$max,                   colors=brewer.pal(8, "dark2"))   }) } 

my data 2 columns 1 agency names , 1 description of different contracts.

thank help! figured out, wanted share final code.

first load data , packages:

 contract_data_df <- read.csv(file.choose(), header = true, stringsasfactors = false) contract_data_df$agency <- as.factor(contract_data_df$agency) attach(contract_data_df) library(shiny) library(tm) library(wordcloud) library(snowballc) library(memoise) 

in dataset have 2 columns: agency (factors) , proposal.title (string). purpose of word cloud visualize words prominent in proposal titles associated multiple federal agencies.

setting user interface (ui):

 ui <- fluidpage(   titlepanel("word cloud"),   sidebarlayout(     sidebarpanel(       #selectinput("selection", "choose agency:", choices = list("dod"="dod", "nasa"="nasa")),       selectinput("selection", "choose agency:", choices = agency, selected = 1),       actionbutton("update", "change"),       hr(),       sliderinput("freq",                   "minimum frequency:",                   min = 1,  max = 50, value = 15),       sliderinput("max", "maximum number of words:", min = 1,  max = 300,  value = 100)),      mainpanel(       plotoutput("plot")))) 

set server:

server <- function(input, output) {  terms <- reactive({ input$update agencies <<- list("dod"="dod", "nasa"="nasa") getcorpusmatrix <- function(agency){ text <- subset(contract_data_df, contract_data_df$agency == input$selection) contract_corpus <- corpus(vectorsource(text$proposal.title)) contract_corpus <- tm_map(contract_corpus, content_transformer(tolower)) contract_corpus <- tm_map(contract_corpus, removepunctuation) contract_corpus <- tm_map(contract_corpus, removewords, stopwords("english")) contract_corpus <- tm_map(contract_corpus, stripwhitespace) contract_corpus <- tm_map(contract_corpus, stemdocument)   contract_dtm <- termdocumentmatrix(contract_corpus) contract_dtm_df <- data.frame(as.matrix(contract_dtm)) sort(rowsums(contract_dtm_df), decreasing = true) }  getcorpusmatrix(input$update) })  wordcloud_rep <- repeatable(wordcloud) output$plot <- renderplot({ v <- terms() wordcloud_rep(names(v), v, scale=c(4,0.5), min.freq = input$freq, max.words=input$max, colors=brewer.pal(8, "dark2")) }) } 

finally, launch app:

shinyapp(ui = ui, server = server) 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -