Shiny error with textAreaInput - "text must be character"

162 views Asked by At

I am trying to write a small shiny app. Part of the app is summarizing text that the user pastes into a box then press an action button. I copied the text example from an online blog and it works outside of shiny

I used the action button code from shiny website

when I run the code below, I get this error Error in sentenceParse(text, docId = docId) : text must be character I guessed this may be because the output from the text box (n or ntext) is not a factor. So I tried to change using as.factor(ntext) but still without success.

I appreciate any guidance.

thank you

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)

1

There are 1 answers

2
YBS On BEST ANSWER

You need to call the reactive function as ntext(). Since it is a reactive it needs to be in a observer.

Update

Width of the output can be controlled with a div(). See the updated code below.

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)