2015-06-21 54 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