'www' folder unavailable to the webserver when using 'Run Selected Line(s)' in RStudio

Dear all,

With reference to the following discussion on stackoverflow, it seems that the www folder isn't made available to the webserver when using Run Selected Line(s) (if not done expicitly via addResourcePath). This issue doesn't occur when running the application through the dedicated command Run App (in the top right corner in RStudio, green arrow).

Am I right this is a bug?

Best regards,

Olivier

Hi,

Can you explain why you would want to use the Run Selected Line(s) instead of the Run app? In my understanding the www folder is indeed sourced during runtime because it is needed to display stuff on the client side. It is seen as the root folder for all images referenced in HTML, and that's why you don't need to type "www/" in front of the image path. Of course if you try and run this without Shiny being active, the image won't be found.

Again, I'm probably not understanding the question here, but please elaborate on why you want to use this approach.

PJ

Hi PJ,

I actually want to be able to use both the Run Selected Line(s) and Run app commands.

OK, I confess I am more used to the Run Selected Line(s) one (that's precisely how I found the issue) probably because I do not only develop Shiny applications within R.

It's also for mere consistency reasons: when resources are online, it's strictly (from a user perspective) equivalent to run the application either via Run Selected Line(s) or Run app.

Why should it then be different when these resources are offline / local?

Moreover, from a very practical perspective, I noticed that when running the application via Run app, in my specific case the input data generate a lot of warnings that are by default displayed and significantly slow down the effective start of the application. When running it via Run Selected Line(s), only a summary message (e.g. 'there are 50+ warnings') is displayed and the application starts immediately.

Are these explanations helpful?

Best regards,

Olivier

Hi,

Could you generate a reprex for this? That will help me understanding the issue way better. You can use the link below to build one if you haven't done this before

Also, the fact that your app starts with 50+ warnings might indicate that the issue is somewhere in your own code and not in Shiny or RStudio itself :slight_smile:

PJ

Hi PJ,

A reprex can be found in the stackoverflow discussion (kindly provided by ismirsehregal) mentioned in my first message. I copy it below:

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)

No, there is no issue in the code: these warnings are simply generated by the (big) input files (mostly format issues).
There is indeed a consistency issue within RStudio on the way a Shiny application is run. You will very probably notice it too by running the reprex (and replacing the local myimages folder by www and removing the line with addResourcePath - the pictures should not display although they should if the www worked properly with the Run Selected Line(s) command).

Best regards,

Olivier

Below please find another example trying to point out what Olivier is refering to. Using the "Run App" button the images are found, while they are not found when the following code is run via Run Selected Line(s) or Ctrl+Enter (my workaround can be seen above):

library(shiny)

# create some local images
if(!dir.exists("www")){
  dir.create("www")
}

myImgNames <- paste0("myplot", seq_len(3), ".png")
myPlotPaths <- file.path("www", myImgNames)

for (myPlot in myPlotPaths) {
  png(file = myPlot, bg = "transparent")
  plot(runif(10))
  dev.off() 
}

ui <- fluidPage(
  tags$img(src = myImgNames[1], width = "400px", height = "400px"),
  tags$img(src = myImgNames[2], width = "400px", height = "400px"),
  tags$img(src = myImgNames[3], width = "400px", height = "400px")
)

server <- function(input, output, session) {
  
}

shinyApp(ui, server)

From ?runApp

appDir
The application to run. Should be one of the following:

A directory containing server.R, plus, either ui.R or a www directory that contains the file index.html.

A directory containing app.R.

An .R file containing a Shiny application, ending with an expression that produces a Shiny app object.

A list with ui and server components.

A Shiny app object created by shinyApp().

In other words we could say, that the contents of the www folder aren't found for a Shiny app object created by shinyApp() when executed via Run Selected Line(s):

However, my personal opinion is, that when you want to run a shiny app in RStudio use Ctrl+Shift+S instead of Ctrl+A (selection) and Ctrl+Enter.

Many thanks, ismirsehregal, for these thorough additions.

1 Like