-1

The code works normally in Rstudio, the code is

library(datatable)
library(shiny)
library(magrittr)

datatable(report) %>% formatStyle('status',target = 'row',
            backgroundColor = styleEqual(c("Completed","Over run"), c('lightgreen','red')))

However, I don't know how to output this datatable/formattable? in Shiny.

The error says:

no applicable method for 'as.htmlwidget' applied to an object of class "c('datatables', 'htmlwidget')"
Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33

1 Answers1

0

I hopte this litte shinyApp will help you. Since I dont have your dataframe report, I replaced it by the iris dataframe. So the formatStyle will look for the column Species and color them differently.

In the ui you define the output via DT::dataTableOutput("YourTableID") and in the server you define an output like output$YourTableID <- DT::renderDataTable({ ... }), in which you place the code to generate a datatable.

Also the library you are looking for is DT not datatable.

library(DT)
library(shiny)

report <- iris

ui <- fluidPage(
  DT::dataTableOutput("table")
)

server <- function(input, output, session) {
  output$table <- DT::renderDataTable({
    datatable(report) %>% formatStyle('Species',target = 'row',
                                      backgroundColor = styleEqual(c("setosa","versicolor", "virginica"), 
                                                                   c('lightgreen','red', "yellow")))
  })
}

shinyApp(ui, server)
SeGa
  • 9,454
  • 3
  • 31
  • 70