2017-08-24 42 views
0

我想創建一個閃亮的儀表板應用程序來使用EBImage進行圖像分析。我的疑問是如何使用EBImage包將本地圖像加載到應用程序中進行後驗分析。如何使用EBImage將本地圖像加載到閃亮的應用程序進行圖像分析

在網上我看到如何加載從EBImage封裝的系統文件相似圖片例如:

library(shiny) 
library(EBImage) 
library(displayWidget) 

# Define UI for application that draws a histogram 
ui <- fluidPage(

    # Application title 
    titlePanel("Display widget demo"), 

    # Sidebar with a select input for the image 
    sidebarLayout(
    sidebarPanel(
     selectInput("image", "Sample image:", list.files(system.file("images", package="EBImage"))) 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     tabsetPanel(
     tabPanel("Static display", plotOutput("display")), 
     tabPanel("Interactive widget", displayWidgetOutput("widget")) 
    ) 
    ) 
) 
) 

server <- function(input, output) { 

    img <- reactive({ 
    f = system.file("images", input$image, package="EBImage") 
    x = readImage(f) 
    }) 

    output$widget <- renderDisplayWidget({ 
    displayWidget(img()) 
    }) 

    output$display <- renderPlot({ 
    display(img(), method="raster", all=TRUE) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 

我知道如何使用這個加載本地數據:

DataXLSX <- reactive({ 
    inFile <- input$fileXLSX 

    if (is.null(inFile)) 
     return(NULL) 

    loadXLSX <- read.xlsx(inFile$datapath) 
    loadXLSX  
    }) 

但我不能做與readImage()一樣。一些幫助?感謝

回答

0

要啓用文件上傳在您的應用程序,你需要更換selectInput("image", ...)部件由一個fileInput控制從樣本文件的預定義列表選擇圖像,並使用無功表達img它的值來訪問本地副本上傳的文件。您還需要通過致電req(img())來保護renderDisplayWidgetrenderPlot中的表達式的執行,以便延遲圖像顯示直到選擇並加載圖像文件。請參閱下面的完整工作示例。 請注意,displayWidget已經融入EBImage devel分支通過R包提供的功能,所以裝載displayWidget是過時,因爲EBImage版本4.19.3的。

library("shiny") 
library("EBImage")# >= 4.19.3 

ui <- fluidPage(

    # Application title 
    titlePanel("Image display"), 

    # Sidebar with a select input for the image 
    sidebarLayout(
    sidebarPanel(
     fileInput("image", "Select image") 
    ), 

    # Show a plot of the generated distribution 
    mainPanel(
     tabsetPanel(
     tabPanel("Static raster", plotOutput("raster")), 
     tabPanel("Interactive browser", displayOutput("widget")) 
    ) 
    ) 
) 

) 

server <- function(input, output) { 

    img <- reactive({ 
    f <- input$image 
    if (is.null(f)) 
     return(NULL)   
    readImage(f$datapath) 
    }) 

    output$widget <- renderDisplay({ 
    req(img()) 
    display(img()) 
    }) 

    output$raster <- renderPlot({ 
    req(img()) 
    plot(img(), all=TRUE) 
    }) 

} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

非常感謝!昨晚我可以加載本地圖像,但沒有req()函數。再次感謝 – Archymedes

相關問題