Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:快速反应图像显示_R_Image_Shiny_Reactive Programming - Fatal编程技术网

R:快速反应图像显示

R:快速反应图像显示,r,image,shiny,reactive-programming,R,Image,Shiny,Reactive Programming,我正在尝试在我的闪亮应用程序中显示图像。我已在服务器.R的脚本中成功地完成了这项工作: output$display.image <- renderImage({ image_file <- paste("www/",input$image.type,".jpeg",sep="") return(list( src = image_file, filetype = "image/jpeg", height = 520,

我正在尝试在我的闪亮应用程序中显示图像。我已在服务器.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)

为什么会有这样的差异?有没有办法使反应图像显示得更快?

您好,您可以使用
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\u type
,因为
在Javascript中有特殊含义(介于
input
image\u 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"))
           )
         })
)
例如,对于来自此by的图像(您也可以在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)
库(“闪亮”)

ui当您从反应式服务器发送图像URL时,您正在通过JavaScript从服务器传输到客户端。这些数据需要由客户端解包并用于转换文档,这一切都需要时间。你的要求是什么?图像是否需要动态加载?是否有一组有限的图像可以加载?@sdgfsdh需要根据用户输入(input$image.type)动态加载图像,是的,有一组有限的图像可以加载。
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)