【问题标题】:Can I create a bunch of reactive input-output pairs from a list in Shiny?我可以从 Shiny 的列表中创建一堆反应式输入-输出对吗?
【发布时间】:2021-01-17 04:37:54
【问题描述】:

在 Shiny 中,是否可以使用函数从字符串的向量/列表创建多个反应式输入-输出对?

比如

id_list <- c("ID_1", "ID_2", "ID_3")

server <- function(input, output) {

lapply(id_list, function(x) quote(output$as.character(x) <- renderText({ input$x })))

}

结果实际上是

output$ID_1 <- renderText({ input$ID_1 })
output$ID_2 <- renderText({ input$ID_2 })
output$ID_2 <- renderText({ input$ID_2 })

等等。对于列表中的所有项目,每个项目都在服务器函数中以反应方式工作。

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    server <- function(input, output) {
     
      for(id in id_list){
        local({
          localId <- id
          output[[localId]] <- renderText({
            input[[localId]]
          })
        })
      } 
    
    }
    

    【讨论】:

    • 所以不能在像 lapply 这样的函数内部完成?
    • @eartoolbox 你可以试试lapply(id_list, function(id){output[[id]] &lt;- renderText({input[[id]]})}).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多