0

How can I use formattable to color some values in reactive tables ?

Here's a reproducible example : I would like to color p-values in red if they are inferior to 0.05 in the reactive results table I created.

library(DT)
library(shiny)
library(shinydashboard)
library(formattable)


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(    
    selectizeInput("v.dependent", "", choices = names(mtcars), 
                                      selected = "mpg", multiple = FALSE),
    selectizeInput("predictor", "", choices = names(mtcars), 
                                      selected = "disp", 
                                      multiple = TRUE)),
  dashboardBody(
    tabsetPanel(
      tabPanel("test with mtcars",
               box(formattableOutput("tab"))
      )
    )
  )
)

server <- function(input, output) {

  dep.var <- reactive({
    out <- input$v.dependent
    out
  })

  ind.var <- reactive({
    out <- input$predictor
    out
  })

  var.selected <- reactive({
    out <- append(ind.var(), dep.var(), 0)
    out
  })

  user.selection <- reactive({
    mtcars[, names(mtcars) %in% var.selected()]
  })

  lmod <- reactive({
    lm(as.formula(paste(input$v.dependent, "~", paste(input$predictor, collapse = "+"))), data = user.selection())
  })

  output$tab <- renderFormattable({
    tmp <- summary(lmod())$coefficients
    colnames(tmp) <- c("Coefficients", "SD", "t statistic", "Pvalue")
    tmp <- signif(x = tmp, digits = 3)
    tmp <- formattable(tmp, list(Pvalue = formatter("span", 
                         style = x ~ style(color = ifelse(x < 0.05, style(color = "red", "black")))
      )))
  })

}


shinyApp(ui, server)

I know how to do with "static" tables but when I try with this code, I've got the error :

Warning: Error in formatC: 'format' must be one of {"f","e","E","g","G", "fg", "s"}

Any idea how to solve it ?

bretauv
  • 7,756
  • 2
  • 20
  • 57

1 Answers1

2

You're using the syntax for formattable.data.table but in your case tmp is a matrix which behaves differently. It seems you want it to be a data.frame so you can cast it yourself. Also, you seem to have some problems with setting the color in your ifelse. This seems to do what you want

output$tab <- renderFormattable({
    tmp <- summary(lmod())$coefficients
    colnames(tmp) <- c("Coefficients", "SD", "t statistic", "Pvalue")
    tmp <- signif(x = tmp, digits = 3)
    tmp <- as.data.frame(tmp)
    tmp <- formattable(tmp, list(
      Pvalue = formatter("span", style = x ~ style(color = ifelse(x < 0.05, "red", "black"))))
    )
  })
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    it works, thanks a lot. However, I have another problem (I will post another topic if necessary) : when I apply this solution to my actual shiny app, I have an error about plotly_build, which is really strange because this code has nothing to do with plots. Do you have an idea of what might be wrong ? Here's the error (translated) : ```Error in plotly_build: argument "p" is missing, with no default value``` – bretauv Jun 29 '19 at 21:31