0

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

> MyData
Tab       Classification_Level    Month_considered   Primary_family Secondary_family Metric_name Metric_Value
1 Level1         Brand family           Apr-17          NP              <NA>        <NA>          34,585
2 Level1      Brand Subfamily           Apr-17          PM             <NA>        <NA>           7,401
3 Level1      Brand Subfamily           Apr-17          NP             <NA>        <NA>           34,596
4 Level1      Brand Subfamily           Apr-17         MB NP           <NA>        <NA>           15,985
5 Level1      Brand Subfamily           Apr-17         MB M            <NA>        <NA>           9,712
6 Level1                Brand           Apr-17         KL              <NA>        <NA>           6,242
> sapply(MyData,class)
         Tab Classification_Level     Month_considered       Primary_family     Secondary_family 
    "factor"             "factor"             "factor"             "factor"             "factor" 
 Metric_name         Metric_Value 
    "factor"

 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())

  })
SNT
  • 1,283
  • 3
  • 32
  • 78
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. (Use a `dput()` rather than `str()` and be clear if what you are sharing is `MyData` or the result of the reactive transformation). Your data looks messed up and it's unclear exactly how it might have gotten that way, – MrFlick Jul 30 '18 at 14:15
  • Added MyData @MrFlick – SNT Jul 30 '18 at 14:27
  • That's not in a reproducible format. See the link I provided for the correct way to add data to your question. What's the object that you showed the `str()` for then? – MrFlick Jul 30 '18 at 14:28
  • 2
    Oh, is the problem that you're calling `DT::datatable()` twice? What if you just did `output$op1 <- renderDataTable({abc()})`? – MrFlick Jul 30 '18 at 14:30
  • @MrFlick twice calling DT:datatable() was the issue. Thank you. – SNT Jul 30 '18 at 14:39

1 Answers1

0

If there are no issues with the data.table definition, then the issue seems to be the render portion. 'renderDataTable' is there in shiny as well as in DT package. Can you try DT::renderDataTable?

Rituraj Achuthan
  • 78
  • 1
  • 1
  • 7