4

I'm following the code from this previous question: R Shiny add picture to box in fluid row with text

Here is my code:

box(title = "Instructions",
            status = "primary",
            solidHeader = F,
            collapsible = F,
            width = 12,
            fluidRow(column(width=10,textOutput("instructions")),
                     column(width=2, align="center",
                            img(src="no1.jpeg", width=100))))

server <- function(input, output) {

output$instructions <-renderText(print("test"))}

##Create and Run Shiny App Object---------------
shinyApp(ui, server)

runApp("~/shinyapp")

I think the location of my www folder is wrong. I put it in the same folder as my .Rpoj. I'm using a Mac

/Users/myname/Desktop/ProjFolder/www:

I'm really not sure where else to put it, or how to get to the place where I need to put the www folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
skk
  • 175
  • 3
  • 10

2 Answers2

13

The www folder should be in the same directory as the app.R file if your app is invoked with runApp("path/to/appfolder"). Your working directory only matters if you run shinyApp(ui, server) directly in the console. That is because runApp changes your working directory temporarily to the appfolder you point to.

If you want to use an absolute path to an image you can use addResourcePath like so

addResourcePath(prefix = 'pics', directoryPath = '~/pictures')
ui <- fluidPage(
   tags$img(src = "pics/my_picture.jpg")  ## use the prefix defined in
                                          ## addResourcePath
)
server <- function(...) { }
shinyApp(ui, server)

addResourcePath can also be used to load JavaScript and CSS resources into your app.

More on runApp()

Since this came up in the comments, I want to clarify some things about the behavior of shiny::runApp(). The first argument of this function can either be a path to an app or an app object. Examples

dummy_app <- shinyApp(getwd(), function(...) {})
runApp("path/to/appfolder")  # path
runApp("path/to/app.R")      # path
runApp(dummy_app)            # app object

If a path is used, runApp() will change the working directory to the app directory (path/to/appfolder or path/to) until the app is finished. If an app object is passed, the current working directory is used as-is.

If an app object is printed as in

class(dummy_app)
#> [1] "shiny.appobj"
dummy_app

this will invoke shiny:::print.shiny.appobj which references to runApp(<appobj>) so again, the working directory is preserved.

Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • "`runApp` changes your working directory temporarily to the appfolder you point to" - is that true? I don't see that anywhere in https://github.com/rstudio/shiny/blob/master/R/runapp.R. – bers Jul 14 '21 at 18:34
  • Changing the working directory is hidden more deeply in the implementation. See [R/shinyApp.R#L234](https://github.com/rstudio/shiny/blob/e29d92c5ff00063778d0fee26eecc48d75eb3aee/R/shinyapp.R#L234). Basically, it makes a difference wether `runApp()` is used as `runApp(appobj)` or `runApp(path)` because `shiny::as.shiny.appobj()` is a S3 generic. – Gregor de Cillia Jul 15 '21 at 17:49
  • Exactly - a `shinyApp` generated by `shinyApp(ui, server)` (which the OP is using) does not even have the concept of an `appDir`, I think. It's `onStart` is `NULL` by default: https://github.com/rstudio/shiny/blob/e29d92c5ff00063778d0fee26eecc48d75eb3aee/R/shinyapp.R#L74 – bers Jul 16 '21 at 09:33
  • This is true. However, OP explicitly has `runApp("~/shinyapp")` in his question so I felt i needed to clarify this (often unexpected) behavior when I wrote my answer. If you use `runApp()` and point to an `app.R` file that has `shinyApp()` in the last line, the working directory is still changed to the parent directory of `app.R`. – Gregor de Cillia Jul 16 '21 at 12:35
  • This can be easily tested with a simple dummy app: `shiny::shinyApp("", function(...) print(getwd()))`. – Gregor de Cillia Jul 16 '21 at 12:39
  • Oh, very true. The OP seems to use a somewhat redundant call of shinyApp, followed by the (only?) relevant call to runApp – bers Jul 17 '21 at 09:18
  • thank you @GregordeCillia! your detailed of using `addResourcePath()` helped solve my issue. I am trying to change to using multiple files for Shiny apps (ui.R, server.R, global.R...) and couldn't figure out why i couldn't add a simple .png image. Worked like a charm! – kyle-G May 10 '23 at 03:01
0

Check out the suggestion here. Solved the issue for me. Move the "www" folder to "inst" and create a new R script containing the following in the folder .R.

.onLoad <- function(libname, pkgname) {
  shiny::addResourcePath(
    prefix = "www",
    directoryPath = system.file(
      "www",
      package = "name of your package"
    )
  )
}

.onUnload <- function(lib name, pkgname) {
   shiny::removeResourcePath("www")
}

When referring to images in the UI, it should look like this:

img(src = "www/image.jpg")  # prefix + filename
Fabias
  • 1