【发布时间】:2019-09-23 14:16:09
【问题描述】:
假设我有以下内容——注意myList:
library(shiny)
myList <- list(
first_element = tibble(a = 1, b = 2),
second_element = tibble(a = 4:5, e = 7:8),
third_element = tibble(a = c("one", "two", "three"), x = c("another", "another one", "another two"))
)
ui <- fluidPage(
titlePanel("A Title"),
verbatimTextOutput("pretty_output")
)
server <- function(input, output, session) {
output$pretty_output <- renderPrint({
myList
})
}
shinyApp(ui, server)
这会导致:
我想要以编程方式将myList 呈现为单独的renderTable 或renderDataTable 元素。下面说明了一种蛮力方法,但我正在寻找更灵活、更 D.R.Y. 的方法,通过利用 for 循环、lapply、purrr::map() 和/或其他方法。
注意:myList 的长度应假定为未知。
library(shiny)
library(DT)
myList <- list(
first_element = tibble(a = 1, b = 2),
second_element = tibble(a = 4:5, e = 7:8),
third_element = tibble(a = c("one", "two", "three"), x = c("another", "another one", "another two"))
)
ui <- fluidPage(
titlePanel("A Title"),
dataTableOutput("dt_01"),
dataTableOutput("dt_02"),
dataTableOutput("dt_03")
)
server <- function(input, output, session) {
output$dt_01 <- renderDataTable({
datatable(myList[[1]], caption = names(myList[1]))
})
output$dt_02 <- renderDataTable({
datatable(myList[[2]], caption = names(myList[2]))
})
output$dt_03 <- renderDataTable({
datatable(myList[[3]], caption = names(myList[3]))
})
}
shinyApp(ui, server)
【问题讨论】:
-
仅供参考:我浓缩了我仓促写下的答案,现在使用 lapply。