我正在尝试使用 renderUI
和 uiOutput
通过 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/