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)
Related
I would like to add hover tooltips to my input and output boxes on a shiny app that is deployed in shinyapps.io.
I found the RLumShiny package which can add tooltips and I have modified my app to accommodate this. The app works locally but when I try to deploy it to shinyapps.io I end up with the error seen below. There are no companion files to the app - just the ui.R and server.R files.
To deploy I run
library(rsconnect)
deployApp('~/sandbox/overdiag/', logLevel="verbose")
I get an error message
----- Deployment error -----
Error: C stack usage 7969336 is too close to the limit
(and a bunch of other information from the track). I've made a minimal example that produces the same error where the ui.R is
## ui.R ##
library("shiny")
library("RLumShiny") ## This package is problematic
library("shinydashboard")
library("shinyWidgets")
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
and server.R
library("shiny")
library("RLumShiny") ## Again this package
library("shinydashboard")
function(input, output, session) {
}
Now if I remove the library("RLumShiny") line then everything works fine and I can deploy it right away. I don't get information that the package is not available but maybe there is something else wrong (I have a nagging feeling that the javascript in the package might do some things that the shinyapps.io service does not like).
Now: is there an alternative approach (ie., some other package) to get hover tooltips on the shinyapps.io or can I do something else to get RLumShiny to work?
In general in shiny you can get tooltips by using tags$div in your ui.r to wrap your controls / outputs and giving it a title. So for example you could do:
tags$div(title="My tooltip", plotOutput(outputId="MyPlot"))
and you will get a tool tip. The same pattern works for controls.
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.
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
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"
I tried to deploy my shiny apps to xxx.shinyapps.io , but the problem is I use own dataset.
I set my dataset to my directory. but when I try to deploy it I got error.
Listening on http://127.0.0.1:45220Loading required package: DBIError in setwd("C:/Users/xxx/Dropbox/shiny/archive/db") : cannot change working directory
Thank you,
Put your dataset in a subdirectory of your shiny app directory (and change you code accordingly). Be sure to make the path to the data a relative path (not an absolute path - this generates a warning). This has worked well for me.
In server.R , have this code at the top
source("datarep.R)
Make a new .R file "datarep.R " in the same directory.
That file has just one code
data <- read.csv("data.csv")
There are two ways
Open a ".R"(navin.R e.g.) file in the app directory( where ui.r and server.R is placed) where you make the R object which contains the data
(For example: dataS <- read.csv("XX.csv"); dataT <- read.csv("YY.csv"))
and then in server.R , write the code initially: source(navin.R)
And use dataS, dataT objects as usual.
Place XX.csv and YY..csv in the same directory ( or any directory but then give the "relative path" )
and do everything as usual
For example : I used local data to make shiny Apps
https://manaswink.shinyapps.io/TelecomTower/
Adding to the comments provided by Paul
Insert the relative path to both ui.R and server.R
No need to pre-load the data file, it loads successfully along with runApp()
Finally, you can directly publish it to the shinyapps.io
Sample code:
For -> ui.R - where 'county.csv is local dataset'
county1<-read.csv("Data/county.csv")
library(shiny)
shinyUI(pageWithSidebar(..
..........
.......
Similarly,
For -> server.R
county1<-read.csv("Data/county.csv")
shinyServer(function(input, output, session) {....
......
......