简体   繁体   English

DT in Shiny:仅更改单行的颜色

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

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. 我想把它画成一个数据表,其中只有'myID'的行是橙色的,而其余的表是蓝色的。 Having looked at the helper functions and other examples , it seems that I should be using styleEqual. 看过辅助函数其他示例 ,似乎我应该使用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. 但是,这不起作用 - 整个表是蓝色的,第二个formatStyle语句被忽略。 If I remove the first formatStyle, I get my row coloured in orange but lose all other formatting. 如果我删除第一个formatStyle,我的行会变成橙色,但会丢失所有其他格式。 Is there a way to use styleEqual to define eg c("myID", "All other IDs") , or is there another workaround? 有没有办法使用styleEqual来定义例如c("myID", "All other IDs") ,还是有另一种解决方法?

If you want to use DT you can use styleEqual() to add background color to your table if the condition is met. 如果要使用DT ,可以使用styleEqual()在满足条件的情况下为表添加背景颜色。 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: 似乎没有else选项,因此您可以创建类JS_EVAL的对象(由styleEqual()返回)并添加否定:

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 : 您也可以使用tableHTMLtableHTML实现它:

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: 结果如下:

在此输入图像描述

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. 根据列是否等于myID ,创建一个1或0的辅助列,然后使用该列设置表的样式并隐藏该列。
  • 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. 为列ID中的所有唯一值创建列映射,默认为某种颜色,并仅将与myID对应的值设置为橙色。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM