Displaying a PDF from a Local Drive in Shiny

R Shiny how to display a pdf file generated in downloadHandler

If you intend to display the pdf, you should not use downloadHandler. Instead, just use your pdf printing function to generate the pdf file, but the key is

  1. Create a www folder under your Shiny project root
  2. Point the file argument of msaPrettyPrint to www/myreport.pdf
  3. Dynamically add an iframe to display the file. Note in the iframe you point to myreport.pdf directly without www, as Shiny automatically looks for static/media files inside the www folder.

See below for a working example (note I am not using the msa package here but the idea should be the same).

library(shiny)

ui <- shinyUI(fluidPage(

titlePanel("Old Faithful Geyser Data"),

sidebarLayout(
sidebarPanel(
actionButton("generate", "Generate PDF")
),

mainPanel(
uiOutput("pdfview")
)
)
))

server <- shinyServer(function(input, output) {

observeEvent(input$generate, {
output$pdfview <- renderUI({
pdf("www/myreport.pdf")
hist(rnorm(100))
dev.off()
tags$iframe(style="height:600px; width:100%", src="myreport.pdf")
})
})
})

shinyApp(ui = ui, server = server)

R shiny uploading a pdf from local drive does not work

You have two options. The first one: just put your file example.pdf on a /www directory where your app file is. The second: use the addResourcePath function before running your app to make a local directory accessible.

addResourcePath("pdfs", "c:/temp/mypdfs")

later use it as

src="pdfs/example.pdf"

r shiny pdf display is blank

The following works fine.

library(shiny)

ui <- shinyUI(fluidPage(

titlePanel("Testing File upload"),

sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
),

mainPanel(
uiOutput("pdfview")
)
)
))

server <- shinyServer(function(input, output) {

observe({
req(input$file_input)

file.copy(input$file_input$datapath,"www", overwrite = T)

output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="0.pdf")
})

})

})

shinyApp(ui = ui, server = server)

Please ensure that you have a www folder and restart RStudio. It should work locally in a browser. Then try to deploy it and test it - after ensuring that there is a www folder under the folder where you keep your app.R.



Related Topics



Leave a reply



Submit