www*_*www 2 html r colors shiny dt
这是一个例子。我创建了一个数据框并使用它来创建一个用于可视化的数据表。如您所见,我的列名和第一列中的行表示来自 A 和 B 的条件。我想要做的是更改此数据表中特定单元格的背景颜色。选择要更改的列很容易,如本链接 ( https://rstudio.github.io/DT/010-style.html ) 中所述。但是,如何指定要选择的行对我来说并不明显。
为了给你更多的上下文,我正在开发一个Shiny应用程序,我想设计一个数据表,允许我根据 fromA和的条件为单元格着色B。例如,如果A is less than 1和B is between 1 and 2,我希望能够从A is less than 1列中选择第二个单元格。为了实现这一点,我需要知道如何指定行号或行名。目前,我只知道如何根据行中的内容指定行,如本例所示。
library(tibble)
library(DT)
dat <- tribble(
~`A/B`, ~`A is less than 1`, ~`A is between 1 and 2`, ~`A is larger than 2`,
"B is less than 1", 10, 30, 30,
"B is between 1 and 2", 20, 10, 30,
"B is larger than 2", 20, 20, 10
)
datatable(dat, filter = "none", rownames = FALSE, selection = "none",
options = list(dom = 't', ordering = FALSE)) %>%
formatStyle(
'A is less than 1',
backgroundColor = styleEqual(20, "orange")
)
Run Code Online (Sandbox Code Playgroud)
我不确定这个问题,但是如果您想更改由其行索引和列索引给出的单元格的背景颜色(这就是我的理解),您可以这样做:
changeCellColor <- function(row, col){
c(
"function(row, data, num, index){",
sprintf(" if(index == %d){", row-1),
sprintf(" $('td:eq(' + %d + ')', row)", col),
" .css({'background-color': 'orange'});",
" }",
"}"
)
}
datatable(dat,
options = list(
dom = "t",
rowCallback = JS(changeCellColor(1, 2))
)
)
Run Code Online (Sandbox Code Playgroud)