R Shiny:快速反应图像显示

mai*_*aia 10 r image reactive-programming shiny

我正试图反应性地在我闪亮的应用程序中显示图像.我已经在server.R脚本中成功完成了以下操作:

output$display.image <- renderImage({

    image_file <- paste("www/",input$image.type,".jpeg",sep="")

    return(list(
      src = image_file,
      filetype = "image/jpeg",
      height = 520,
      width = 696
    ))

  }, deleteFile = FALSE)
Run Code Online (Sandbox Code Playgroud)

但它很慢.

但是,将其中一个图像嵌入到ui.R脚本中是非常快的,如下所示:

tabPanel("Live Images", img(src = "img_type1.jpeg"))
Run Code Online (Sandbox Code Playgroud)

为什么会有这样的差异?有没有办法让反应图像看起来更快?

Vic*_*orp 5

您好,您可以用来执行此操作,它会嵌入您的所有图像,但只会显示符合条件的conditionalPanel图像:TRUE

tabPanel("Live Images", 
     conditionalPanel(condition = "input.image_type == 'img_type1'",
                      img(src = "img_type1.jpeg")
     ),
     conditionalPanel(condition = "input.image_type == 'img_type2'",
                      img(src = "img_type2.jpeg")
     )
)
Run Code Online (Sandbox Code Playgroud)

并将输入的名称从 更改为 ,image.type因为image_type.Javascript 中具有特殊含义(如input和之间image_type)。

如果你有很多图像,你总是可以这样做:

tabPanel("Live Images", 
         lapply(X = seq_len(10), FUN = function(i) {
           conditionalPanel(condition = paste0("input.image_type == 'img_type", i, "'"),
                            img(src = paste0("img_type", i, ".jpeg"))
           )
         })
)
Run Code Online (Sandbox Code Playgroud)

例如,使用 tsperry 这篇文章中的图像(可以在 rbloggers 上找到它),您可以执行以下操作:

library("shiny")
ui <- fluidPage(
  tabsetPanel(
    tabPanel("Live Images", 
         # 50 images to display
         lapply(X = seq_len(50), FUN = function(i) {
           # condition on the slider value
           conditionalPanel(condition = paste0("input.slider == ", i),
                            # images are on github
                            img(src = paste0("https://raw.githubusercontent.com/pvictor/images/master/",
                                             sprintf("%04d", i), "plot.png"))
           )
         }),
         sliderInput(inputId = "slider", label = "Value", min = 1, max = 50, value = 1, 
                     animate = animationOptions(interval = 100, loop = TRUE))
    )
  )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)