简体   繁体   English

闪亮:从目的地渲染动态数量的图像

[英]Shiny: Render dynamic number of images from destination

I need to render dynamic number of images from destination.我需要从目的地渲染动态数量的图像。 But as the result through the loop I render several times only the last image from destination.但作为循环的结果,我只渲染了目的地的最后一张图像。 But all images must be different.但所有图像都必须不同。

My solution is inspired by these answers from stackoverflow我的解决方案的灵感来自于 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)

Could you try this:你可以试试这个:

  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)
      })
    }
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM