r - Shiny 的 renderUI 只显示最后的输出

标签 r dynamic shiny

我正在尝试使用 renderUIuiOutput 通过 for 循环动态创建一些内容,但每个呈现的元素仅包含 for 循环中最后一次迭代的信息.示例:

require(shiny)
ui <- fluidPage(
  uiOutput("out1"),
  uiOutput("out2")
)

server <- function(input, output, session) {
  count <- 1
  for(a in c("hello", "world")){
    name <- paste0("out", count)
    output[[name]] <- renderUI({
      strong(a)
    })
    count <- count + 1
  }
}
shinyApp(ui = ui, server = server)

这会输出两次 world 而不是 hello world

最佳答案

它在使用 sapply 而不是 for 循环时有效:

require(shiny)

ui <- fluidPage(
  uiOutput("out1"),
  uiOutput("out2")
)

server <- function(input, output, session) {

  vec <- c("hello", "world")

  sapply(seq_along(vec), function(x) {
    name <- paste0("out", x)
    output[[name]] <- renderUI({
      strong(vec[x])
    })
  })

}

shinyApp(ui = ui, server = server)

关于r - Shiny 的 renderUI 只显示最后的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646778/

相关文章:

python - 当决胜局随机时在 python 中排名

r - 添加等倾线和/或方向场以绘制

R - 神经网络集成?

r - 将来自 shiny brushedPoints() 的输入转换为数据帧

r - 如何查看解析后的 R 函数的内容?

python - 如何在 python 中使用 selenium 在动态 href 链接上进行循环?

java - 动态类加载在运行时失败

c# - 动态递归 lambda 表达式

从 Shiny 将数据帧读入单独的函数中

r - Golem在docker中部署:找不到%>%