0
下面是我的Shiny應用程序的初始界面,該界面使用用戶上傳的圖像並返回用戶指定數量的主要組件的壓縮圖像。從https://ryancquan.com/blog/2014/10/07/image-compression-pca/沒有輸出,具有響應式閃光圖
回收的代碼我得到沒有錯誤,但情節從未出現在mainPanel中。
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()
})
})
})
功能在本地工作,而無需加載特定的庫,但它總是好的,不要認爲。 – vieuphoria
只有在寫入文件時才起作用,而不是重複寫入。我現在明白了! – vieuphoria