Home > Blockchain >  Upload an image to R shiny downloadable to pdf
Upload an image to R shiny downloadable to pdf

Time:11-09

I am trying to upload an image to R shiny and have the image download into a report app.r But when I knitt the report it gives me the image data not the image

Any ideas on what I am doing wrong here? As always thank you in advance

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)

report.Rmd

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

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

CodePudding user response:

The issue is that RenderImage will delete the file after usage unless deleteFile = False. This is the line to change, Remove this:

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

Add this:

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