I don't really know where to begin with this - I have a file that is akin to an appendix that I want made available to my shiny app users. Is possible to embed a PDF from my local drive in to my shiny app & if so is there an ability to have the pdf icon built in? Meaning when you click the pdf icon, you'd download the file specified in the code.
Any help is appreciated!!
Assuming you have a pdf icon file pdficon.png and a pdf file mypdf.pdf, put them in the subfolder www. Then in your app:
library(shiny)
ui <- fluidPage(
tags$a(tags$img(src="pdficon.png"), href="mypdf.pdf", download="pdfname.pdf")
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Then clicking on the icon will download the pdf file under the new name pdfname.pdf.
Related
To view a pdf file and my shiny app, I use the following code on the server side, which everyone recommends:
output$pdf_info_exper <- renderUI({
selected_Exper = input$select_data
pdf_path = get_pdfExper (selected_Exper,base_dir='.')
tags$iframe(style="height:600px; width:100%", src= pdf_path, type="application/pdf")
#tags$embed( src=pdf_path, style="height:600px; width:100%", type="application/pdf")
})
The problem is that when I do it, it skips my app adobe reader directly and I haven't embedded it in my app.
when run the app in the browser if the pdf is embedded in the app, I understand,I would like to know how to embed the pdf viewer in my app without the adoba/reader jumping...
I accept any suggestion, thanks....
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")
)
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.
How to load PDF file in same window of R Shiny application?
Here is an example:
library(shiny)
ui <- fluidPage(
navbarPage("Demo",
tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
tabPanel("PDF file", fluidRow(uiOutput("load_pdf_file")))))
server <- function(input, output) {
# 1. Load PDF file
output$load_pdf_file <- renderUI({
# 1.1. This loads pdf file in a 'new' window
browseURL("http://www.africau.edu/images/default/sample.pdf")
# 1.2. How to load pdf file in the 'same' window where R Shiny app works
# Expect to see pdf file on the page in 'PDF file' section
# ...
})
}
shinyApp(ui = ui, server = server)
The method that I know of, does not rely on serving up PDF's it uses the standard HTML methods one would use in a traditional webpage, with the shiny code js wrappers to place it in an iFrame.
With this method, in the tabPanel you set up an iFrame and give it some dimension ( I picked arbitrary ones) the decide if it scrolls or not, the use src to supply it with the path to your pdf file. The condition of course is that your PDF file is discoverable by some kind of a local path or URL to the outside world.
library(shiny)
ui <- fluidPage(
navbarPage("Demo",
tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
tabPanel("PDF file",
tags$iframe(style="height:800px;
width:200%;
scrolling=no",
src="https://your-path/your-file.pdf"))))
I am trying to upload a pdf to shiny. If the pdf file is from the Internet, the following code works well:
library(shiny)
runApp(list(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
h5("use case - embed a pdf user guide in the app - embed as a local pdf or from web URL")
),
mainPanel(
tabsetPanel(
# using iframe along with tags() within tab to display pdf with scroll, height and width could be adjusted
tabPanel("Reference",
tags$iframe(style="height:800px; width:100%; scrolling=yes",
src="https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf")),
tabPanel("Summary"),
tabPanel("Plot")
)
))
),
server = function(input, output,session){}
))
However, when I tried to upload a pdf saved in Desktop, which is also the working directory, I cannot see the pdf file. I used src="example.pdf" to replaced the web file link. As suggested by some other StackOverflow posts, I saved the pdf file in a folder named www, but it still not working.
The system is MacOS X El Capiton and safari browser. I am not sure if that makes any difference.
Thanks a lot!
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"