【问题标题】:How to renderDataTable or renderTable for individual list elements from a list in shiny如何为闪亮列表中的单个列表元素渲染数据表或渲染表
【发布时间】: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 呈现为单独的renderTablerenderDataTable 元素。下面说明了一种蛮力方法,但我正在寻找更灵活、更 D.R.Y. 的方法,通过利用 for 循环、lapplypurrr::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。

标签: r shiny


【解决方案1】:

请检查以下lapply 方法:

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)

【讨论】:

    【解决方案2】:

    所以我认为大多数方法都会涉及renderUI,这就是我所采用的方法(感觉非常优雅),但我会暂时搁置这个问题,看看其他人是否可以加入。代码受到@987654321 的严重影响@:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-05
      • 2016-06-12
      • 1970-01-01
      • 2014-10-22
      • 2014-12-12
      • 2016-03-04
      • 1970-01-01
      • 2022-08-16
      相关资源
      最近更新 更多