简体   繁体   English

r shiny - 使用 imageOutput() 和 renderImage() 绘制多个图像

[英]r shiny - Plotting multiple images using the imageOutput() and renderImage()

I am trying to plot several images saved in the www sub-directory folder of my shiny app folder.我正在尝试将 plot 几张图像保存在我的 shiny 应用程序文件夹的 www 子目录文件夹中。 The image file names are in a data frame column;图像文件名在数据框列中; let's say “img_path”.让我们说“img_path”。

I am using the imageOutput() function in the UI and renderImage() in the server interface.我在 UI 中使用imageOutput() function,在服务器界面中使用renderImage() Since I want to plot all the images in the www subfolder and that are referenced in the data frame, I am using a for loop.由于我想要 plot www子文件夹中的所有图像以及数据框中引用的所有图像,因此我使用了 for 循环。

Unfortunately, instead of rendering all the images, it always displays the last image.不幸的是,它不会渲染所有图像,而是始终显示最后一张图像。 I guess this is happening because images are being overlayed on top of each other.我猜这是因为图像正在彼此叠加。

Let say that I have: my data假设我有:我的数据

df_img <- data.frame(id = c(1:5), img_path = c("h1000.png", "h2000.png", "h3000.png", "h4000.png", "h000.png"))

which is stored in the data subfolder;它存储在数据子文件夹中; the 5 images in the www subfolder are named as in the df_img[["img_path"]] . www 子文件夹中的 5 张图像在df_img[["img_path"]]中命名。 My basic shiny app code is:我的基本 shiny 应用程序代码是:

library(shiny)
library(shinydashboard)

Define UI定义用户界面

ui <- fluidPage(
    # Application title
    titlePanel("Test app"),
    # to render images in the www folder 
    box(imageOutput("houz"), width = 3)
)

Define server logic定义服务器逻辑

server <- function(input, output) {
    df_img <- read.csv("data/df_img.csv", header = T)
    for (i in 1:nrow(df_img)) {
        output$houz <- renderImage({
            list(
                src = file.path('www', df_img$img_path[i]),
                contentType = "image/jpeg",
                width = "100%", height = "45%"
            )
        }, deleteFile = FALSE)
        
    }
}

# Run the application 
shinyApp(ui = ui, server = server)

what_i_expect and what_i_get what_i_expect 和 what_i_get

Consider using Shiny modules.考虑使用 Shiny 模块。 A working example is below, which assumes you have images with a "jpeg" extension in a "www" subdirectory of the working directory.下面是一个工作示例,假设您在工作目录的“www”子目录中有扩展名为“jpeg”的图像。 I use purrr for functional programming - you could use lapply() or a for loop if you prefer.我将purrr用于函数式编程 - 如果您愿意,可以使用lapply()for循环。

Chapter 19 of Mastering Shiny is a good introduction to Shiny modules.精通Shiny的第19章很好的介绍了Shiny模块。

library(shiny)
library(purrr)

ui_module <- function(id) {
  imageOutput(NS(id, "img"))
}

server_module <- function(id,
                          img_path) {
  moduleServer(
    id,
    function(input, output, session) {
  output$img <- renderImage({
    list(src = img_path,
         contentType = "image/jpeg",
         width = "100%",
         height = "45%")
  },
  deleteFile = FALSE)
    })
}

images <- list.files(path = "www",
                     pattern = "jpeg",
                     full.names = TRUE)

ids <- tools::file_path_sans_ext(
  basename(images)
)

ui <- fluidPage(
  map(ids, ui_module)
  )

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

  map2(.x = ids,
       .y = images,
       .f = server_module)

}

shinyApp(ui, server)

You can use renderUI to display the list of images you wish to display.您可以使用renderUI来显示您希望显示的图像列表。 Try this尝试这个

df_img <- data.frame(id = c(1:5), img_path = c("h1000.png", "h2000.png", "h3000.png", "h4000.png", "h000.png"))

ui <- fluidPage(
  # Application title
  titlePanel("Test app"),
  # to render images in the www folder 
  box(uiOutput("houz"), width = 3)
)

server <- function(input, output) {
  #df_img <- read.csv("data/df_img.csv", header = T)
  
  n <- nrow(df_img)
  
  observe({
    for (i in 1:n)
    {
      print(i)
      local({
        my_i <- i
        imagename = paste0("img", my_i)
        print(imagename)
        output[[imagename]] <-
          renderImage({
            list(src = file.path('www', df_img$img_path[my_i]), 
                 width = "100%", height = "55%",
                 alt = "Image failed to render")
          }, deleteFile = FALSE)
      })
    }
  })
  
  
  output$houz <- renderUI({
    
    image_output_list <- 
      lapply(1:n,
             function(i)
             {
               imagename = paste0("img", i)
               imageOutput(imagename)
             })
    
    do.call(tagList, image_output_list)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

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

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