RShiny中表的条件格式 [英] Conditional formatting of a table in RShiny

查看:81
本文介绍了RShiny中表的条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对同类群组分析进行可视化,并希望在发亮的环境中使用RenderDataTable以获得这种可视化效果,在此我可以根据值1/0的单独列来突出显示所有单元格,其中1被阴影着色,而0不被阴影着色.

I'm trying to visualize a cohort analysis, and wanted to use RenderDataTable in shiny to get this sort of a visualization where I would be able to highlight all the cells based on a separate column having values 1/0, with 1 being shaded and 0 not being shaded.

我尝试了几件事,包括尝试在ggplot2中使用geom_tile,但这没有用.我也尝试查看rpivotTable,但无法弄清楚如何为某些单元格着色.

I Tried a couple of things, including trying to use geom_tile in ggplot2, but it was of no avail. I also tried looking at rpivotTable, but I wasn't able to figure out how to shade certain cells.

示例数据:

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") ) )

#服务器.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'))) 
    })  
})  

\

这篇关于RShiny中表的条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆