r - Shiny:从目的地渲染动态数量的图像
问题描述
我需要从目的地渲染动态数量的图像。但作为通过循环的结果,我只渲染了来自目的地的最后一张图像。但所有图像都必须不同。
我的解决方案受到来自 stackoverflow 的这些答案的启发
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)
解决方案
你能试试这个:
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)
})
}
})
推荐阅读
- bash - 简单循环某个目录makefile中的文件
- java - Json 数组未正确序列化
- html - 用户角色的用户身份验证中是否有任何功能?
- reactjs - 警告:失败的道具类型:道具“主题”在“MuiThemeProviderOld”中被标记为必需,但其值为“未定义”
- python - 获取 Python 3 中超类的类型
- jenkins - 如何让 Jenkins 管道在(任何)代理机器上运行,但从不掌握?
- reactjs - React 媒体播放器多色进度条
- apache-kafka - 如何以自定义方式从主题恢复全局存储?
- typescript - 如何键入装饰器更改类型的装饰属性?
- android - 有什么办法可以创建半圆形作为形状?