Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny color dataframe

I have a data frame:

   runApp(
      list(ui = bootstrapPage(pageWithSidebar(
        headerPanel("Data frame with colors"),
        sidebarPanel(),
        mainPanel(
           tableOutput("my_dataframe")
        ) 
      )
     )
   ,
    server = function(input, output) {
       output$my_dataframe <- renderTable({ 
               data.frame("Brand ID"=1:4,"Client1"=c("red", "green", "green", "green"),
                                         "Client2"=c("green", "red", "green", "red")) 
       }) 
    }
)
)

Is it possible to color data frame like:

enter image description here

For example, when I have contidion1 I need to color data frame cell with red, on condition2 - with green.

Any help would be really appreciated.

like image 871
Marta Avatar asked Mar 27 '14 10:03

Marta


People also ask

Why are my DataTables parameter names different in shiny?

If you have used DataTables in Shiny before (specifically, before Shiny v0.10.2), you may need to change some parameter names for your DataTables, because Shiny (<= v0.10.1) was using DataTables v1.9, and DataTables v1.10 has changed the parameter names.

Is it possible to use htmlwidgets DT with shiny DataTable?

I would like to use htmlwidgets DT, as I think it is aesthetically a little better than default Shiny DataTable and some of the other options seem easier to work with - table CSS classes, custom table containers, custom filters etc. Sorry, something went wrong. @phillc73 Yes, it is possible.

What are the functions of the shiny and DT packages?

Note: Both shiny and DT packages have functions named dataTableOutput and renderDataTable.


1 Answers

Here is a solution. To use it, you have to define css in a vector:

css <- c("#bgred {background-color: #FF0000;}",
          "#bgblue {background-color: #0000FF;}")

and write #... inside the cell :

> data.frame(x=c("A","B"), y=c("red cell #bgred", "blue cell #bgblue"))
  x                 y
1 A   red cell #bgred
2 B blue cell #bgblue

Then use my colortable() function mainly inspired from the highlightHTML package and from my personal shiny experience. Here is an example:

library(pander)
library(markdown)
library(stringr)
library(shiny)

# function derived from the highlightHTMLcells() function of the highlightHTML package
colortable <- function(htmltab, css, style="table-condensed table-bordered"){
  tmp <- str_split(htmltab, "\n")[[1]] 
  CSSid <- gsub("\\{.+", "", css)
  CSSid <- gsub("^[\\s+]|\\s+$", "", CSSid)
  CSSidPaste <- gsub("#", "", CSSid)
  CSSid2 <- paste(" ", CSSid, sep = "")
  ids <- paste0("<td id='", CSSidPaste, "'")
  for (i in 1:length(CSSid)) {
    locations <- grep(CSSid[i], tmp)
    tmp[locations] <- gsub("<td", ids[i], tmp[locations])
    tmp[locations] <- gsub(CSSid2[i], "", tmp[locations], 
                           fixed = TRUE)
  }
  htmltab <- paste(tmp, collapse="\n")
  Encoding(htmltab) <- "UTF-8"
  list(
    tags$style(type="text/css", paste(css, collapse="\n")),
    tags$script(sprintf( 
                  '$( "table" ).addClass( "table %s" );', style
                )),
    HTML(htmltab)
  )
}

##
runApp(
  list(
    ui=pageWithSidebar(
      headerPanel(""),
      sidebarPanel(
      ),
      mainPanel(
        uiOutput("htmltable")
      )
    ),
    server=function(input,output,session){
      output$htmltable <- renderUI({
        # define CSS tags
        css <- c("#bgred {background-color: #FF0000;}",
                 "#bgblue {background-color: #0000FF;}")
        # example data frame 
        # add the tag inside the cells
        tab <- data.frame(x=c("A","B"), y=c("red cell #bgred", "blue cell #bgblue"))
        # generate html table with pander package and markdown package
        htmltab <- markdownToHTML(
          text=pandoc.table.return(
            tab, 
            style="rmarkdown", split.tables=Inf
          ), 
          fragment.only=TRUE
        ) 
        colortable(htmltab, css)
      })
    })
)

enter image description here

like image 163
Stéphane Laurent Avatar answered Oct 12 '22 01:10

Stéphane Laurent