【问题标题】:Display reactive htmlTable table in Shiny在 Shiny 中显示反应式 htmlTable 表
【发布时间】:2018-10-19 16:54:40
【问题描述】:

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

Shiny 显示 html 代码而不是表格。我不知道用什么替换 server-part 中的“renderTable”和 ui-part 中的 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我改变了两件事:

    1. 您需要renderUIhtmlOutput 对。
    2. 我将htmlTable 包装到来自shinyHTML 函数中,以告诉shiny () 括号内的每个都在html 中。

    对于未来的shiny-apps,我建议使用 RStudio 的备忘单:https://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)
    

    【讨论】:

    猜你喜欢
    • 2019-12-02
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多