Say I have the following -- taking note of 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)
This results in:
What I would like is to present myList
as either individual renderTable
or renderDataTable
elements programmatically. The following illustrates a brute force approach, but I am looking for something a little more flexible, more D.R.Y., by leveraging a for loop, lapply
, purrr::map()
, and/or something else.
NOTE: The length of myList
should be assumed to be unknown.
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)