【问题标题】:Wordcloud doesn't update with new input in ShinyWordcloud 不会使用 Shiny 中的新输入进行更新
【发布时间】:2015-06-26 06:57:58
【问题描述】:

作为闪亮应用程序 (testapp) 的一部分,我具有以下功能。它为默认选择生成词云,但不会随新选择更新。

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)
  )
))

服务器.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)])
  }

数据 df1 位于 testapp 内的数据文件夹中。数据结构如下:

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")

附:需要使用 data.table 因为我正在聚合大量组。它需要重新设置为 wordcloud 的数据框。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    javascript 文件 d3Cloud.js 中存在错误。我已经在 rWordCloud (https://github.com/NikNakk/rWordCloud) 的一个分支上修复了它,并向 adymimos 提交了一个拉取请求。实际的错误在 htmlwidgets/d3Cloud.js 的第 59 行

     if ( instance.lastValue !== undefined) {
       svg.remove();
       console.log('Clearing svg');
       var svg = d3.select(el).append("svg")
       .attr("class", "rWordCloud");
       instance.svg = svg;  
    }
    

    应该是

     if ( instance.lastValue !== undefined) {
       instance.svg.remove();
       console.log('Clearing svg');
       var svg = d3.select(el).append("svg")
       .attr("class", "rWordCloud");
       instance.svg = svg;  
    }
    

    否则,您需要删除isolate,您可以将server.R代码简化为:

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

    【讨论】:

    • 我从你的 github 安装了这个包,它工作正常。在包维护者修复错误之前,我将使用它。非常感谢。
    • @user227710 现已合并到主分支中。
    • 感谢@Nick K 的更新,再次感谢您的帮助。
    【解决方案2】:

    我认为你的问题在这里:

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

    isolate 函数将确保 dataInput 不会对 isolatem 中的任何内容产生反应性依赖,在您的情况下是 dataInput 中的所有内容。如果您只是删除isolate,那么它应该会按预期更新,我相信(我没有测试过)。

    dataInput() 是否也被用作未发布的应用程序的一部分?如果没有,您可以使用以下方法缩短内容:

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

    我没有使用过renderd3Cloud - 您可能需要使用data&lt;-reactive({readydata(input$firm)}),然后将其称为data()

    【讨论】:

    • 谢谢。但是您提供的任何建议都没有帮助解决我的问题。如果您能测试代码(它是可重现的),我将不胜感激。
    • 我也无法工作。如果为dataInput()$indivdataInput()$Freq 添加文本输出,它们显然正在更新,但图形没有。我想知道这是否是一个 rWordClud 错误。
    • 再次感谢。我确实尝试过使用 base r plot 并且它可以工作,可能是你所说的一个错误。
    猜你喜欢
    • 2017-09-03
    • 2020-02-01
    • 2013-06-22
    • 2021-07-23
    • 2017-04-10
    • 1970-01-01
    • 2016-08-14
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多