I have following functions as part of the shiny app (testapp). It generates the word cloud for default selection, but doesn't update with the new selection.
ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
headerPanel("Word Cloud"),
# Sidebar with selection inputs
sidebarPanel(width = 6,
selectInput("firm", label="Choose a firm:",
choices = c("a","b"),
selected = "a" )
),
# Show Word Cloud
mainPanel(
d3CloudOutput("Plot1", width = "100%", height = 500)
)
))
server.R
library(shiny)
library(rWordCloud) #require(devtools);install_github('adymimos/rWordCloud')
library(data.table)
source("helpers.R")
df1<-readRDS("data/df1.rds")
shinyServer(function(input, output) {
dataInput <- reactive({
isolate({
readydata(input$firm)
})
})
output$Plot1 <- renderd3Cloud({
data <- dataInput()
d3Cloud(text = data$indiv,size=data$Freq)
})
})
helpers.R
readydata<-function(company){
data.frame(setDT(df1)[firm==company,list(Freq=.N),by=indiv][order(Freq,decreasing=TRUE)])
}
The data df1 is inside the data folder which is inside the testapp. The structure of data is as follows:
df1<-structure(list(firm = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a",
"b"), class = "factor"), indiv = structure(c(5L, 6L, 7L, 1L,
4L, 5L, 6L, 7L, 1L, 4L, 3L, 2L, 3L, 2L, 3L, 2L, 8L, 8L, 8L, 8L
), .Label = c("bello", "billow", "dillow", "fellow", "hello",
"kello", "nello", "tillow"), class = "factor")), .Names = c("firm",
"indiv"), row.names = c(NA, -20L), class = "data.frame")
P.S. The use of data.table is needed because I am aggregating large number of groups. It need to be set back to dataframe for wordcloud.