2017-06-27 54 views
0

我試圖在R閃亮應用程序中從列表中反應顯示圖像。R Shiny Reactively顯示列表中的圖像

我有很多.tiff圖像存儲在我的應用程序的「www」目錄。他們遵循的命名約定OHC_130.tiff,OHC_131.tiff,OHC_132.tiff,IHC_133.tiff,Deiter_134.tiff,OHC_135.tiff等等

我也有包含所有這些名字的矢量

ImgID_Vector <- as.vector(c("OHC_130", "OHC_131", "OHC_132", "IHC_133", "Deiter_134", "OHC_135")

我想做一個可選擇的輸入下拉列表,這樣用戶可以選擇圖像然後點擊提交按鈕使圖像顯示在下面。我已經爲ui.r設置了這個,但我不確定如何使它在服務器端工作。

#ui.r 
library(shiny) 
library(shinydashboard) 

dashboardBody(
    tabItems(
    tabItem(tabName = "dt", 
      h2("Select an image"), 
      fluidRow(
      box(title="This is a searchable database of images", solidHeader = TRUE,status = "primary"), 
      selectInput("input$ImageIDVariable1", label = h4("Enter your image of interest"), choices = (ImgID_Vector), multiple = TRUE), 
      submitButton("Submit"), 
      imageOutput("ImageID_Image") 
     ) 
) 
) 

概念我知道,在服務器端,我需要用戶輸入從UI端連接到實際圖像中「www」的文件夾。我應該能夠使用無功輸入和renderImage來做到這一點,我認爲。但我不知道如何編寫render image命令來實現所需的結果。

#server.r 

#This is the data that contains the choices for the dropdown menu 
ImgID_Vector <- readRDS("ImgID_Vector.RDS") 

shinyServer(function(input, output) { 
# This is where I am struggling, with the render image command 
output$ImageID_Image <- renderImage({ 
    filename <- normalizePath(file.path('./www', 
          paste(input$ImageIDVariable1, '.tiff', sep=''))) 
    list(src = filename) 
    }, deleteFile = FALSE) 
} 

#This is where I have the reactive input variable 
ImageIDVariable1 <- reactive({input$ImageIDVariable1}) 

}) 

感謝您的幫助!

回答

0

嗨參數inputId從您的selectInput是錯的,應該是"ImageIDVariable1",而不是input$ImageIDVariable1

在ui.R:

selectInput(inputId = "ImageIDVariable1", label = h4("Enter your image of interest") 

在server.R

input$ImageIDVariable1 

此外:

你應該在一個名爲global.R或腳本至少ui.R使用:

ImgID_Vector <- readRDS("ImgID_Vector.RDS") 

而且您不應該使用multiple = TRUE,因爲renderImage一次只能渲染一個圖像。

你應該把默認選擇的選項,如果不是renderImage將搜索不存在的圖像。