2015-06-21 71 views
3

作为闪亮应用(testapp)的一部分,我有以下功能。它为默认选择生成词云,但不会使用新选择进行更新。Wordcloud不会使用Shiny中的新输入进行更新

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

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

p.S. data.table的使用是必要的,因为我汇集了大量的组。它需要重新设置为wordcloud的数据框。

回答

2

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) 
    }) 
}) 
+0

我从你的github安装了软件包,它工作正常。我将使用它,直到软件包维护人员修复错误。非常感谢。 – user227710

+0

@ user227710它现在被合并到主分支中。 –

+0

感谢您的更新@Nick K,并再次为您提供帮助。 – user227710

1

我觉得你的问题是在这里:

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

isolate功能将确保dataInput并不需要依赖反应上的任何东西isolate米而你的情况是dataInput一切之内。如果你只是删除isolate那么它应该按预期更新,我相信(我没有测试它)。

dataInput()是否也被用作未发布的应用程序的一部分?如果没有,你可以缩短事情:

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

我还没有和renderd3Cloud工作 - 你可能需要使用data<-reactive({readydata(input$firm)}),然后称其为data()

+0

谢谢。但是,您提供的任何建议都无助于解决我的问题。如果您可以测试代码(可重现),我将不胜感激。 – user227710

+1

我也无法工作。如果为'dataInput()$ individual'或'dataInput()$ Freq'添加文本输出,则它们显然正在更新,但图形不会。我不知道这是否是一个rWordClud错误。 –

+0

再次感谢。我确实尝试过使用base r plot,它可以工作,可能是你说的一个bug。 – user227710

相关问题