R Shiny:快速反应图像显示

时间:2015-04-14 20:47:20

标签: r image shiny reactive-programming

我试图在我闪亮的应用中反复显示图像。我已在 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)

非常慢

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

tabPanel("Live Images", img(src = "img_type1.jpeg"))

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

1 个答案:

答案 0 :(得分:4)

您好,您可以使用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")
     )
)

并将输入的名称从image.type更改为image_type,因为.在Javascript中具有特殊含义(在inputimage_type之间)。< / p>

如果你有很多图片,你可以随时做这样的事情:

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

例如,对于post 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)