DT in Shiny: Change only the colour of a single row

余生长醉 提交于 2019-12-10 14:23:31

问题


I have a dataset:

ID Value
102 306
41  800
101 783
105 193
myID 334

I would like to draw this up as a datatable where only the row with 'myID' is coloured orange and the rest of the table is blue. Having looked at the helper functions and other examples, it seems that I should be using styleEqual. However, I don't know what the values in my other rows are, and also they will change dynamically.

I tried using

datatable(tableData) %>%
formatStyle(0, target= 'row',color = 'black', backgroundColor = tableColour, 
                lineHeight='70%', padding = '3px 3px', fontSize = '80%') %>%
    formatStyle('ID', target = 'row', 
    backgroundColor = styleEqual(c("myID"), c('orange')))

However, this does not work - the whole table is blue, and the second formatStyle statement is ignored. If I remove the first formatStyle, I get my row coloured in orange but lose all other formatting. Is there a way to use styleEqual to define e.g. c("myID", "All other IDs"), or is there another workaround?


回答1:


I can think of two possible workarounds:

  • Create a helper column that is 1 or 0, based on if your column is equal to myID or not, then use that column to style the table and hide that column.
  • Create a column mapping for all your unique values in the column ID, that defaults to a certain color, and set only the value corresponding to myID to orange.

A working example of the second option is given below. Hope this helps!


df = read.table(text='ID Value
102 306
41  800
101 783
105 193
myID 334',header=T)

library(DT)

my_vals = unique(df$ID)
my_colors = ifelse(my_vals=='myID','orange','grey')

datatable(df) %>%
  formatStyle('ID', target = 'row', 
              backgroundColor = styleEqual(my_vals,my_colors))



回答2:


If you want to use DT you can use styleEqual() to add background color to your table if the condition is met. There does not seem to be an else option, so you can create an object of class JS_EVAL (which would be returned by styleEqual()) and add a negation:

background <- "value == 'myID' ? 'orange' : value != 'else' ? 'blue' : ''"  
class(background) <- "JS_EVAL"

datatable(tableData) %>% formatStyle(
  'ID',
  target = 'row',
  backgroundColor = background
)

The result looks like this:

You could also achieve it using the package tableHTML:

library(tableHTML)

tableData %>% 
  tableHTML(rownames = FALSE,
            widths = c(100, 100)) %>% 
  add_css_row(rows = which(tableData$ID == 'myID') + 1,
              css = list(c("background-color"),
                         c("orange"))) %>% 
  add_css_row(rows = which(tableData$ID != 'myID') + 1,
              css = list(c("background-color"),
                         c("blue")))

The result looks like this:



来源:https://stackoverflow.com/questions/49176026/dt-in-shiny-change-only-the-colour-of-a-single-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!