【问题标题】:no output with reactive shiny plots没有反应性闪亮图的输出
【发布时间】:2017-01-24 03:59:16
【问题描述】:

这是我对一个 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')
  )
))

服务器.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()
      })
    })
})

【问题讨论】:

  • 函数在本地工作而无需加载特定的库,但最好不要假设。
  • 它仅在写入文件时有效,不适用于绘图。我现在明白了!

标签: r shiny


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2015-06-23
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多