用于 renderImage 的闪亮环境

Shiny environment to use for renderImage

在 shiny 中,我正在构建一个框列表,每个框内都有一个 renderImage,如下所示:

images = ["i1.png", "i2.png", "i3.png"]
for(i in 1:3){
  print(i)
  q_list[[i]] = box(
    width = 12,
    status = "primary",
    renderImage({
      print(i)
      img_path = images[i]
      print(img_path)
      list(src = img_path, alt = "This is alternate text")},
      deleteFile = F)
  )
}

不幸的是,这些框似乎没有在循环中注册 i。在上面,它打印的第一个 i 是正确的 (1-2-3)。但是第二个 i,里面的 renderImage 打印为 3 而 img_pathi3.png。我是否需要将另一个环境放入 renderImage 才能正常工作?

这里的评论中要求的是一个最小的例子。仪表板在 3 个框中显示相同的图表 (i3.png):

rm(list = ls(all.names = TRUE))
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(shinybusy)
library(shinyjs)
options(shiny.error = browser)
options(shiny.fullstacktrace = TRUE)
options(shiny.trace = TRUE)

images = c("i1.png", "i2.png", "i3.png")

ui = dashboardPage(
  sidebar = dashboardSidebar(disable = T),
  body = dashboardBody(uiOutput("module_body")),
  header = dashboardHeader(disable = T)
)

server = function(input, output, session){
  imgs_fct = function(){
    im_list = list()
    for(i in 1:3){
      print(i) # prints correctly
      img_path = images[i] # prints correctly
      print(img_path)
      # store each box in im_list
      im_list[[i]] = box(
        width = 12,
        status = "primary",
        renderImage({
          print(img_path) # prints "i3.png"
          list(src = img_path, alt = "This is alternate text")}, deleteFile = F)
      )
    }
    return(im_list)
  }

  output$module_body = renderUI({
    theitems = tabItem(tabName = "xxx", fluidRow(column(width = 8, offset = 2, id = "form", imgs_fct())))}
  )
}

# run the app
shinyApp(ui = ui, server = server)

如评论中所建议,如果您在 imgs_fct 函数中使用 lapply,这将按预期工作,每个图都按预期呈现,而不仅仅是所有图的最终图.请注意 images 是图像文件路径的向量,因此假设它们与您的 Shiny 应用程序代码位于同一目录中:

  imgs_fct = function(){
    lapply(images, 
           function(x) {
             box(
               width = 12,
               status = "primary",
               renderImage({
                 list(src = x, alt = "This is alternate text")}, deleteFile = F)
             )
           })
    }

此方法起作用而 for 循环不起作用的原因是因为您用于迭代的计数器(此处 i)在所有 renderImage 调用中共享,因为它们'在相同的环境中重新评估(因为它们在相同的循环中)。发生的事情是,计算运行,然后当 Shiny 尝试在此过程结束时解决任何反应性依赖项时,所有 render 函数共享相同的引用 i,此时等于3,所以所有渲染都使用相同的图像。

相比之下,对于 lapply,每个 renderImage 都在一个单独的函数调用中,因此在单独的环境中进行评估,其中每个函数都有一个不同的图像文件路径作为参数。因此,它们会按照您的预期单独呈现。您可以将 for 循环的内容包装在 local({}) 中,但我个人喜欢 lapply 方法,因为它看起来更 'R'-y.