我想创建一个闪亮的仪表板应用程序,以使用EBImage进行图像分析。我的疑问是如何使用EBImage包将本地图像加载到应用程序中以进行后验分析。
在互联网上,我看到了如何从EBImage-package的系统文件中加载图像,如下例所示:
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()做同样的事情。一些帮助?谢谢
要在您的应用中启用文件上传,您需要用selectInput("image", ...)
控件替换用于从示例文件的预定义列表中选择图像的小部件fileInput
,并在反应表达式中使用其值img
来访问上传文件的本地副本。您还需要防范内部的表达式的执行renderDisplayWidget
,并renderPlot
通过调用req(img())
以延迟的图像显示,直到图像文件被选中并加载。有关完整的工作示例,请参见下文。请注意,R包displayWidget提供的功能已集成到EBImage devel分支中,因此从EBImage 4.19.3版本开始不再加载displayWidget。
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句