Error when I subset of data frame within the shiny application "Error: 'data' must be 2-dimensional (e.g. data frame or matrix)"


I am new to using the "shiny app" features within R, and have hit a wall in my attempt to access a portion of a data frame. When I try to display the data using the DT::renderDataTable() feature I get the output "Error: 'data' must be 2-dimensional (e.g. data frame or matrix)"

In the code snippet below I am attempting to create a data frame from a range of rows and columns from a larger data frame based on user input. The code works beautifully when I have hard coded integers into the snippet, and only malfunctions when I have inserted variables into the function call. Despite my numerous attempts to overcome, the problem persists. This snippet is located within the server part of my shiny source code.

Code that doesn't work:
smaller_table <- larger_table[c(input$var_1:input$var_2),c(input$var_3:input$var_4)]

Code that does work:
smaller_table <- larger_table[c(1:7),c(1:2)]

based on the fact that the application works when there are ints hard-coded into the line I am assuming is has something to do with the way input$foo is being processed, but I have tried storing them in other variables to access, transforming them with as.numeric(input$foo) and several other approaches. I need the application to allow the user to specify the start/ end of the rows and columns, so this step is integral.
If I have formatted this question incorrectly please let me know, and I would be happy to edit/ delete/ resubmit accordingly.

Thanks in advance,


When the app starts, the objects input$var_1, input$var_2, input$var_3,input$var_4 etc are NULL, so this will throw an error. To avoid this you can add the following line before you try to build the data table, which requires that these variables have a value. You can also check that the values are not NA if necessary.

req(input$var_1, input$var_2, input$var_3, input$var_4)
req(input$var_1 != "NA", input$var_2 != "NA", input$var_3 != "NA", input$var_4 != "NA")

Thank you so much! As soon as I read your response I realized I was trying to populate the table before initializing the values for each of the variables.

1 Like

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