[英]Format color of shiny datatable (DT) according to values in a different dataset
I am trying to format the DT according to the values from the previous table. 我正在尝试根据上表中的值格式化DT。 For example, I want to display if something has increased, decreased or remained the same. 例如,我想显示是否增加,减少或保持不变。 I could do this with kable but could not get the next step where I want to clik a the cell and show all the data related to that value in another DT. 我可以使用kable来执行此操作,但是无法进行下一步,在该步骤中我想单击一个单元格并在另一个DT中显示与该值相关的所有数据。
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
mainPanel(
dataTableOutput("iris_head")
)
)
server <- function(input, output) {
#df_data <- iris
df_data <- head(iris[-5])
# Just a dataset describing if iris has changed over a month
# If reference data is of the same size as the original data (df_data).
# If reference data is negative I want the cell in the df_data to be green;
# If zero blue and if positive then green.
# I can make changes with ranges within the current range, can we get the color encoding from another table?
# set the seed
set.seed(42)
reference_df <- (sapply(df_data, function(x) jitter(x, amount = 2)) - df_data) %>%
round(. , digits = 0)
print(reference_df)
output$iris_head <- renderDataTable(datatable(df_data, selection = "single" )%>%
formatStyle('Sepal.Width',
color = styleInterval(c(3.4, 3.8), c('green', 'blue', 'red')),
backgroundColor = styleInterval(3.4, c('gray', 'yellow'))) %>%
formatString('Sepal.Width', suffix = '<font color="red">⇑ </font>'))
}
shinyApp(ui = ui, server = server)
The reference_df
in this case is: 在这种情况下, reference_df
为:
Sepal.Length Sepal.Width Petal.Length Petal.Width
2 1 2 0
2 -1 -1 0
-1 1 0 2
1 1 2 -1
1 0 2 2
0 1 -2 2
The required output is shown in the Figure where I also want to color the text and if possible the background according to values in reference_df. 所需的输出显示在该图中,在这里我也想根据reference_df中的值给文本加上颜色(如果可能)。
For the text color part, you could do it with formatStyle
but you would need to cbind
the df_data
and reference_df
, then pass it to datatable
and change the style of columns 1 to 4 based on the value of columns 5 to 8: 对于文本颜色的部分,你可以用做formatStyle
但你需要cbind
的df_data
和reference_df
,然后把它传递给datatable
和改变1列的风格,基于列5至8值4:
datatable(cbind(df_data,reference_df), selection = "single",
options=list(columnDefs = list(list(visible=FALSE, targets=c(5:8)))))%>%
formatStyle(1:4, valueColumns=5:8,
color = JS("value < 0 ? 'red' : value > 0 ? 'green' : 'blue'"))
The columnDefs part hides the last 4 columns. columnDefs部分隐藏了最后4列。
You can't formatString
based on values so if you want to add the arrows, you could modify df_data
to add the colors and arrows before passing it to datatable
: 您不能基于值设置formatString
,因此,如果要添加箭头,可以在将df_data
传递到datatable
之前修改df_data
以添加颜色和箭头:
for(col in 1:dim(df_data)[2]){
df_data[col] <- mapply(function(i,j){
ifelse(i > 0, paste0("<span style='color:red'>",j,"<font>⇑ </font></span>"),
ifelse(i<0, paste0("<span style='color:green'>",j,"<font>⇓ </font></span>"),
paste0("<span style='color:blue'>",j,"<font>⇔ </font></span>")))
},reference_df[col],df_data[col])
}
output$iris_head <- renderDataTable(
datatable(df_data, selection = "single",escape = F)
)
This loops through the values of df_data
and changes them depending on the values of reference_df
. 这遍历的值df_data
和改变它们取决于值reference_df
。 You need escape=F
as an argument in the datatable
call to prevent HTML escaping. 你需要escape=F
作为参数datatable
呼叫阻止HTML逃跑。
You can add more CSS styling in the span
tags if you want to color the background etc. 如果您想给背景上色,可以在span
标签中添加更多CSS样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.