3

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.

user227710
  • 3,164
  • 18
  • 35

2 Answers2

2

There was a bug in the javascript file d3Cloud.js. I've fixed it at a fork of rWordCloud (https://github.com/NikNakk/rWordCloud) and submitted a pull request to adymimos. The actual bug 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 have been

 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 your server.R code to:

shinyServer(function(input, output) {
  output$Plot1 <- renderd3Cloud({
    data <- readydata(input$firm)
    d3Cloud(text = data$indiv,size=data$Freq)
  })
})
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
1

I think your problem is here:

dataInput <- reactive({
    isolate({
        readydata(input$firm)
    })
})

The isolate function will ensure that dataInput does not take a reactive dependency on anything within isolatem which in your case is everything in dataInput. If you simply remove the isolate then it should update as expected, I believe (I did not test it).

Does dataInput() also get used as part of the app that was not posted? If not you could shorten things with:

output$Plot1 <- renderd3Cloud({
      data <- readydata(input$firm)
      d3Cloud(text = data$indiv,size=data$Freq)
})

I have not worked with renderd3Cloud - you might need to use data<-reactive({readydata(input$firm)}), and then refer to it as data().

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • Thanks. But none of suggestions you provided is helping for my problem. I would appreciate if you could test the code (it is reproducible). – user227710 Jun 23 '15 at 02:54
  • 1
    I can't get t to work either. If you add a text output for `dataInput()$indiv` or `dataInput()$Freq`, they are clearly updating, but the graphic does not. I wonder if it is a rWordClud bug. – John Paul Jun 23 '15 at 12:50
  • Thanks again. I did try with base r plot and it works, Probably, a bug as you said. – user227710 Jun 24 '15 at 01:04