gpt4 book ai didi

r - Wordcloud 不会随着 Shiny 中的新输入进行更新

转载 作者:行者123 更新时间:2023-12-02 03:06:35 24 4
gpt4 key购买 nike

我有以下功能作为 Shiny 应用程序(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)

})

})

助手.R

readydata<-function(company){
data.frame(setDT(df1)[firm==company,list(Freq=.N),by=indiv][order(Freq,decreasing=TRUE)])
}

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

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 的数据框。

最佳答案

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

关于r - Wordcloud 不会随着 Shiny 中的新输入进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com