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
- Create a
www
folder under your Shiny project root - Point the file argument of
msaPrettyPrint
towww/myreport.pdf
- Dynamically add an iframe to display the file. Note in the iframe you point to
myreport.pdf
directly withoutwww
, as Shiny automatically looks for static/media files inside thewww
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
Add Max Value to a New Column in R
How to Override a Non-Visible Function in the Package Namespace
Subset Based on Variable Column Name
Struggling with Integers (Maximum Integer Size)
R - Emulate the Default Behavior of Hist() with Ggplot2 for Bin Width
How to Fit a Smooth Curve to My Data in R
Saving Multiple Ggplots from Ls into One and Separate Files in R
Generate Markdown Comments Within for Loop
Extract Last Word in String in R
How to Prevent Rbind() from Geting Really Slow as Dataframe Grows Larger
What Leads the First Element of a Printed List to Be Enclosed with Backticks in R V3.5.1
Dplyr::Group_By_ with Character String Input of Several Variable Names
Displaying a PDF from a Local Drive in Shiny
Cumulative Count of Each Value
R Package Lattice Won't Plot If Run Using Source()
Remove Grid, Background Color, and Top and Right Borders from Ggplot2