Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r shiny - DataTableOutput - color specific rows and colomns

Is there a way to color specific rows in r shiny's dataTableOutput?

All I've found is that I can set orderClasses = TRUE to color ordered columns, but I'd like to always color column 2 and row 2 of the output data table. I've been reading through the options manual but haven't found anything.

-----------edit-------------

Ok, so after Yihui and user5029763's suggestions I'm now able to highlight columns using DT, but I'm still having trouble highlighting rows. Here is my code using the iris data set as an example - here I'm trying to highlight entire rows if the Species is setosa, however, on my computer, it's only highlighting the cells that say setosa:

datatable(iris) %>% formatStyle(
  'Species',
  target = 'row',
  backgroundColor = styleEqual('setosa', 'gray')
)

enter image description here

------- edit -----------------------

sorry for the long post - in fact, I ran the exact code as was specified here: http://rstudio.github.io/DT/010-style.html

library(DT)
options(DT.options = list(pageLength = 5))
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

datatable(df) %>% formatStyle(
  'V6',
  target = 'row',
  backgroundColor = styleEqual(c(0, 1), c('gray', 'yellow'))
)

And it is still highlighting just cells rather than rows on my computer. I'm using 3.2.1 version of R and Version 0.99.447 of RStudio. I also have the github version of shiny, not sure if that might override any options in datatable?

like image 689
md1630 Avatar asked Oct 21 '25 00:10

md1630


2 Answers

The link Yihui posted explains formatStyle really well, but I think the argument 'target' is not mentioned there.

To color an entire row, use the argument target="row" in formatStyle.

like image 165
user5029763 Avatar answered Oct 23 '25 14:10

user5029763


The option to use target = 'row' is only available in the developmental version of the DT package on github, not in the CRAN version.

To install the developmental version

install.packages("devtools")
devtools::install_github('rstudio/DT')

Your code above should then work. @yihui can maybe confirm this?

like image 27
Philip Perrin Avatar answered Oct 23 '25 16:10

Philip Perrin