我需要从目的地渲染动态数量的图像。但是作为循环的结果,我仅渲染了来自目标的最后一张图像几次。但是所有图像都必须不同。
我的解决方案受到stackoverflow的这些答案的启发
Shiny: Dynamic Number of Output Elements/Plots
dynamically add plots to web page using shiny
library(shiny)
# get all files from destination
images <- list.files("charts")
image_names <- str_replace_all(images, ".png", "")
server <- shinyServer(function(input, output) {
output$images <- renderUI({
image_output_list <-
lapply(1:length(image_names),
function(i)
{
imagename = paste0(image_names[i], "_image")
imageOutput(imagename)
})
do.call(tagList, image_output_list)
})
observe({
# if(is.null(input$files)) return(NULL)
for (i in 1:length(image_names))
{
print(i)
local({
imagename <- paste0(image_names[i], "_image")
print(imagename)
output[[imagename]] <-
renderImage({
list(src = normalizePath(paste0('charts/', image_names[i], '.png')))
}, deleteFile = FALSE)
})
}
})
})
ui <- shinyUI(fluidPage(
titlePanel("Sidebar"),
sidebarLayout(
sidebarPanel(),
mainPanel(
uiOutput('images')
)
)
))
shinyApp(ui=ui,server=server)
答案 0 :(得分:1)
您能尝试一下吗?
observe({
for (i in 1:length(image_names))
{
local({
ii <- i
imagename <- paste0(image_names[ii], "_image")
print(imagename)
output[[imagename]] <-
renderImage({
list(src = normalizePath(paste0('charts/', image_names[ii], '.png')))
}, deleteFile = FALSE)
})
}
})