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

Create an R package #2

Closed
HenrikBengtsson opened this issue Mar 21, 2020 · 11 comments
Closed

Create an R package #2

HenrikBengtsson opened this issue Mar 21, 2020 · 11 comments

Comments

@HenrikBengtsson
Copy link

I propose that you create an R package named ThreeDGliomaAtlas that serves your function. The advantage is that all package dependencies will be installed automatically, there is no need for source() in the Shiny app - just a library(ThreeDGliomaAtlas). When there is a first working R package, we can then automatically check it with R CMD check which is an awesome tool for catching current mistakes but also future ones (you can have GitHub Actions to run these check every time you push and you'll be notified if you made a mistake).

The gist is:

  1. Create a DESCRIPTION file that specifies the name of the package, has a short description and that specify all package dependencies that your app needs.

  2. Move your scripts/**/*.R files to R/*.R

  3. Run devtools::install(). Tweak what needs to be tweaked, and repeat.

If you haven't done an R package or struggle, then I can help you. For me, it's pretty straightforward to do but if you want to learn, this is a great opportunity.

When the above is in place, the next step is to also move in the data files into the R package. Hosting data is something R packages do well too. Then, one can move in also the Shiny app into the package. At this point, you will have your Shiny app/artifact fully contained in a single R package. This is where you can consider submitting it to CRAN and have it archived there until the end of time.

@HenrikBengtsson
Copy link
Author

Another, major, advantage with having a self-contained R package is that it will be much much ... much easier for you to update the app that will be deployed on the ShinyProxy server. In that case, all that will be required will be to rebuild the Docker container (which should be fully automatic) and re-deploy on the server (also automatic).

@SRHilz
Copy link
Owner

SRHilz commented Mar 22, 2020

Okay, thanks for figuring out these steps. I will make it tomorrow (I've made one before a few years ago, so it should be a nice review). Will update here with any qs or once done.

@SRHilz
Copy link
Owner

SRHilz commented Mar 24, 2020

Screen Shot 2020-03-24 at 11 26 28 AM

Quick question - I originally had (for the non packaged version of this) all of my data organized in nested subdirectories. For R packages I've made in the past, I had always had my data in .rda files directly in /data. Is it valid to keep this original nested subdirectory structure in /data? Or will I need to get all of my individual data files out of these subdirectories and have them directly in /data? Thanks!

@SRHilz
Copy link
Owner

SRHilz commented Mar 25, 2020

I also have one other quick question about going from a shiny structure to an R package structure. For a shiny app to find image files to display as part of the html layout (like the ones I use in the About app tab aboutTab.R), I have to put them in a folder called www in the main directory in which app.R is located. But in an R package structure, I can't have an app.R out in the main directory (has to be nested in an inst folder according to
image )

Screen Shot 2020-03-24 at 6 10 39 PM

Where then do I put the www folder so that shiny can still find it? Does it go in the inst/shinyApp folder with app.R still?

Thanks again!

@HenrikBengtsson
Copy link
Author

I originally had (for the non packaged version of this) all of my data organized in nested subdirectories. For R packages I've made in the past, I had always had my data in .rda files directly in /data. Is it valid to keep this original nested subdirectory structure in /data? Or will I need to get all of my individual data files out of these subdirectories and have them directly in /data?

It is ok to use whatever nested folder structures you'd like. However, you must not put in a folder named data/ because that folder is reserved for very special purposed so that the data() function can find them and among other things. From Section 'Data in packages' in 'Writing R Extensions' (help.start()):

Data files can have one of three types as indicated by their extension: plain R code (.R or .r), tables (.tab, .txt, or .csv, see ?data for the file formats, and note that .csv is not the standard22 CSV format), or save() images (.RData or .rda)

Instead, I recommend that your put your existing data/{datasets,metadata,models}/** structure holding the *.rds files under inst/exdata/{datasets,metadata,models}/**. As you found out, anything under inst/ will end up in the package "root" folder available from within R as:

root <- system.file(package = "GliomaAtlas3D", "exdata")

and specific subfolders as:

path <- system.file(package = "GliomaAtlas3D", "exdata", "datasets")
path <- system.file(package = "GliomaAtlas3D", "exdata", "datasets", "Patient260")
...

or if you prefer:

root <- system.file(package = "GliomaAtlas3D", "exdata")
path <- file.path(root, "datasets")
path <- file.path(root, "datasets", "Patient260")
...

You could of course also skip the exdata/ level and just drop in inst/datasets/, inst/metadata/, and inst/models/. If so, then you'd use:

root <- system.file(package = "GliomaAtlas3D")

above.

@HenrikBengtsson
Copy link
Author

HenrikBengtsson commented Mar 25, 2020

FYI, and we don't have to do this already now, but if you imagine that there might be other data sets/samples in the future, you could also create a standalone dataset package that only holds the data, e.g. GliomaAtlas3D.data (or some more informative name) that then the main GliomaAtlas3D package uses. If you want to add other types of data set, you could then image a GliomaAtlas3D.data2021 package, and so on. It depends on how tightly coupled the main package is to the data. Again, this can be discussed/done at a later stage.

@HenrikBengtsson
Copy link
Author

Where then do I put the www folder so that shiny can still find it? Does it go in the inst/shinyApp folder with app.R still?

Not sure - I typically end up with trial'n'error in these cases. But, yes, inst/shinyApp/www/ is a good guess.

@SRHilz
Copy link
Owner

SRHilz commented Apr 1, 2020

I originally had (for the non packaged version of this) all of my data organized in nested subdirectories. For R packages I've made in the past, I had always had my data in .rda files directly in /data. Is it valid to keep this original nested subdirectory structure in /data? Or will I need to get all of my individual data files out of these subdirectories and have them directly in /data?

It is ok to use whatever nested folder structures you'd like. However, you must not put in a folder named data/ because that folder is reserved for very special purposed so that the data() function can find them and among other things. From Section 'Data in packages' in 'Writing R Extensions' (help.start()):

Data files can have one of three types as indicated by their extension: plain R code (.R or .r), tables (.tab, .txt, or .csv, see ?data for the file formats, and note that .csv is not the standard22 CSV format), or save() images (.RData or .rda)

Instead, I recommend that your put your existing data/{datasets,metadata,models}/** structure holding the *.rds files under inst/exdata/{datasets,metadata,models}/**. As you found out, anything under inst/ will end up in the package "root" folder available from within R as:

root <- system.file(package = "GliomaAtlas3D", "exdata")

and specific subfolders as:

path <- system.file(package = "GliomaAtlas3D", "exdata", "datasets")
path <- system.file(package = "GliomaAtlas3D", "exdata", "datasets", "Patient260")
...

or if you prefer:

root <- system.file(package = "GliomaAtlas3D", "exdata")
path <- file.path(root, "datasets")
path <- file.path(root, "datasets", "Patient260")
...

You could of course also skip the exdata/ level and just drop in inst/datasets/, inst/metadata/, and inst/models/. If so, then you'd use:

root <- system.file(package = "GliomaAtlas3D")

above.

This worked beautifully, thank you.

@SRHilz
Copy link
Owner

SRHilz commented Apr 1, 2020

Where then do I put the www folder so that shiny can still find it? Does it go in the inst/shinyApp folder with app.R still?

Not sure - I typically end up with trial'n'error in these cases. But, yes, inst/shinyApp/www/ is a good guess.

Alright I finally figured out from trial'n'error how to get this to work. For future ref just in case someone else asks, I ended up having to create a new resource path so shiny could find it:

addResourcePath('www', system.file('shinyApp/www', package = 'GliomaAtlas3D'))

and then to use an object from it, reference this resource name:

img(src= 'www/image1.png')

@SRHilz
Copy link
Owner

SRHilz commented Apr 1, 2020

FYI, and we don't have to do this already now, but if you imagine that there might be other data sets/samples in the future, you could also create a standalone dataset package that only holds the data, e.g. GliomaAtlas3D.data (or some more informative name) that then the main GliomaAtlas3D package uses. If you want to add other types of data set, you could then image a GliomaAtlas3D.data2021 package, and so on. It depends on how tightly coupled the main package is to the data. Again, this can be discussed/done at a later stage.

Great idea, definitely could be a handy thing to have as it sounds like we will be adding different datatypes in the future from our lab. Thanks!

@SRHilz
Copy link
Owner

SRHilz commented Apr 1, 2020

Final new package is in https://github.com/SRHilz/GliomaAtlas3D/

@SRHilz SRHilz closed this as completed Apr 1, 2020
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

2 participants