DT in Shiny:仅更改单行的颜色 [英] DT in Shiny: Change only the colour of a single row

查看:34
本文介绍了DT in Shiny:仅更改单行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集:

  ID值
102306
41800
101783
105193
myID 334

我想起草这个作为数据表,其中只有带有 myID的行被着色为橙色,而表的其余部分为蓝色。看过

  df = read.table(text ='ID值
102306
41800
101783
105 193
myID 334',标题= T)

库(DT)

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

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


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?

解决方案

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

这篇关于DT in Shiny:仅更改单行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆