首页 > 解决方案 > 在 R 编程 Shiny App 中,inherits(x, "character") 不是 TRUE

问题描述

我正在创建 Shiny App,目的是输入文本文件并使用 udpipe 库需要创建 wordcloud、annoate 等...

运行应用程序时,我收到“inherits(x, "character") is not TRUE"。问题来自“注释”选项卡,因为我试图从 Server.R 文件返回数据表

ui.R 代码:

shinyUI(
   fluidPage(

    ##today's date
     dateInput("date6", "Date:",
               startview = "decade"),
     ##current time
     h2(textOutput("CurrentTime")),
  # Application title
  titlePanel("UDPipe Text Analysis"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      # Input: for uploading a file ----
      fileInput("file1", "Choose Text File"),
      # Horizontal line ----
      tags$hr(),

      ##checkbox input
      checkboxGroupInput("upos",label = h4("Parts Of Speech to Show:"),
                         c("Adjective" = "ADJ",
                           "Propernoun" = "PROPN",
                           "Adverb" = "ADV",
                           "Noun" = "NOUN",
                           "Verb"= "VERB"),
      selected = c("ADJ","NOUN","VERB"),
      width = '100%'
      #choiceNames =
       # list(icon("Adjective"), icon("Adverb")),
      #choiceValues =
        #list("ADJ", "ADV")
      )),
mainPanel(
  tabsetPanel(type = "tabs",
              tabPanel("Overview",h4(p("Who deveoped this App")),
                       p("This app supports only text files. ", align = "justify"),
                       h4("Pupropse of this app"),
                       h4("what precaution to take")
              ),
              tabPanel("Annotate",dataTableOutput('Annotate'))

              )


)

服务器.R代码

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  Dataset <- reactive({


    if (is.null(input$file)) { return(NULL) } else
    {

      Data <- readlines(input$file1)
      Data  =  str_replace_all(Data, "<.*?>", "")
      return(Data)

    }

  })  


  output$Annotate <- renderDataTable(
    {
      english_model = udpipe_load_model("./english-ud-2.0-170801.udpipe")
      x <- udpipe_annotate(english_model, x = Dataset)
      return(x)
    }
  )


  })

我正在尝试在 output$Annotate 变量中返回数据表。但它不能正常工作。

标签: rshinyudpipe

解决方案


将您udpipe_annotate的代码行替换为以下内容。

txt <- as.character(Dataset())
udpipe_annotate(ud_dutch, x = txt, doc_id = seq_along(txt))

如果您的闪亮应用程序中尚未加载文本数据,这将避免您传递NULLudpipe_annotate


推荐阅读