Shiny app reading input as character

Hello everybody. I'm having trouble with my shiny app and I have been stuck in this for several hours.

My app is supposed to take a data frame and display a table of the data frame, and a plot of some of the information contained in the data frame. The plot is created through a function I designed which receives as input the data frame. Which data frame that is going to be ploted or printed is selected by the user with a selectInput. The select input looks like this:

selectInput(inputId = "bank", label= "Choose the bank to analyse", 
                                 choices = c("Bank One" = "bank1",
                                                         "Bank Two" = "bank2",
                                                         "Bank Three" = "bank3"),
                                  selected = "bank1"),            

I have many banks and for each bank I generate a data frame named bank1, bank2 and so on. The data frames are created after some burdensome calculations I put in the server function. I'm creating a data frame named data as a reactive like this:

  data <- reactive({ input$bank})

And then it is supposed to display the data frame with the following code chunk:

  output$table1 <- DT::renderDataTable({
    DT::datatable(data(), options = list(pageLength=6)))

The code as shown generates the following Error message: 'data' must be 2-dimensional (e.g. data frame or matrix).

The app is actually generating the data frames becouse if, for example, I replace the input$bank with bank1 the data frame is displayed correctly. So, the data frames not existing is not the problem.

The problem, I believe, is that somehow shiny is reading the input as character and not as a data frame, even though the data frame exists. But so far I do not know why this is happening and how to solve it. I'd really appreciate your help.

I do not provide the whole app becouse it is too large.

data <- reactive({ get(req(input$bank))})

Shiny inputs are usually undefined (NULL) when the app starts so you need to check that the input exists before you use it, as suggested by @nirgrahamuk, using req().

1 Like

I think what is happening is that the input$bank is not the data frame you were expecting:

  • Is it filled in by a text field (and therefore contains a string ”bank1” and not the actual data frame)? In this case you can use get() to fetch the binding attached to that name or you can use a switch() statement to map from the string to the data frame.
  • Maybe it is actually a file that was uploaded (although that seems less likely from your description. In this case you’ll need to read it from disk first before plugging it in.

Thanks nirgrahamuk!! It works!!

Thanks for your help tjpalanca. As you mentioned, my app was fixed with the get() command.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.