Wordcloud won't update new Shiny login
I have the following functions as part of a brilliant application (testapp). It generates word cloud for the default selection, but does not update 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 df1 data is inside the data folder, which is inside testapp. The data structure looks like this:
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")
PS Using data.table is necessary because I am aggregating a large number of groups. It should be returned to the dataframe for wordcloud.
source to share
There was an error in the javascript d3Cloud.js file. I fixed it on the rWordCloud fork ( https://github.com/NikNakk/rWordCloud ) and sent a pull request to adymimos. The actual error was on line 59 of htmlwidgets / d3Cloud.js
if ( instance.lastValue !== undefined) {
svg.remove();
console.log('Clearing svg');
var svg = d3.select(el).append("svg")
.attr("class", "rWordCloud");
instance.svg = svg;
}
should be
if ( instance.lastValue !== undefined) {
instance.svg.remove();
console.log('Clearing svg');
var svg = d3.select(el).append("svg")
.attr("class", "rWordCloud");
instance.svg = svg;
}
Otherwise, you need to remove isolate
and you can simplify the server.R code to:
shinyServer(function(input, output) { output$Plot1 <- renderd3Cloud({ data <- readydata(input$firm) d3Cloud(text = data$indiv,size=data$Freq) }) })
source to share
I think your problem is here:
dataInput <- reactive({
isolate({
readydata(input$firm)
})
})
The function isolate
makes sure it dataInput
doesn't respond to anything within isolate
m, which in your case is all in dataInput
. If you just uninstall isolate
then it should update as expected I believe (I haven't tested it).
Is it dataInput()
being used as part of an application that hasn't been published? If not, you can shorten the time:
output$Plot1 <- renderd3Cloud({
data <- readydata(input$firm)
d3Cloud(text = data$indiv,size=data$Freq)
})
I haven't worked with renderd3Cloud
- you may need to use data<-reactive({readydata(input$firm)})
and then reference it as data()
.
source to share