【问题标题】:Error in [.data.frame: undefined columns selected for worldcloud in Shiny[.data.frame 中的错误:为 Shiny 中的 worldcloud 选择了未定义的列
【发布时间】:2019-09-04 02:31:37
【问题描述】:

我正在创建一个简单的 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))
  })
}

【问题讨论】:

    标签: r shiny wordcloud2


    【解决方案1】:

    您的代码存在多个问题。

    1. wordcloud2 需要 data.frame,包括两列中的单词和频率计数。目前,您提供 data_source() 作为输入,这是一个返回单个 character 字符串的反应结构。
    2. 你需要正确解析textInput服务器端,这意味着你需要从textAreaInput提供的输入中创建一个wordcloud2-合适的data.frame;事实上,使用textAreaInput 可能不是这里使用的最佳元素,因为您的输入文本是高度结构化的,而textAreaInput 最适合用于非结构化文本值,请参阅?textAreaInput。但是,出于教学目的,让我们继续使用您的 textAreaInput
    3. 您还应该包括一项检查,以确保仅在实际使用任何数据时才绘制 wordcloud。我们可以使用validate 来做到这一点,见下面的代码。不包括此检查将导致Warning: Error in [.data.frame: undefined columns selected
    4. 问题较少,但在清晰度方面对您的帖子没有帮助:您根本没有使用input_filecolourInput 同上。

    以下是一个可重现的最小示例(我已经删除了不必要的部分)

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

    这会产生例如

    【讨论】:

    • 非常感谢!我已经相应地增强了代码,现在它适用于文本和文件。但是,当侧边栏中没有输入时,错误“[.data.frame: undefined columns selected”仍然在开始时显示...
    • @crelyz 您的增强代码不会检查空数据,因此会出现错误。看看我上面的例子,特别是validate 部分。
    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多