隨着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')))
})
})
\
你能提供一個最小[重複的例子(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)?您的示例圖像沒有0/1列,它有陰影的單元格,而不是行。它是否真的代表您的預期產出? – Molx
感謝@Molx,根據您的意見進行了編輯 –
@Karthikg最好的是使用DT(使用數據表JS庫)。它允許使用條件格式。 – Enzo