首页 > 解决方案 > 在 Shiny 中显示反应式 htmlTable 表

问题描述

我正在制作我的第一个 Shiny 应用程序,但找不到任何关于如何显示使用 htmlTable 包创建的表格的示例。我基本上想在按下按钮时创建一个表格并显示它。

Shiny 显示 html 代码而不是表格。我不知道用什么替换服务器部分中的“renderTable”和 ui 部分中的 tableOutput。

我没有使用普通表,因为我需要 htmlTable 包中提供的跨列功能。

library(shiny)
library(htmlTable)

####################  ui part ##############################################
ui <- pageWithSidebar(
headerPanel("Tables"),

sidebarPanel(
    actionButton("goButton", "Run Table")
  ),
  mainPanel(
    tableOutput("filetable")
  )
)

####################  server part #######################################    
server <- function(input,output)
{

  selectedData <- eventReactive(input$goButton, {

    # Create the table (using table from htmlTables doc as example)
    htmlTable(matrix(paste("Content", LETTERS[1:16]), 
                 ncol=4, byrow = TRUE),
          header =  paste(c("1st", "2nd",
                            "3rd", "4th"), "header"),
          rnames = paste(c("1st", "2nd",
                           "3rd", "4th"), "row"),
          rgroup = c("Group A",
                     "Group B"),
          n.rgroup = c(2,2),
          cgroup = c("Cgroup 1", "Cgroup 2&dagger;"),
          n.cgroup = c(2,2), 
          caption="Basic table with both column spanners (groups) and row 
groups",
          tfoot="&dagger; A table footer commment") 
  })

  output$filetable <- renderTable({selectedData()})

}

shinyApp(ui,server)

标签: rshiny

解决方案


我改变了两件事:

  1. 你需要renderUIandhtmlOutput对。
  2. 我将 包装htmlTable成一个HTML-function from shiny,以告诉-bracketsshiny中的每个()都在 html 中。

对于未来的应用程序,我建议使用 RStudio 的备忘单: httpsshiny ://shiny.rstudio.com/images/shiny-cheatsheet.pdf

library(shiny)

library(htmlTable)

####################  ui part ##############################################
ui <- pageWithSidebar(
  headerPanel("Tables"),

  sidebarPanel(
    actionButton("goButton", "Run Table")
  ),
  mainPanel(
    htmlOutput("filetable")
  )
)

####################  server part #######################################    
server <- function(input,output)
{

  selectedData <- eventReactive(input$goButton, {

    # Create the table (using table from htmlTables doc as example)
    HTML(
    htmlTable(matrix(paste("Content", LETTERS[1:16]), 
                     ncol=4, byrow = TRUE),
              header =  paste(c("1st", "2nd",
                                "3rd", "4th"), "header"),
              rnames = paste(c("1st", "2nd",
                               "3rd", "4th"), "row"),
              rgroup = c("Group A",
                         "Group B"),
              n.rgroup = c(2,2),
              cgroup = c("Cgroup 1", "Cgroup 2&dagger;"),
              n.cgroup = c(2,2), 
              caption="Basic table with both column spanners (groups) and row 
              groups",
              tfoot="&dagger; A table footer commment") 
    )

  })

  output$filetable <- renderUI({selectedData()})

}

shinyApp(ui,server)

推荐阅读