I want to upload images and text in shiny web (not inset image in code) , and then download as PDF document.
I am get stuck in download images into PDF document.
In "output$report <- downloadHandler(...)", params cannot be "observe" or "output$image". How to write the right params for the images?
library(shiny)
ui<-navbarPage("Report",
tabPanel("Upload Images", uiOutput('page1')),
tabPanel("Input Text", uiOutput('page2')),
tabPanel("Download Report", uiOutput('page3'))
)
server <- function(input, output, session) {
output$page1 <- renderUI({
fluidPage(
fluidRow(
column(5,
fileInput(inputId = 'files',
label = 'Select 1st Image',
multiple = TRUE,
accept=c('image/png', 'image/jpeg'),
width = '400px')
))) })
output$page2 <- renderUI({
fluidPage(
fluidRow(
column(8,
textInput("Text1", "(1)", " ",width = '600px')
#verbatimTextOutput("Value1")
),
column(4, uiOutput('Image1'))
))
})
files <- reactive({
files <- input$files
files$datapath <- gsub("\\\\", "/", files$datapath)
files
})
output$Image1 <- renderUI({
if(is.null(input$files)) return(NULL)
image_output_list <-
lapply(1:nrow(files()),
function(i)
{
imagename = paste0("image", i)
imageOutput(imagename)
})
do.call(tagList, image_output_list)
})
IMAGE1 <- observe({
if(is.null(input$files)) return(NULL)
for (i in 1:nrow(files()))
{
print(i)
local({
my_i <- i
imagename = paste0("image", my_i)
print(imagename)
output[[imagename]] <-
renderImage({
list(src = files()$datapath[my_i],
width = 250,
height = 250,
alt = "Image failed to render")
}, deleteFile = FALSE)
})
}
}) ######!!!! Parms cannot be observe or output$Image1
output$page3 <- renderUI({ downloadButton("report", "Generate report")})
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
tempReport <- file.path(tempdir(), "VIWpdf.Rmd")
file.copy("VIWpdf.Rmd", tempReport, overwrite = TRUE)
params <- list(
Text1 = input$Text1,
Image1 = IMAGE1 ######!!!!!Here this the Problem######
)
out<- rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv()))
file.rename(out, file)
}
)}
shinyApp(ui=ui,server=server)
Here is the .rmd
---
title: "Report"
date: "`r format(Sys.time(), '%d %B, %Y')`"
always_allow_html: yes
output:
pdf_document:
fig_caption: yes
keep_tex: yes
toc: true
toc_depth: 2
params:
Text1: 'NULL'
Image1: 'NULL'
---
(1) `r params$Text1`
`r params$Image1`
I expect the output of image can show in the Rmarkdown PDF, but the actual output is empty.