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"
Related
I spent a fair amount of time trying to solve that issue.
Of course I did my homework before sharing my issue here.
In particular I have unsuccessfully consulted :
local image in shiny app without img(src())?
Shiny can not display Image locally
adding local image with html to a Shiny app
R Shiny img() on UI side does not render the image
Display images from web in shiny R
Image failing to display in R shiny
Embedding Image in Shiny App
How to place an image in an R Shiny title
So I did create a 'www' folder at the root of the RStudio project file where I put some pictures.
These pictures are used in the titlePanel but also by the main htmlwidget the application calls.
It is crucial for me to have these pictures stored locally because the application may be running in a secured environment without any access to the Internet.
I tried a relative path to these pictures and an absolute path: no picture was displayed.
Then I noticed some kind of inconsistency: I experience this issue only when I run the application through the regular command in RStudio, "Run Selected Line(s)".
On the other hand, when I run the application through the dedicated command "Run App" (in the top right corner in RStudio, green arrow), I don't have this issue anymore, the pictures display nicely (but the input data are somehow inspected and it takes a lot of time before the application is launched).
Initially I thought that displaying local images would be much easier than with remote images stored on the Internet but it seems it is rather the other way around.
Hence my questions:
Do you know why we can observe this difference (which is an inconsistency to me)?
And do you know how I could still continue to use the regular execution command ("Run Selected Line(s)")?
Best regards,
Olivier
For me the following also works when running the app via Run Selected Line(s) in RStudio:
library(shiny)
# create some local images
if(!dir.exists("myimages")){
dir.create("myimages")
}
myPlotPaths <- paste0("myimages/myplot", seq_len(3), ".png")
for (myPlot in myPlotPaths) {
png(file = myPlot, bg = "transparent")
plot(runif(10))
dev.off()
}
myImgResources <- paste0("imgResources/myplot", seq_len(3), ".png")
# Add directory of static resources to Shiny's web server
addResourcePath(prefix = "imgResources", directoryPath = "myimages")
ui <- fluidPage(
tags$img(src = myImgResources[1], width = "400px", height = "400px"),
tags$img(src = myImgResources[2], width = "400px", height = "400px"),
tags$img(src = myImgResources[3], width = "400px", height = "400px")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Managing directories can be tricky.
You could use the here package to make things much easier to handle directories in R projects, see Ode to the here package.
After opening the project, images in www can then easily be accessed by:
here::here('www/myimage.jpg')
This will also work for sourcing an app or a script.
I don't have a specific answer, but Hadley has showed an example of how to display images from your stored locally under the 'Graphics' Chapter in 'Mastering shiny' book. The book is under development and it should be released soon, I will paste the link for that chapter:
Graphics chapter
The example is under images section.
HTH
I am trying to work through the R Studio Shiny tutorials. The second tutorial includes embedding an image into an app. It seems straight-forward and a similar question-and-answer here seems to use the same approach:
Embedding Image in Shiny App
However, I cannot get this approach to work. Here is a stripped-down version of R Studio's second tutorial code and a screenshot of the result I get. I have the png file in my working directory. Must the photo be placed somewhere else?
setwd('C:/Users/mark_/Documents/RShiny/')
library(shiny)
ui <- fluidPage(
titlePanel('Shiny App with Photo'),
sidebarLayout(
sidebarPanel(
h2("Installation"),
p("Shiny is on CRAN"),
br(),
img(src = "myScreenshot.png", height = 70, width = 200),
br()
),
mainPanel(
h1("Shiny"),
p("Shiny is a package from RStudio"),
br()
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
EDIT to Add Additional Steps Taken
When I originally posted my issue with R Shiny I was using the default R GUI. I have since switched to R Studio in case R Shiny works best that way. But this has not helped.
I have installed Rtools and added the following code to the very top of the above app.R file:
install.packages('zip')
install.packages('shinyjs')
install.packages('shinydashboard')
install.packages('shinyBS')
install.packages('shinyWidgets')
library(zip)
library(shinyjs)
library(shinydashboard)
library(shinyBS)
library(shinyWidgets)
install.packages('backports')
library(backports)
install.packages('devtools')
library(devtools)
rm(list=ls())
getwd()
setwd('C:/Users/mark_/Documents/RShiny/')
getwd()
library(shiny)
But this has not helped either.
As suggested by #MattB below I have created a subfolder named www inside the folder C:/Users/mark_/Documents/RShiny and place the file myScreenshot.png inside that www subfolder. However, this has not solved the problem. The image still does not appear.
I also tried placing the file myScreenshot.png inside that www subfolder of the RStudio folder under Program Files (C:\Program Files\RStudio\www) but this has not helped.
The image does not appear under the Open in Browser tab or under the http:// tab.
There are two options I have not yet tried. Perhaps I must change the name of the folder containing the app.R file (C:/Users/mark_/Documents/RShiny) to something other than RShiny. Perhaps that is confusing R Studio.
There is another issue that perhaps is preventing R Studio from locating the image file. When I install an R package I get the following message:
> install.packages('reshape2')
Installing package into ‘C:/Users/mark_/Documents/R/win-library/4.0’
(as ‘lib’ is unspecified)
also installing the dependency ‘plyr’
I end up with both:
C:\Program Files\R\R-4.0.0
C:\Users\mark_\Documents\R\win-library\4.0
I wonder whether that has anything to do with R Studio not being able to locate the image file.
For any files to be available to the app user, they need to be placed in the /www directory within your app folder. You don't need to change your code otherwise, as the user's browser will see this folder automatically.
I was able to get the R script to run in R Studio and display an image file by following suggestions by Dean Attali #DeanAttali at this website.
https://deanattali.com/blog/building-shiny-apps-tutorial/
I renamed the folder to Shineexample2 and placed that folder directly in the Documents folder. (C:\Users\mark_\Documents\Shineexample2) This was not one of Dean Attali's suggestions but the rest of the following was or was at least how I interpreted his suggestions.
This new folder Shineexample2 only contained the file app.R and the www subfolder containing the image file.
I opened the app.R file in R Studio, not the default R GUI, using File then Open File....
Then I clicked on Run App in the upper middle area of the R Studio GUI.
The file ran as expected.
The above process differed from my earlier attempts in several ways. Previously the app.R file was in a folder with a different name and containing many other files and subfolders. Usually I opened the file app.R in Notepad and copied and pasted its contents into R Studio. Then I selected all of the code and ran it using Code then Run Selected Line(s). I never noticed the Run App term at the top of the R Studio Gui before visiting Dean's website.
If I determine exactly which of these changes was most critical to successful execution of my code I may edit this answer in the future to provide that information.
Here are the complete contents of the app.R file that ran successfully.
#
# Stack_Overflow_Shiny_question_about_embedding_images_May22_2020.R
#
# install.packages('shiny')
# install.packages('zip')
# install.packages('shinyjs')
# install.packages('shinydashboard')
# install.packages('shinyBS')
# install.packages('shinyWidgets')
# install.packages('devtools')
#
library(shiny)
library(zip)
library(shinyjs)
library(shinydashboard)
library(shinyBS)
library(shinyWidgets)
install.packages('backports')
library(backports)
library(devtools)
rm(list=ls())
getwd()
setwd('C:/Users/mark_/Documents/Shineexample2/')
getwd()
library(shiny)
ui <- fluidPage(
titlePanel('Shiny App with Photo'),
sidebarLayout(
sidebarPanel(
h2("Installation"),
p("Shiny is on CRAN"),
br(),
img(src = "RStudio.png", height = 70, width = 200),
br()
),
mainPanel(
h1("Shiny"),
p("Shiny is a package from RStudio"),
br()
)
)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
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 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.
I am rather new in shiny, so I was running through some tutorials. However, I have a problem loading images from a file inside the "www" folder of the shiny app.
For example, when I run the code below, I get a missing image. However, if I refer to an image online, e.g., if I substitute "bigorb.png" by "http://shiny.rstudio.com/tutorial/lesson2/www/bigorb.png", I get the desired image without problems. I am using R version 3.3.1 on Windows 10. Can anyone help me?
ui <- shinyUI(fluidPage(
titlePanel("My Shiny App"),
sidebarLayout(
sidebarPanel(),
mainPanel(
img(src="bigorb.png", height = 400, width = 400)
)
)
))
server <- shinyServer(function(input, output) {
})
shinyApp(ui = ui, server = server)
This works for me:
Create folder /myApp
Inside /myApp: create a file starter.R with the following code, and make sure to set the working directory correctly:
library(shiny)
setwd("PATH_TO_myApp")
shiny::runApp("app")
In the folder /myApp, create a subfolder /app.
In /myApp/app/: create file "app.R" with your code from above.
Create another sub-subfolder /myApp/app/www/ . Include your image file.
Execute starter.R.
So your file list is:
/myApp/starter.R
/myApp/app/app.R
/myApp/app/www/bigorb.png