我正在创建一个简单的Shiny UI,允许用户输入文本或上传文件以创建文字云,侧边栏显示为正常,但主面板继续显示
[。data.frame:选择了未定义的列中的错误”。
避免在textAreaInput中设置默认值的初始警告
关键代码如下:
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add radio buttons input
radioButtons(
inputId = "source",
label = "Word source",
choices = c(
"Use your own words" = "own",
"Upload a file" = "file"
)
),
conditionalPanel(
condition = "input.source == 'own'",
textAreaInput("text", "Enter text",value="Paste here",rows = 7)
),
conditionalPanel(
condition = "input.source == 'file'",
fileInput("file", "Select a txt file (encoding='UTF-8')")
),
colourInput("col", "Background color", value = "white"),
# Add a "draw" button to the app
actionButton(inputId = "draw", label = "Draw!")
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
library(tidyverse)
library(jiebaR)
mixseg = worker()
server <- function(input, output) {
data_source <- reactive({
if (input$source == "own") {
(data <- as.data.frame(table(mixseg <= input$text)))
} else if (input$source == "file") {
f<-read_file(input$file$datapath)
if(is.null(f)){
return(NULL)
}else{
data <- as.data.frame(table(mixseg <=f))
}
}
return(data)
})
output$cloud <- renderWordcloud2({
input$draw
isolate(
wordcloud2(data_source(), backgroundColor =input$col))
})
}
答案 0 :(得分:0)
您的代码存在多个问题。
wordcloud2
要求data.frame
包括两列的单词和频率计数。当前,您正在提供data_source()
作为输入,这是一个反应性结构,它返回单个character
字符串。textInput
服务器端,这意味着您需要根据wordcloud2
提供的输入来创建data.frame
合适的textAreaInput
;实际上,使用textAreaInput
可能不是在此处使用的最佳元素,因为您输入的文本高度结构化,而textAreaInput
最适合用于非结构化文本值,请参见?textAreaInput
。但是,出于教学目的,让我们继续使用textAreaInput
。validate
进行此操作,请参见下面的代码。不包括此检查将导致Warning: Error in [.data.frame: undefined columns selected
。 input_file
; colourInput
的同上。以下是一个可重复的示例(我已删除了不必要的部分)
library(shiny)
library(shinyjs)
library(wordcloud2)
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add radio buttons input
radioButtons(
inputId = "source",
label = "Word source",
choices = c(
"Use your own words" = "own",
"Upload a file" = "file")
),
conditionalPanel(
condition = "input.source == 'own'",
textAreaInput("text", "Enter comma-separated text", rows = 7)
),
conditionalPanel(
condition = "input.source == 'file'",
fileInput("file", "Select a file")
)
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
data_source <- reactive({
if (input$text != "")
as.data.frame(table(unlist(strsplit(input$text, "[, ]"))))
else
NULL
})
output$cloud <- renderWordcloud2({
validate(need(data_source(), "Awaiting data"))
wordcloud2(data_source(), backgroundColor = "white")
})
}
这会产生例如