RShiny中表的条件格式化

Kar*_*k g 7 r knitr r-markdown shiny

我正在试图想象一个队列分析,并希望RenderDataTable在闪亮中使用这种可视化,我可以基于具有值1/0的单独列突出显示所有单元格,其中1为阴影,0不被遮蔽.

队列表

我试了几件事情,其中包括试图使用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)
Run Code Online (Sandbox Code Playgroud)

use*_*763 6

随着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))
    )
))
Run Code Online (Sandbox Code Playgroud)

#ui.R

shinyUI( fluidPage( DT::dataTableOutput(outputId="table") ) )
Run Code Online (Sandbox Code Playgroud)

#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'))) 
    })  
})  
Run Code Online (Sandbox Code Playgroud)

\

在此输入图像描述

  • 并不是对这个答案的不尊重,有些用户似乎很喜欢这个答案,并且也得到了丰厚的奖励,但是一些额外的解释或评论会很好,我在过去的 5 到 10 分钟里一直盯着这个答案,我不知道发生了什么这里。 (2认同)

Jas*_*lns 0

这应该可以帮助您开始使用以下命令创建绘图ggplot2

library(ggplot2)

ggplot(df, aes(x = wk, y = cohort, fill = factor(flag))) + 
  geom_tile(color = "white") +
  geom_text(aes(label = value), color = "white") +
  scale_y_reverse()
Run Code Online (Sandbox Code Playgroud)

群组图

以闪亮的方式渲染绘图应该是微不足道的,并且由于您没有提供任何闪亮的代码(例如服务器或用户界面),因此很难说您可能在哪里遇到问题。