0

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

1 回答 1

0

您的代码存在多个问题。

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

这会产生例如

在此处输入图像描述

于 2019-09-04T05:16:09.563 回答