Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny: How to change background color of a table

Tags:

r

shiny

I found how to change the background color of the User Interface in Shiny. The withdraw I found is that it also colors the background of the tables i'm showing with tableOutput. Here I show a dummy example.

ui.R

shinyUI(pageWithSidebar(
headerPanel("Dummy"),
sidebarPanel( tags$hr() ),

mainPanel(

# This is what I use to change the background color
list(tags$head(tags$style("body {background-color: #ADD8E6; }"))),

tableOutput("dummy")   ) ))

server.R

shinyServer(function(input, output) { output$dummy <- renderTable({ data.frame(A=1:4,B=2:5,C=rep("aaa",4)) }) })

What I get is this

enter image description here

and what I would like to get (I used GIMP to recreate it) is

enter image description here

Thanks everyone for your help!

like image 618
Rufo Avatar asked Feb 20 '14 09:02

Rufo


People also ask

How do I change the background color of a table in CSS?

The better way to change the background color is to add the style property background-color to the table, row, or cell tag.

Which tool is used to apply different background Colours in a table?

The Shading tool is used to apply different background colours in a table.

How do I change the background in R?

In the menu bar, open the “Tools” menu. From the drop down menu, choose “Global Options”. In the pane on the left hand side of the options window, click “Appearance”. To import a theme, click on the “Add…” button.


1 Answers

A solution has been given on the shiny google group:

runApp(
  list(ui = bootstrapPage(pageWithSidebar(
    headerPanel("Rummy"),
    sidebarPanel( tags$hr() ),

    mainPanel(

      tableOutput("dummy"),
      # change style:    
      tags$head(tags$style("#dummy table {background-color: red; }", media="screen", type="text/css"))
    )

  )
  )

  ,
  server = function(input, output) {
    output$dummy <- renderTable({ data.frame(A=1:4,B=2:5,C=rep("aaa",4)) }) 
  }

  )
)

I also invite you to read this discussion on the shiny google group, which shows how to use the pander package to generate html tables and insert them in a shiny app. This allows a more flexible control of the style.

like image 159
Stéphane Laurent Avatar answered Oct 06 '22 11:10

Stéphane Laurent