2016-09-16 50 views
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() 
     }) 
    }) 
}) 
+0

功能在本地工作,而無需加載特定的庫,但它總是好的,不要認爲。 – vieuphoria

+0

只有在寫入文件時才起作用,而不是重複寫入。我現在明白了! – vieuphoria

回答

1

除了問題所指出的@Jota,有一對夫婦的其他問題:

  • fileInput返回一個數據幀,而不是文件名,所以ReduceDimsPNG(png.file = inFile(), ...)將產生一個錯誤。
  • 錯誤放置圓括號ReduceDimsPNG(inFile(), PCAdim(), target = outfile))
  • renderImage應返回包含文件名的列表,例如, list(src = outfile, contentType = 'image/png', ...)

下單文件閃亮的應用程序,它糾正上述問題,作品在我的機器上:

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) 
+0

謝謝!我有很多要學習的優雅。 – vieuphoria