'data' must be 2-dimensional (e.g. data frame or matrix) while using DT format R Shiny

dt

#1

I am trying to format an output using DT . Below is the code where I get my dataframe

 a <- reactive({
    MyData %>%
      filter(
        Classification_Level == input$selected_class &
          Primary_family == input$selected_product &
          Metric_name == input$selected_metric
      ) %>%
      mutate(`ATC_Count` = Metric_Value) %>%
      mutate(`pct` = as.numeric(as.character(Metric_Value)) * 100) %>%
      select(Month_considered, `pct`) %>%
      group_by(Month_considered)
  })

When I check for the class for this data.frame

List of 8
 $ x            :List of 5
  ..$ filter   : chr "none"
  ..$ data     :'data.frame':	11 obs. of  3 variables:
  .. ..$                 : chr [1:11] "1" "2" "3" "4" ...
  .. ..$ Month_considered: Date[1:11], format: "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" ...
  .. ..$ pct             : num [1:11] 33.4 34.8 36.6 36.6 34.6 ...

Now while trying to format and highlight a column I get the error 'data' must be 2-dimensional (e.g. data frame or matrix)

abc<- reactive ({datatable(a()) %>% formatStyle(
    'pct',
    backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
  )})
 output$op1 <- renderDataTable({
    DT::datatable(abc())

  })

#2

Why do you have group_by(Month_considered) at the end of the dplyr chain? My initial guess is that if a is a grouped data frame it could be causing the issue.


#3

I did try removing group_by and still throws the same error.


#4

I'm a little confused by the reproducible example provided. What is throwing the error 'data' must be 2-dimensional (e.g. data frame or matrix)?

This seems to reveal that this is in fact a list, not a data.frame.

If you're still having issues, could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.