Missing image when running shiny example from tutorial - r

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

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")
)

unable to see output image in R shiny

i'm trying to show a logo in my Shiny app, but when i try to see i only can see a small icon, but not the image, this is my code:
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='C:/Users/carlo239/image[![enter image description here][1]][1]/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
And this is the issue, i tried to reload, but is not working,
I know that the image is ok, because if i click on Download i get the same image, but i'm unable to see in my outputs. THANKS !!
There are two ways:
Put the file in the www folder of your app (as suggested by #YBS). That is in most cases the recommended solution.
"Register" your folder as resource folder for your app (see below). However, that solution is intended to help package authors make resources available to the package's components. Also be aware that absolute paths can pose problems when you deploy your app.
library(shiny)
ui <- fluidPage(
mainPanel(
img(src='/foo/Capture2.jpg',
align = "right", height = '300px'),
### the rest of your code
)
)
server <- function(input, output, session) {
addResourcePath("foo", "C:/Users/wherever/your/file/may/be/located")
}
shinyApp(ui, server)

Shiny App with Embedded Photo not Working

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)

display.mode = 'showcase' in shinyApp() call - no code shown

I would like to be able to use display.mode = 'showcase' in an app run with the shinyApp() function call. According to the docs I should be able to pass any arguments that go runApp() through the options argument. The showcase mode works (the window is split) but it does not show the code. What's interesting is that if I run runExample("01_hello") everything works fine. I'm using shiny 1.0.5.
Code:
library(shiny)
ui <- fluidPage(
titlePanel("Sample App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("mtcars", "iris"))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
data <- reactive({
get(input$data, 'package:datasets')
})
output$table <- renderTable({
head(data())
})
}
shinyApp(ui, server, options = list(display.mode = 'showcase'))
Output:
I was having the same issue. I had the app.R file and created the DESCRIPTION file using notepad but couldn't deploy a shinyApp with the code. Then I copied the DESCRIPTION file from shiny\examples\01_hello and noticed this:
Turns out my file had a TXT extension, so Shiny wasn't reading it as a metadata file. Once I used the correct DESCRIPTION file (which you can edit using notepad), everything worked out fine.
This is more of an addendum to Gus_est's answer, since I had the same problem and wasn't able to get it run right from there.
Create a file inside the directory your app.R-file resides in, e.g. a txt-file. Write into the file what display mode is to be used using Debian Control file format. In our case it would look like that (Title is not necessary):
Title: My App
DisplayMode: Showcase
Then rename the file DESCRIPTION without providing a file ending. Ignore the warning.
When you run the app now, it will always be in display mode "showcase", you can override this only inside the runApp()-statement. So I find the documentation to be misleading.
Check your current working directory. This problem seems to occur, if the working directory is not set to the folder with the app code.

R shiny uploading a pdf from local drive does not work

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"

Resources