我正在尝试在我 Shiny 的应用程序中 react 性地显示图像。我已经在 中成功地做到了这一点。 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"))
为什么会有这样的差异?有什么方法可以使 react 图像显示得更快?
最佳答案
您好,您可以使用 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 中具有特殊含义(在 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"))
)
})
)
例如,来自此 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)
关于R Shiny : fast reactive image display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637112/