【问题标题】:Shiny error with textAreaInput - "text must be character"textAreaInput 出现闪亮错误 - “文本必须是字符”
【发布时间】:2021-01-17 17:30:58
【问题描述】:

我正在尝试编写一个闪亮的小应用程序。该应用程序的一部分是汇总用户粘贴到框中然后按下操作按钮的文本。 我从一个在线博客复制了文本示例,它在闪亮之外工作

我使用了来自闪亮网站的操作按钮代码

当我运行下面的代码时,我得到了这个错误Error in sentenceParse(text, docId = docId) : text must be character 我猜这可能是因为文本框(n 或 ntext)的输出不是一个因素。所以我尝试使用as.factor(ntext) 进行更改,但仍然没有成功。

感谢任何指导。

谢谢

library(shiny)
library(lexRankr)

# 
ui <- fluidPage(
    textAreaInput("n", "Paste text and click submit", rows = 5),
    br(),
    actionButton("goButton", "Go!"),
    verbatimTextOutput("nText")
    
)


#
server <- function(input, output) {
    
    ntext <- eventReactive(input$goButton, {
        input$n
    })  
    
    
    
    #perform lexrank for top 3 sentences
    top_3 = lexRankr::lexRank(ntext,
        docId = rep(1, length(ntext)),
        n = 3,
        continuous = TRUE)
    
    #reorder the top 3 sentences to be in order of appearance in article
    order_of_appearance = order(as.integer(gsub("_","",top_3$sentenceId)))
    #extract sentences in order of appearance
    ordered_top_3 = top_3[order_of_appearance, "sentence"]
    
    
    
    output$nText <- renderText({
        ordered_top_3()
    })
}



# 

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    您需要将响应函数调用为ntext()。由于它是响应式的,因此它需要在 observer 中。

    更新

    可以使用div() 控制输出的宽度。请参阅下面的更新代码。

    library(shiny)
    library(lexRankr)
    
    ui <- fluidPage(
      textAreaInput("n", "Paste text and click submit", width='300px', rows=5),
      br(),
      actionButton("goButton", "Go!"),
      div(style="width:500px;padding-left:100px;",fluidRow(verbatimTextOutput("nText", placeholder = TRUE)))
      #verbatimTextOutput("nText")
      
    )
    
    server <- function(input, output) {
      
      ntext <- eventReactive(input$goButton, {
        req(input$n)
        input$n
      })  
      
      observe({
        #perform lexrank for top 3 sentences
        top_3 = lexRankr::lexRank(ntext(),
                                docId = rep(1, length(ntext())),
                                n = 3,
                                continuous = TRUE)
    
        #reorder the top 3 sentences to be in order of appearance in article
        order_of_appearance = order(as.integer(gsub("_","",top_3$sentenceId)))
    
        #extract sentences in order of appearance
        ordered_top_3 = top_3[order_of_appearance, "sentence"]
      
        output$nText <- renderText({
          ordered_top_3
        })
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢。这解决了它!我还有一个问题。如何更改 renderText 框的宽度?现在它填满了整个屏幕宽度,有没有办法让它变窄?
    • 答案很有帮助。感谢您花时间回答我的问题。
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多