vieuphoria vieuphoria - 2 months ago 11
R Question

no output with reactive shiny plots

Here are my initial trappings for a Shiny app that takes a user-uploaded image and returns a plot of the compressed image with the user-specified number of principal components. Code recycled from https://ryancquan.com/blog/2014/10/07/image-compression-pca/

I get no error but the plot never appears in 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()
})
})
})

Answer

In addition to the problems pointed out by @Jota, there are a couple of other problems:

  • fileInput returns a dataframe, not a file name, so ReduceDimsPNG(png.file = inFile(), ...) will generate an error.
  • Wrongly placed parentheses in ReduceDimsPNG(inFile(), PCAdim(), target = outfile))
  • renderImage should return a list containing the filename, e.g. list(src = outfile, contentType = 'image/png', ...)

The following single-file Shiny app, which corrects the problems above, works on my machine:

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)