首页 > 解决方案 > 在 Shiny 中显示 htmlTable

问题描述

我创建了一个返回 htmlTable 的函数,但我找不到我需要在闪亮的应用程序中正确打印它的渲染和输出。

我已经尝试过:

闪亮的输出(带有renderUI和htmlOutput)是用于制作表格而不是表格本身的html代码。在 html() 中将 htmlTable() 包装在我的函数中,导致没有输出闪亮。

更新:在我的函数中包装 htmlTable() 在 HTML() 中有效。感谢大家的所有提示!

my_function <- function(nr_TP, nr_sus_normal, nr_FP, nr_FN, nr_sus_fraud, nr_TN){
  result_table <- cbind(Normal = c("nr_TP", "nr_sus_normal", "nr_FP"), Fraud = c("nr_FN", "nr_sus_fraud", "nr_TN"))
  row.names(result_table) <- c('Normal', "Suspicious", "Fraud")

  where <- rbind(c(1,1), c(1,2), c(2,1), c(2,2), c(3,1), c(3,2))
  style <- c('background-color: green; color: white;',
             'background-color: red; color: white;',
             'background-color: yellow; color: black;',
             'background-color: yellow; color: black;',           
             'background-color: orange; color: black;',
             'background-color: green; color: white;')

  css.cell <- matrix('', nrow(result_table), ncol(result_table))
  css.cell[where] <- style

  htmlTable(result_table, css.cell = css.cell, cgroup = c("As classified by AD"),n.cgroup = c(2))}


ui <- fluidPage(
  titlePanel("Test-report and correlation overview"),

  sidebarLayout(
    sidebarPanel(
      numericInput("nr_TP", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10),
      numericInput("nr_sus_normal", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10),
      numericInput("nr_FP", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10),
      numericInput("nr_FN", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10),
      numericInput("nr_sus_fraud", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10),
      numericInput("nr_TN", 
                   h3("Number of Digits"), 
                   value = 4, min = 0, max = 10)

),
    mainPanel(      
    uiOutput("performance_table"),
    )
  )
)

server <- function(input, output) { 

  output$performance_table <- renderUI({

    inFile <- dataInput()

    if (is.null(inFile))
    {return(NULL)}

    else
      {
      performance_table(input$nr_TP, input$nr_sus_normal, input$nr_FP, input$nr_FN, input$nr_sus_fraud, input$nr_TN)
      }
 }

我通常有不同的输入,函数 performance_table 会自行计算这些值。但问题不在于功能,问题是我无法在我的闪亮中显示 htmlTable。当我使用格式表时(连同 formattableOutput 和 renderFormattable 我很好,但我想在不使用依赖格式的情况下为不同的单元格着色,而且我无法在格式表中修复它)。

标签: rshiny

解决方案


您应该结合使用 print 功能和 kable extra。然后像这样使用 renderUI 和 UI 输出:

服务器部分:

output$table <- renderUI(print(kable(table_of_analysis(), caption = "Title : Subitle") %>% kable_styling()))

用户界面部分:

uiOutput("table")

推荐阅读