gpt4 book ai didi

r - R Shiny 中表格的条件格式

转载 作者:行者123 更新时间:2023-12-04 11:22:45 29 4
gpt4 key购买 nike

我正在尝试可视化队列分析,并希望使用 RenderDataTable在 Shiny 的情况下获得这种可视化,我将能够基于具有值 1/0 的单独列突出显示所有单元格,其中 1 被着色,0 不被着色。

Cohort Table

我尝试了几件事,包括尝试使用 geom_tileggplot2 ,但无济于事。我也试过查看 rpivotTable ,但我无法弄清楚如何为某些单元格着色。

示例数据:

df <- "
cohort wk value flag
1 1 24 0
1 2 12 0
1 3 10 0
1 4 5 0
1 5 2 0
2 1 75 0
2 2 43 1
2 3 11 0
2 4 14 0
3 1 97 0
3 2 35 0
3 3 12 1
4 1 9 0
4 2 4 0
5 1 5 0"

df <- read.table(text = df, header = TRUE)

最佳答案

DT-package :

# global.R

library(shiny)
library(DT)

sketch = htmltools::withTags(table(
class = 'display',
thead(
tr(
th(rowspan = 2, ''),
th(rowspan = 2, 'Cohort'),
th(colspan = 10, 'Wk')
),
tr(lapply(paste(c('', 'f'), rep(1:5, each=2), sep=''), th))
)
))

#ui.R
shinyUI( fluidPage( DT::dataTableOutput(outputId="table") ) )

# server.R
shinyServer(function(input, output, session) {
output$table <- DT::renderDataTable({
df$flag <- as.factor(df$flag)
x <- reshape(df, timevar = 'wk', sep = '_', direction = 'wide',idvar ='cohort')
row.names(x) <- NULL
colnames(x)[-1] <- paste(c('', 'f'), rep(1:5, each = 2), sep = '')
datatable(x, rownames = T, container = sketch,
options = list(dom = 'C<"clear">rti', pageLength = -1,
columnDefs = list(list(visible = F, targets = c(3,5,7,9,11))))
)%>%
formatStyle('1', 'f1', backgroundColor = styleEqual(c(0, 1), c('white','lightblue'))) %>%
formatStyle('2', 'f2', backgroundColor = styleEqual(c(0, 1), c('white','lightblue'))) %>%
formatStyle('3', 'f3', backgroundColor = styleEqual(c(0, 1), c('white','lightblue'))) %>%
formatStyle('4', 'f4', backgroundColor = styleEqual(c(0, 1), c('white','lightblue'))) %>%
formatStyle('5', 'f5', backgroundColor = styleEqual(c(0, 1), c('white','lightblue')))
})
})

\

enter image description here

关于r - R Shiny 中表格的条件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888834/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com