Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image won't appear from www folder in Shiny App

Tags:

shiny

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.

like image 516
skk Avatar asked Sep 06 '25 03:09

skk


1 Answers

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.

like image 65
Gregor de Cillia Avatar answered Sep 07 '25 23:09

Gregor de Cillia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!