簡體   English   中英

無輸出,有反應性亮點

[英]no output with reactive shiny plots

這是我對Shiny應用程序的最初陷阱,該應用程序獲取用戶上傳的圖像,並返回包含用戶指定數量的主成分的壓縮圖像的圖。 https://ryancquan.com/blog/2014/10/07/image-compression-pca/回收的代碼

我沒有收到任何錯誤,但該圖從未出現在mainPanel中。

用戶界面

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')
  )
))

服務器

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()
      })
    })
})

除了@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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM