Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resources in www aren't served when shinyApp() is print()ed or passed to runApp() #3742

Open
klmr opened this issue Nov 29, 2022 · 6 comments

Comments

@klmr
Copy link

klmr commented Nov 29, 2022

System details

Output of sessionInfo():

R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux 8.6 (Ootpa)

Matrix products: default
BLAS/LAPACK: /apps/rocs/2020.08/cascadelake/software/OpenBLAS/0.3.9-GCC-9.
3.0/lib/libopenblas_skylakexp-r0.3.9.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] shiny_1.7.3         nvimcom_0.9-132     BiocManager_1.30.17

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9       crayon_1.5.2     digest_0.6.30    later_1.3.0
 [5] mime_0.12        R6_2.5.1         jsonlite_1.8.3   lifecycle_1.0.3
 [9] xtable_1.8-4     magrittr_2.0.3   cachem_1.0.6     rlang_1.0.6
[13] cli_3.4.1        promises_1.2.0.1 jquerylib_0.1.4  bslib_0.4.1
[17] ellipsis_0.3.2   tools_4.2.0      httpuv_1.6.5     fastmap_1.1.0
[21] compiler_4.2.0   memoise_2.0.1    htmltools_0.5.3  sass_0.4.4

Example application or steps to reproduce the problem

library(shiny)

ui <- fluidPage(img(src = "test.png"))

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

print(shinyApp(ui, server))

With the following working directory:

.
├── app.r
└── www
    └── test.png

Describe the problem in detail

Running this Shiny app results in a page that shows only a placeholder icon. The image URL returns a 404 HTTP status. The image is a valid PNG file with read permissions, and Shiny can successfully serve this file by adding an explicit resource handler via addResourcePath('prefix', file.path(getwd(), 'www')).

How the Shiny app is run does not matter; none of the following options works:

  • Inside RStudio via “Run App” button
  • Inside RStudio by executing the source file app.r
  • In an interactive R terminal (manually executing the code of the app.r file)
  • In a shell terminal via Rscript app.r

The issue exists across operating systems (Linux, Windows) and R versions, and at least with both Shiny 1.7.2 and Shiny 1.7.3.

Some (but not all!) other people can see the file when running the minimal example above. This indicates some machine-specific configuration, but I’m at a loss how to find it.

When intercepting shiny:::staticHandler calls I can see that the function is called once while constructing the Shiny app object and then repeatedly when loading the app:

  • for the sys.www.root (i.e. system_file('www', package='shiny'))
  • repeatedly (!) for the shared root, LIBPATH/shiny/www/shared
  • for the bootstrap resource path (LIBPATH/shiny/www/shared/bootstrap)

… but never for the app’s www directory. Based on this I believe that there is fundamentally no chance that the resource /test.png could ever be served by my minimal Shiny app, despite the fact that it works for other people on their systems. And the documentation states that

Static files under the www/ directory are automatically made available under a request path that begins with /.

So this should work.

@cpsievert
Copy link
Collaborator

Interesting! To be clear, this doesn't appear to be a problem without the print() call. Is there a particular reason why you need to explicitly print() it?

@cpsievert cpsievert changed the title Resources in www subdir are not found Resources in www aren't served when shinyApp() is print()ed Nov 29, 2022
@klmr
Copy link
Author

klmr commented Nov 29, 2022

Ah. So actually the issue persists even without the explicit print call! Just not when running it via the button in RStudio. But running Rscript app.r still fails, as does executing the code with auto-printing (e.g. by sending the line to R from Vim via Nvim-R, or by executing the code manually line by line in the R terminal).

In fact, the purpose of the explicit print call in my code is so that the app still launches when source-ing the file, because it otherwise doesn’t launch (except when explicitly running source('app.R', echo = TRUE)) — this is what Nvim-R does when executing the whole file.

@klmr
Copy link
Author

klmr commented Nov 29, 2022

For completeness, using runApp instead of print also has this issue. That is, the following also doesn’t download static resources:

runApp(shinyApp(ui, server))

@klmr klmr changed the title Resources in www aren't served when shinyApp() is print()ed Resources in www aren't served when shinyApp() is print()ed or passed to runApp() Nov 29, 2022
@ismirsehregal
Copy link
Contributor

ismirsehregal commented Nov 30, 2022

So far my understanding of this matter was, that the www folder to / prefix mapping only takes place if runApp's appDir parameter is provided with a directory or file path (some relevant functions can be seen here and here). Once a shiny app object is passed to appDir we would have to use addResourcePath.

@klmr
Copy link
Author

klmr commented Dec 1, 2022

@ismirsehregal But you can’t use addResourcePath with a prefix of /. In addition, this limitation should be clearly documented: the current documentation strongly implies that this should work.

@ismirsehregal
Copy link
Contributor

ismirsehregal commented Dec 1, 2022

@klmr as already stated on SO I absolutely agree. Either the documentation or the default implementation of the resource publishing via www needs an update. This topic causes confusion in the shiny community for years. There are numerous SO and posit community questions regarding it. This was the latest discussion I remember.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants