textAreaInput 闪亮错误 - "text must be character"

Shiny error with textAreaInput - "text must be character"

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

之外工作

我使用了 shiny 网站上的操作按钮代码

当我 运行 下面的代码时,我得到这个错误 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)

您需要将反应函数调用为 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)