如何为列表中的单个列表元素提供renderDataTable或renderTable
假设我有以下内容——注意如何为列表中的单个列表元素提供renderDataTable或renderTable,r,shiny,R,Shiny,假设我有以下内容——注意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 &
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)
请检查以下
lappy
方法:
library(shiny)
library(DT)
myList <- list(
first_element = data.frame(a = 1, b = 2),
second_element = data.frame(a = 4:5, e = 7:8),
third_element = data.frame(a = c("one", "two", "three"), x = c("another", "another one", "another two"))
)
ui <- fluidPage(
titlePanel("A Title"),
uiOutput("tables")
)
server <- function(input, output, session) {
lapply(names(myList), function(x) {
output[[x]] = renderDataTable({myList[[x]]})
})
output$tables <- renderUI({
lapply(names(myList), dataTableOutput)
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(DT)
我的列表所以我认为大多数方法都会涉及到renderUI
,以下是我的想法(感觉很优雅),但我会暂时不回答这个问题,看看其他人是否可以插话。严重受以下因素影响的代码:
库(闪亮)
我的列表仅供参考:我现在用Lappy压缩了我草草写下的答案。
library(shiny)
library(DT)
myList <- list(
first_element = data.frame(a = 1, b = 2),
second_element = data.frame(a = 4:5, e = 7:8),
third_element = data.frame(a = c("one", "two", "three"), x = c("another", "another one", "another two"))
)
ui <- fluidPage(
titlePanel("A Title"),
uiOutput("tables")
)
server <- function(input, output, session) {
lapply(names(myList), function(x) {
output[[x]] = renderDataTable({myList[[x]]})
})
output$tables <- renderUI({
lapply(names(myList), dataTableOutput)
})
}
shinyApp(ui, server)
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"),
uiOutput("tables")
)
server <- function(input, output, session) {
# Create the outputs dynamically
output$tables <- renderUI({
tableList <- imap(myList, ~ {
tagList(
h4(.y), # Note we can sprinkle in other UI elements
tableOutput(outputId = paste0("table_", .y))
)
})
tagList(tableList)
})
# Now render each output
iwalk(myList, ~{
output_name <- paste0("table_", .y)
output[[output_name]] <- renderTable(.x)
})
}
shinyApp(ui, server)