How should I access data and python scripts in a Shiny app?

I'm building a Shiny app as an internal package (ie. not to be deployed to CRAN or publicly accessible, just installable by coworkers). The app also uses reticulate, so I currently have a package structure that looks like:

DESCRIPTION
NAMESPACE
README.md
LICENCE
R/
|---package-launch.r
|---package-server.r
|---package-ui.r
|---configure-reticulate.r
inst/
|---assets
    |---background.png
|---python
    |---daily_processing.py
    |---indices.py
|---internaldata
    |---something.csv

I refer to the python scripts using:

reticulate::import_from_path('indices',
  path = file.path(find.package('mypackage'), 'inst', 'python'))

While I retrieve other assets with:

system.file('inst', 'internaldata', 'something.csv', package = 'mypackage')

While I'm testing using devtools (using document) and load_all()), this works great. But if someone installs the package, the contents of inst is raised a level, and the files can't be found anymore. But if I don't refer to inst, I can't access the files without installing the package myself—simply using load_all() doesn't move the files.

Should I be accessing or storing the files differently? (Since this package is internal, it doesn't need to pass CRAN checks, but it should install without an issue!)

EDIT: The R package book says to store raw data in inst/extdata and to refer to them using system.file() without inst " To refer to files in inst/extdata (whether installed or not)" (emphasis mine). My experience is that for othe subfolders in inst, this doesn't work unless installed. Is inst/extdata special here?

The book also explicitly says to store Python scripts in inst/python and also says:

To find a file in inst/ from code use system.file() . For example, to find inst/extdata/mydata.csv , you’d call system.file("extdata", "mydata.csv", package = "mypackage") . Note that you omit the inst/ directory from the path. This will work if the package is installed, or if it’s been loaded with devtools::load_all() .

This suggests to me that system.file() should work fine if I omit inst. I'm not sure what to do for reticulate::import_from_path, though, as it asks for a file base name and a path, not a file path that could be used with system.file().

EDIT: turns out system.file() also works with directories!

system.file('python', package = 'mypackage')
# [1] "C:/Users/rensa/Code/mypackage/inst/python"

Which means I can use:

reticulate::import_from_path('indices',
  path = system.file('python', package = 'mypackage'))

Phew!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.