首页 > 解决方案 > 将图像上传到 R 闪亮可下载到 pdf

问题描述

我正在尝试将图像上传到 R Shiny 并将图像下载到报告 app.r 但是当我编织报告时,它给了我图像数据而不是图像

关于我在这里做错了什么的任何想法?一如既往地提前谢谢你

library(shiny)

ui <- fluidPage(
    titlePanel('Upload Image Test'),
    sidebarLayout(  
       sidebarPanel(
           fileInput(
                inputId = "file1",
                label = "Upload Image",
                accept = c('image/png', 'image/jpeg','image/jpg')
            ),
            tags$hr()),
        mainPanel(
            textOutput("filename"),
            imageOutput(outputId = "uploaded_image"),
            downloadButton("report", "Generate report")
        )
    )
)

 

server <- function(input, output) {
    re1 <- reactive({gsub("\\\\", "/", input$file1$datapath)})
output$uploaded_image <- renderImage({list(src = re1())})
    output$report <- downloadHandler(
      filename = "report.pdf",
      content = function(file) {
        tempReport <- file.path(tempdir(), "report.Rmd")
        file.copy("report.Rmd", tempReport, overwrite = TRUE)
        params <- list(n = input$slider, image = input$file1)
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
}
shinyApp(ui, server)

报告.Rmd

---
title: "report"
author: "Author"
date: "4/14/2019"
output: pdf_document
params:
  image: "Null"
  n: "Null"
---

```{r}
params$image
params$n

标签: rpdfshinyr-markdown

解决方案


问题是 RenderImage 将在使用后删除文件,除非 deleteFile = False。这是要更改的行,删除此行:

 re1 <- reactive({gsub("\\\\", "/", input$file1$datapath)})

添加这个:

output$uploaded_image <- renderImage({list(src = input$file1$datapath)}, deleteFile = FALSE)

推荐阅读