Elisandra Lurdes Kern
Elisandra Lurdes Kern

Reputation: 33

'data' must be 2-dimensional (e.g. data frame or matrix) RShiny

Hy, I am trying to format output using DT. However, it shows an error: 'data' must be 2-dimensional (e.g. data frame or matrix). I appreciate it if anyone can help!

Below is an example created from the data structure (a1).

data: a1

ind lproof lproofo var1 var2 var3 var4  var5 var6 var7 var8 var9 var10  
A001  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A002  D        M   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A003  D        M   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A004  G        M   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A005  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A006  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A007  G        D   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A008  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
A009  D        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B001  D        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B002  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B003  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B004  M        D   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B005  M        D   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B006  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B007  M        G   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B008  M        D   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5
B009  M        D   2.4  0.5  20.3  0.2  2.4  0.5  2.4  0.5  2.4  0.5

When I run the data it results in the following error: image.png

ui <- dashboardPage(
  dashboardHeader(disable = TRUE),
  dashboardSidebar(disable = TRUE),
  dashboardBody( 
    fluidPage(
     
          selectInput("lproofo",
                      label = h3("Evaluación Vieja:"), 
                       choices = c ("All", unique(as.character(a1$lproofo))), selected = "All")
      ),
    
          selectInput("lproof", 
                      label = h3("Evaluación Nueva:"), 
                      choices = c ("All", unique(as.character(a1$lproof))), selected = "All"),
    
      DTOutput('a1'), style = "overflow-y: scroll;overflow-x: scroll;")
  )

  server = function(input, output) {
    a2 <- reactive ({
      if (input$lproof=="D" & input$lproofo=="M"){ 
          a3 <- a1[a1$lproof == "D" & a1$lproofo == "M", ]  
      } else
           if (input$lproof=="G" & input$lproofo=="M"){ 
          a3 <- a1[a1$lroof == "G" & a1$lproofo == "M", ]  
      }  else
           if (input$lproof=="M" & input$lproofo=="G"){ 
          a3 <- a1[a1$lproof == "M" & a1$lproofo == "G", ]  
      } else
          if (input$lproof=="D" & input$lproofo=="G"){ 
          a3 <- a1[a1$lroof == "D" & a1$lproofo == "G", ] 
      }  else
         if (input$lproof=="M" & input$lproofo=="D"){ 
          a3 <- a1[a1$lroof == "M" & a1$lproofo == "D", ] 
      } else
         if (input$lproof=="G" & input$lproofo=="D"){ 
          a3 <- a1[a1$lroof == "G" & a1$lproofo == "D", ]
         }
      return(a3)
    })
    
      output$a1<- renderDataTable ({
                      DT::datatable(a2)
})
  }
shinyApp(ui, server)

Upvotes: 3

Views: 6095

Answers (1)

chemdork123
chemdork123

Reputation: 13833

Your reactive function, is just that - a function. As such, when you use it you need to reference it like you would a function, so a2() and not a2. Looking through your code, It seems the error is stemming from the line:

DT::datatable(a2)

Which should be changed to reference a2 as a function:

DT::datatable(a2())

Upvotes: 7

Related Questions