r - 在 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†"),
n.cgroup = c(2,2),
caption="Basic table with both column spanners (groups) and row
groups",
tfoot="† A table footer commment")
})
output$filetable <- renderTable({selectedData()})
}
shinyApp(ui,server)
解决方案
我改变了两件事:
- 你需要
renderUI
andhtmlOutput
对。 - 我将 包装
htmlTable
成一个HTML
-function fromshiny
,以告诉-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†"),
n.cgroup = c(2,2),
caption="Basic table with both column spanners (groups) and row
groups",
tfoot="† A table footer commment")
)
})
output$filetable <- renderUI({selectedData()})
}
shinyApp(ui,server)
推荐阅读
- python - 为什么 wd.switch_to.default_content() 不返回原始网页?
- python - 在 Python 中使用 speech_recognition 在终端中获取不需要的输出
- pandas - 拆分列并提取国家、城市和组织名称
- android - 使用 Android Room Insert AsyncTask 时如何获取插入 ID?
- dart - Dart:在“if”语句中检查子类型后尝试访问字段时出现编译器错误
- actions-on-google - Actions Builder:延迟过渡到下一个场景(理想情况下,直到演讲结束)
- php - Guzzle 满足什么通用 HTTP 客户端接口?
- ibm-midrange - 从 RPGLE 程序问题调用 CL 程序
- javascript - 为什么 Apple 会像在 apple.com 上那样创建一些图像资产?
- flutter - 创建提供程序时调用方法