根据不同数据集中的值格式化闪亮数据表 (DT) 的颜色
Format color of shiny datatable (DT) according to values in a different dataset
我正在尝试根据之前 table 的值格式化设备标识符。
例如,我想显示一些东西
增加、减少或保持不变。
我可以用 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)
本例中的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
图中显示了所需的输出,其中我还想根据 reference_df 中的值为文本和背景着色(如果可能的话)。
对于文本颜色部分,您可以使用 formatStyle
来完成,但您需要 cbind
df_data
和 reference_df
,然后将其传递给 datatable
并根据第 5 至 8 列的值更改第 1 至 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'"))
columnDefs 部分隐藏了最后 4 列。
您不能 formatString
基于值,因此如果您想添加箭头,您可以修改 df_data
以添加颜色和箭头,然后再将其传递给 datatable
:
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)
)
这遍历 df_data
的值并根据 reference_df
的值更改它们。您需要 escape=F
作为 datatable
调用中的参数以防止 HTML 转义。
如果您想为背景着色等,可以在 span
标签中添加更多 CSS 样式。
我正在尝试根据之前 table 的值格式化设备标识符。 例如,我想显示一些东西 增加、减少或保持不变。 我可以用 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)
本例中的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
图中显示了所需的输出,其中我还想根据 reference_df 中的值为文本和背景着色(如果可能的话)。
对于文本颜色部分,您可以使用 formatStyle
来完成,但您需要 cbind
df_data
和 reference_df
,然后将其传递给 datatable
并根据第 5 至 8 列的值更改第 1 至 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'"))
columnDefs 部分隐藏了最后 4 列。
您不能 formatString
基于值,因此如果您想添加箭头,您可以修改 df_data
以添加颜色和箭头,然后再将其传递给 datatable
:
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)
)
这遍历 df_data
的值并根据 reference_df
的值更改它们。您需要 escape=F
作为 datatable
调用中的参数以防止 HTML 转义。
如果您想为背景着色等,可以在 span
标签中添加更多 CSS 样式。