以下是我对Shiny应用程序的初始捕获,该应用程序采用用户上传的图像并返回压缩图像的图表,其中包含用户指定的主要组件数量。代码从https://ryancquan.com/blog/2014/10/07/image-compression-pca/
回收我没有错误,但剧情从未出现在主面板中。
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("PCA compression"),
sidebarPanel(
fileInput('selfie', "SELECT PNG", accept=c('image/png')),
sliderInput("PCAdim", "Number of dimensions to be reduced to:", min=3, max=5, value = 4),
actionButton("exec", "EXECUTE")
),
mainPanel(
imageOutput('Image')
)
))
server.R
library(shiny)
shinyServer(function(input, output, session) {
inFile <- reactive({
inFile <- input$selfie
})
PCAdim <- reactive({
PCAdim <- input$PCAdim
})
ProjectImage <- function(prcomp.obj, pc.num) {
# project image onto n principal components
scores <- prcomp.obj$x
loadings <- prcomp.obj$rotation
img.proj <- scores[, c(1:pc.num)] %*% t(loadings[, c(1:pc.num)])
return(img.proj)
}
SplitImage <- function(rgb.array) {
# decompose image into RGB elements
rgb.list <- list()
for (i in 1:dim(rgb.array)[3]) {
rgb.list[[i]] <- rgb.array[, , i]
}
return(rgb.list)
}
ReduceDimsPNG <- function(png.file, pc.num, display.only=TRUE) {
# reduce dimensions of PNG image
rgb.array <- readPNG(png.file)
rgb.list <- SplitImage(rgb.array)
# apply pca and reproject onto new principal components
rgb.list <- lapply(rgb.list, prcomp, center=FALSE)
rgb.proj <- lapply(rgb.list, ProjectImage, pc.num=pc.num)
# restore original dimensions
restored.img <- abind(rgb.proj, along=3)
}
eventReactive(input$exec, {
output$Image <- renderImage({
outfile <- tempfile(fileext='.png')
writePNG(ReduceDimsPNG(inFile(), PCAdim(), target = outfile))
renderPlot(png(outfile))
dev.off()
})
})
})
答案 0 :(得分:1)
除了@Jota指出的问题外,还有其他一些问题:
fileInput
会返回一个数据框,而不是文件名,因此ReduceDimsPNG(png.file = inFile(), ...)
会生成错误。ReduceDimsPNG(inFile(), PCAdim(), target = outfile))
renderImage
应返回包含文件名的列表,例如list(src = outfile, contentType = 'image/png', ...)
以下单个文件Shiny应用程序可以解决上述问题,可以在我的机器上运行:
ui <- pageWithSidebar(
headerPanel("PCA compression"),
sidebarPanel(
fileInput('selfie', "SELECT PNG", accept=c('image/png')),
sliderInput("PCAdim", "Number of dimensions to be reduced to:", min=3, max=5, value = 4),
actionButton("exec", "EXECUTE")
),
mainPanel(
imageOutput('Image')
)
)
server <- function(input, output, session) {
inFile <- reactive({
inFile <- input$selfie
})
PCAdim <- reactive({
PCAdim <- input$PCAdim
})
ProjectImage <- function(prcomp.obj, pc.num) {
# project image onto n principal components
scores <- prcomp.obj$x
loadings <- prcomp.obj$rotation
img.proj <- scores[, c(1:pc.num)] %*% t(loadings[, c(1:pc.num)])
return(img.proj)
}
SplitImage <- function(rgb.array) {
# decompose image into RGB elements
rgb.list <- list()
for (i in 1:dim(rgb.array)[3]) {
rgb.list[[i]] <- rgb.array[, , i]
}
return(rgb.list)
}
ReduceDimsPNG <- function(png.file, pc.num, display.only=TRUE) {
# reduce dimensions of PNG image
rgb.array <- png::readPNG(png.file)
rgb.list <- SplitImage(rgb.array)
# apply pca and reproject onto new principal components
rgb.list <- lapply(rgb.list, prcomp, center=FALSE)
rgb.proj <- lapply(rgb.list, ProjectImage, pc.num=pc.num)
# restore original dimensions
restored.img <- abind::abind(rgb.proj, along=3)
}
img.array <- eventReactive(input$exec, {
ReduceDimsPNG(inFile()$datapath[1], PCAdim())
})
output$Image <- renderImage({
outfile <- tempfile(fileext='.png')
png::writePNG(img.array(), target = outfile)
list(src = outfile, contentType = 'image/png')}, deleteFile = TRUE
)
}
shinyApp(ui = ui, server = server)