highlight rows in renderTable

1,884 views
Skip to first unread message

Tracy Nance

unread,
Sep 9, 2013, 7:53:16 PM9/9/13
to shiny-...@googlegroups.com
Hi, 

I'm trying to change the color of some rows in a renderTable output -- but it seems like the arguments sent to print.xtable are just being printed as text at the top of the table. Clearly I'm doing something wrong! Below is a slightly modified shiny tutorial example (the add.to.row line in server.R)...

thanks,
Tracy

server.R
-----------
library(shiny)
library(datasets)

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {
  
  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
  
  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })
  
  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  }, add.to.row = list(pos = as.list(1:3), command = rep("\\rowcolor[gray]{.8} ", 3)))
})

-------
ui.R
-------

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
  
  # Application title
  headerPanel("Shiny Text"),
  
  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "pressure", "cars")),
    
    numericInput("obs", "Number of observations to view:", 10)
  ),
  
  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    verbatimTextOutput("summary"),
    
    tableOutput("view")
  )
))

Kirill Savin

unread,
Sep 10, 2013, 1:10:28 AM9/10/13
to shiny-...@googlegroups.com
I am not very good with xtable and found it easier to generate html tags for tables myself.

You can assign specific CSS classes to rows you want highlighted. Then pass tags through renderText() instead of renderTable(). I recently had the same problem and uploaded several example tables. You may want to check it out.
https://groups.google.com/forum/#!topic/shiny-discuss/ZaYYmFLHA6U

Tracy Nance

unread,
Sep 12, 2013, 1:58:39 AM9/12/13
to shiny-...@googlegroups.com
Thanks Kirill, this works great! I'll be using the selectable rows in other contexts too...

best,
Tracy
Reply all
Reply to author
Forward
0 new messages