2
votes

I am building a shiny web app that takes input values, such as name and gender, from users. I would like to turn those values into a dataframe, but I am getting an "Error: cannot coerce class "c("shiny.render.function", "function")" to a data.frame" error.

Here is what I have in the server.R file:

    nText<- eventReactive(input$click_counter, valueExpr = {
    a<- renderText(input$name)
    b<- renderText(input$gender)
    c<- renderText(input$college)
    d<- renderText(input$team)
    e<- renderText(input$score)

    return(data.frame(player=a,college=b,gender=c,team=d,score=e))
  })

  output$nText<- renderDataTable({
    nText()
  })

And this is what I have in the ui.R file:

actionButton("click_counter","Submit"),
dataTableOutput("nText")
1
Exactly what are you thinking is the structure of a,b,c,d or e? Do you realize that you cannot have dataframe columns with language elements? Perhaps you should be using a list? - IRTFM
Just don't wrap all the inputs with renderText() - Yang

1 Answers

1
votes

As @Yang said you need to remove 'renderText' from around your inputs. If you want to store the inputs on each click of 'click_counter' you can use 'reactiveValues'. I've put a worked example of both options below depending on what you want to do with your data after input:

ui <- fluidPage(title = "",

                sidebarLayout(

                  sidebarPanel(width = 6,
                               textInput("name", "Name"),
                               textInput("gender", "Gender"),
                               actionButton("click_counter","Submit")
                  ),
                  mainPanel(
                    dataTableOutput("nText"),
                    dataTableOutput("nText2")

                ))

)

server <- function(input, output, session) {

  values <- reactiveValues()
  values$df <- data.frame()

  mdf <- eventReactive(input$click_counter,{    
    name <- input$name
    gender <- input$gender

    da <- data.frame(name, gender)
    return(da)
  })

  observeEvent(input$click_counter, {
    name <- input$name
    gender <- input$gender

    da <- data.frame(name, gender)

    values$df <- rbind(values$df, da)
  })

  output$nText<- renderDataTable({
    mdf()
  })  

  output$nText2<- renderDataTable({
    values$df
  })
}

shinyApp(ui = ui, server = server)