Download and display PDF in Shiny - r

I am trying to show some PDFs from around the web in an app on shinyapps.io. Unfortunately, the standard way of using an iframe with the URL is not an option because of the mixed-content safeguards (the pdfs are served via http). I think that a possible option is to download the pdfs from the url then display them in an iframe from the local file, but I cannot get this to work with tempfile().
A sample app:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("url", "add a url"),
actionButton("button","hit the button"),
h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL")
),
mainPanel(
tabsetPanel(
tabPanel("PDF",
htmlOutput("pdf")
)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$button, {
temp <- tempfile(fileext = ".pdf")
download.file(input$url, temp)
output$pdf <- renderUI({
tags$iframe(src=temp)
})
})
}
shinyApp(ui, server)
Sample pdf: http://www.pdf995.com/samples/pdf.pdf
When I open this in the browser I get an error in the browser console:
Not allowed to load local resource: file:///C:/Users/.../Local/Temp/Rtmp8subWX/file19403a2a2fc8.pdf and nothing in the panel where the iframe is.
A similar attempt uploaded to shinyapps.io failed as well, showing a 404 Not Found error in the pdf viewer.
I think this may be an issue with how shiny/shinyapps.io deal with temp files, but can't quite figure it out. Thanks.

You need to download the PDF in binary mode in a subfolder of your current directory, then call addResourcePath to allow shiny to serve it:
observeEvent(input$button, {
pdf_folder <- "pdf_folder"
if(!file.exists(pdf_folder))
dir.create("pdf_folder")
temp <- tempfile(fileext = ".pdf", tmpdir = "pdf_folder")
download.file(input$url, temp, mode = "wb")
addResourcePath("pdf_folder",pdf_folder)
output$pdf <- renderUI({
tags$iframe(src=temp)
})
})

Related

Make my existent Excel file available for users to download in shiny app

In my app, users should insert some data from an Excel file, but I want to make it possible for them to download one (TesteR.xlsx) in their computer and use it as an example. I have tried to apply this solution but it didn't work, when I click the button it downloads a kinda weird file.
library(shiny)
ui <- fluidPage(
downloadButton("downloadOP", label = "Download")
)
server <- function(input, output){
output$downloadOP <- downloadHandler(
filename = "ph1data",
content = function(file) {
file.copy("www/TesteR.xlsx", file)
}
)
}
shinyApp(ui, server)
I also included the file in a www folder like suggested in the other question, but maybe I am missing something.
Any help would be very much appreciated!
Make sure your www folder is in the same directory as your app.r or server.r/ui.r files. It must be readable by the shiny server.
As you don't state how your app is started/served (from your computer, on a server, what kind of server, using shiny/server, on shinyapps.io, shinyproxy, ...) further advice won't be very useful.
I will also add the file extension to filename = "ph1data.xlsx".
If you add a A tag to your UI, does it work ? (target="self" prevent opening a new tab)
ui <- fluidPage(
downloadButton("downloadOP", label = "Download"),
tags$a("Download", href="TesteR.xlsx", target="self")
)
If you put and image (eg test.jpg) in your www folder and add an IMG tag to your UI, does it show the image ?
ui <- fluidPage(
downloadButton("downloadOP", label = "Download"),
tags$a("Download", href="TesteR.xlsx", target="self", class="btn btn-primary"),
tags$img(src="test.jpg")
)

Rendering A Local Image File in Shiny

I am trying to render a local image file onto the Shiny app interface using a snippet of the example code provided by Shiny website below:
ui <- fluidPage(
imageOutput("plot3")
)
server <- function(input, output, session) {
# Send a pre-rendered image, and don't delete the image after sending it
# NOTE: For this example to work, it would require files in a subdirectory
# named images/
output$plot3 <- renderImage({
filename <- normalizePath(file.path('./images',
paste('image', '2', '.jpeg', sep='')))
# Return a list containing the filename
list(src = filename, alt = "Alternate text")
}, deleteFile = FALSE)
}
shinyApp(ui, server)
It didn't work when I ran the application. However when I added back the interactive() function that was originally in the example code, encase the main code body, I was able to display the local image file without any problems.
if (interactive()) {
ui <- fluidPage(
imageOutput("plot3")
)
server <- function(input, output, session) {...
}
This puzzles me as many of Shiny's tutorials and demonstrations showed rendering can be done without encasing the code body in a scope.
Is there anyone who encounter a similar scenario such as this? Rendering a local image file into a Shiny app?
I just discovered what was wrong with the code through trial and error. The directory which the shiny code app is saved in a different location from the location where the image file is saved. So the solution is either to place a setwd([www image folder location]) or relocate image folder www to where the shiny app resides which it worked - the image is successfully rendered.

Where is the local host directory of a shiny app in R to display local pdf file?

I am Looking at this Question: displaying a pdf from a local drive in shiny and want to Display a local pdf file.
I placed my pdf within C:\Users\user1\Documents\shiny_pdf\www. The app is placed in C:\Users\user1\Documents\shiny_pdf and i set my working Directory to the latter Directory.
Now i am unsure on how to reference that file within the app.
The author of the answer post in the linked Question states:
so you have to save them in your www directory (a local web server)
and access to files with their http(s): URLs (the URL will be
something like http://localhost/.../mypdf.pdf)
So i am unsure on how to navigate from http://localhost/ to C:\Users\user1\Documents\shiny_pdf\www.
What i tried:
I would have assumed i have www is the Server Directory so i would use http://localhost/R-intro.pdf.
I added an Image to my shiny app and checked its server address in the browser. Then i located the pdf file accordingly. I can open it via: http://127.0.0.1:6023/r-intro.pdf (with 6023 being my port number). But i cant use that either to reference it in the iframe.
I also tried list.files(), but that would (obv.) give me the files from the working Directory.
http://localhost/R-intro.pdf also does not work.
The error:
Fehler: Verbindung fehlgeschlagen
Firefox kann keine Verbindung zu dem Server unter localhost aufbauen.
which loosely translates to. Connection failed. Firefox canĀ“t make a Connection to the Server under localhost.
Reproducible Code:
Save the following file (see below) as, e.g. app.R.
Run the following Code to create a WWW directoy for shiny and place a sample pdf into it.
dir.create("www")
pdf(file = "www/r-intro.pdf")
plot(1)
dev.off()
list.files()
Here the Code to save in e.g. app.R.
Code:
library(shiny)
server <- shinyServer(function(input, output, session) {
observe({
print(list.files("http://localhost/R-intro.pdf"))
})
output$pdfviewer <- renderText({
return(paste('<iframe style="height:600px; width:100%" src="', input$pdfurl, '"></iframe>', sep = ""))
})
})
row <- function(...) {
tags$div(class="row", ...)
}
col <- function(width, ...) {
tags$div(class=paste0("span", width), ...)
}
ui <- shinyUI(bootstrapPage(
headerPanel("PDF VIEWER"),
mainPanel(
tags$div(
class = "container",
row(
col(3, textInput("pdfurl", "PDF URL"))
),
row(
col(6, htmlOutput('pdfviewer')),
col(6, tags$iframe(style="height:600px; width:100%", src="https://localhost/www/R-intro.pdf"))
)
)
)
))
shinyApp(ui, server)
Hmm ok, against advice from the post
col(6, tags$iframe(style="height:600px; width:100%", src="R-intro.pdf")),
just referencing the file seems to work.

Choose folder or folder directory inside shiny app

I have a problem using shiny. I want to choose the folder where all the files I want to use in my app are saved either 1) by setting the working directory to that folderpath or 2) by uploading all csv data inside this folder to my app for further processing. for 1) I found the shinyFiles package but it is very very slow -not due to my PC- as well as giving me the error:
Warning: Error in dir.create: invalid 'path' argument
Stack trace (innermost first):
59: dir.create
58: dirCreate
57: observerFunc
2: runApp
1: shinyFilesExample
when I selected a folder and the create folder button becomes clickable and I am putting a name of the new folder into it and clicking on the "+" beneath that panel. Anybody knows why? Despite that this method works but is very very slow. code below:
library(shiny)
library(shinyFiles)
ui<-fluidPage(sidebarLayout(
sidebarPanel(
shinyDirButton("dir", "Chose directory", "Upload")
),
mainPanel(
h4("output$dir"),
verbatimTextOutput("dir"), br()
)
))
server <- function(input,output,session){
# dir
shinyDirChoose(input, 'dir', roots = getVolumes())
dir <- reactive(input$dir)
output$dir <- renderPrint(dir())
}
shinyApp(ui = ui, server = server)
Is there another option? Maybe to upload all csv data via the fileInput function? Or another way? It should not work only locally but on a server so choose.dir might be not the right way.
Many thanks
so far, shinyfiles is the only way to input folders, as far as I know. It cannot work on a server, because browsers are not allowed to select folders (for security reasons).
The zipping way might be the only way to go if you want it to be working on a server (but I have no clue if it can actually be done)
The funktion getwd() gets your current working directory.
server <- function(input,output,session){
# dir
shinyDirChoose(input, 'dir', roots = c(name=getwd()))
dir <- reactive(input$dir)
output$dir <- renderPrint(dir())
}
If there are not too many files in your directory, you can just use fileInput with multiple=T. You can also filter them by extension.
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# ?fileInput
fileInput("file1", "Choose HTML File(s)", multiple=T, accept = ".html")
),
mainPanel(
verbatimTextOutput("out.print")
)
)
)
server <- function(input, output) {
output$out.print <- renderPrint({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "html", "Please upload html file(s)"))
file
})
}
shinyApp(ui, server)
However, if you have too many files, it seems like library(shinyFiles) remains to be your option

downloadButton/ downloadHandler does not recognize filename argument

I've run into a problem with downloadHandler() in Shiny:
If I want to download any file via this function, the filename in the download window is the same as the name of the output-variable (in the example: "downloadData"), but not as it is declared in "filename=" in downloadHandler() (which should be "data-2017-02-13.csv").
Note that the following example is from the downloadHandler() - help page, so I guess there is a general problem with R or RStudio in which I write R scripts.
Additionally when I open the shiny app in a web browser, the problem vanishes.
This partially solves it, but I would still like to know why shiny is behaving differently inside RStudio and a web browser.
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
}
and here the download window I get:
I had the same issue when I used the RStudio preview window and was able to solve this issue by always opening a browser with the command
runApp(launch.browser = TRUE)

Resources