R Shiny : formattable does not change anything

I'm writing a Shiny app and I would like to use formattable to color some results under conditions.
I followed the solutions provided here
and here
but none of these worked. Inexplicably, the example below does not color the results (for me at least), though the table is displayed :

library(DT)
library(shiny)
library(shinydashboard)
library(data.table)
library(formattable)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tabsetPanel(box(formattableOutput("dat"))
    )
  )
)

server <- function(input, output) {
  
data <- head(mtcars)
  
  output$dat <- renderFormattable({
    formattable(data, 
                disp = formatter("span", 
                                 style = x ~ style(color = ifelse(x < 200, "green", "gray")))
    )
  })
  
}


shinyApp(ui, server)

Does somebody have a solution ?

StackOverflow link :

The argument after data in formattable() is expecting a list, all we need to do us wrap disp in a list() function. working code below!

library(DT)
library(shiny)
library(shinydashboard)
library(data.table)
library(formattable)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tabsetPanel(box(formattableOutput("dat"))
    )
  )
)

server <- function(input, output) {
  
  data <- head(mtcars)
  
  output$dat <- renderFormattable({
    formattable(data, list(
                disp = formatter("span", 
                                 style = x ~ style(color = ifelse(x < 200, "green", "gray")))
    ))
  })
  
}


shinyApp(ui, server)

it works now, thanks !

1 Like

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