在Shiny中仅突出显示表中的一列

时间:2014-12-03 03:29:55

标签: shiny

我目前正在设计一个输出表格的Shiny应用程序。我想强调特定列中的细胞(例如,使细胞变蓝)。我已尝试使用shinyBS包中的HighlightRows函数,但这似乎无法正常工作。

以下是构成表格的服务器脚本的一部分:

output$text1 <- renderTable({
  tab1 <- as.data.frame(matrix(c(rrround(input$patha,3),PowerF()$tta,input$nxn,rrround(currentInput()$patha,3),rrround(rxyval()$rxy,3),rrround(rxyval()$rxy_p,3),rround(PowerF()$tra,3),
                                  rrround(input$pathp,3),PowerF()$ttp,input$nxn,rrround(currentInput()$pathp,3),rrround(rxyval()$rxyp,3),rrround(rxyval()$rxyp_p,3),rround(PowerF()$trp,3))
                                ,ncol=7, byrow=TRUE))

   rownames(tab1) <- c('Actor', 'Partner')
   colnames(tab1) <- c('Size', 'Power', 'N','Beta','r','partial r','ncp')
   tab1.align = "r"

   highlightRows(session, id='tab1', class = "info", column="Power", regex = ".")
   print(tab1, type="html")

})

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用tags$script修改数据表。下面是突出显示样本数据表的3列(1),(5)和(9)的示例。我遇到类似问题的小问题,您可以查看How to change Datatable row background colour based on the condition in a column, Rshiny

rm(list = ls())
library(shiny)
options(digits.secs=3) 

test_table <- cbind(rep(as.character(Sys.time()),10),rep('a',10),rep('b',10),rep('b',10),rep('c',10),rep('c',10),rep('d',10),rep('d',10),rep('e',10),rep('e',10))
colnames(test_table) <- c("Time","Test","T3","T4","T5","T6","T7","T8","T9","T10")

ui =navbarPage(inverse=TRUE,title = "Coloring datatables",
               tabPanel("Logs",icon = icon("bell"),
                        mainPanel(htmlOutput("logs"))),
               tabPanel("Extra 2",icon = icon("bell")),
               tabPanel("Extra 3",icon = icon("bell")),
               tags$style(type="text/css", "#logs td:nth-child(1) {text-align:center;background-color:red;color: white;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(5) {text-align:center;background-color:blue;color: white;text-align:center}"),
               tags$style(type="text/css", "#logs td:nth-child(9) {text-align:center;background-color:green;color: white;text-align:center}")
)
server <- (function(input, output, session) {

  my_test_table <- reactive({
    other_data <- rbind(c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2)),  
                        (c(as.character(Sys.time()),(sample(c("Pass","Aggr","Bad"))[1]),round(c(rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1),rnorm(1)),2))))
    test_table <<- rbind(apply(other_data, 2, rev),test_table)
    as.data.frame(test_table) 
  })
  output$logs <- renderTable({my_test_table()},include.rownames=FALSE)

})

runApp(list(ui = ui, server = server))