Share R Scripts across projects


#1

Hi,

I have some functions in separate R script files that are useful for several different RStudio projects I am working on. Is there a preferred way to use these functions across the projects? Duplicating the files in each project seems stupid and violates standard coding. For reproducibility, I'd like to have access to the exact version of the fxn I used for each project. I suspect over time these functions will slowly morph which might create some problems.

It seems like I could:

  1. Create soft links from one project directory to another. Using Git with this seems odd, though. I suppose the working project could Git commit the currently used version of the function. I'd have to return to the home project for the fxns and commit over there, too, at some point. Maybe this works. It just seems odd have one fxn stored in two different Git repositories.
  2. Source() the fxns from their home project. If I need to make changes, I'd just switch projects, fix them, and go back to my working project. This seems like I can't save them in a Git repository for my working project, however.
  3. Creating a package sounds slick, but I've never done this and seems like a lot of work. I suppose this also might suffer from reproducibility because I won't have a Git commit for the exact version of the function that I used.

Maybe 1 is the right way to go after thinking about this.

What am I missing?

thanks.


#2

I think creating a package is not as hard as it may look, specially if it is for your own use and don't need to comply with cran requirements. It also keeps your workspace cleaner. The main con is that you have to re-install / update after any changes, and that may be a small issue if you do a lot of changes.
cheers.
I've never use RStudio projects, so my choice when I am writting a lot of functions and are actively developed normally is the second one. I have a folder called scripts where I "try" to keep all my functions in different scripts
Fer.


#3

Hello @budall

Before I learned how to create R packages, I had a similar feeling that it might be too complicated....turns out it is a life changer and the way to go here. The learning curve is less steep than it seems and there are plenty of great resources...E.g.:

You should definitely use version control like Git. In your case, Git would allow you to revert back to earlier versions for each project. Perhaps a better option is the packrat package. The basic idea of packrat is that it will copy packages used in your project within the project directory and use these versions within the project. Thus, the version used at the time is always accessed. This is ideal for situations like yours where you want to have access to the exact version of the functions for each project, but the functions evolve over time.


#4

I'll second @jrelewi - I thought package development would be this massive undertaking, and it can be, but making a basic package that is really just for you doesn't have to be. I have a small package that I named after myself on GitHub. I have no intention of ever submitting it to CRAN, but I use to to store a few "general" functions that I find useful across many projects or tasks.

I do have continuous integration and unit testing set-up on it, but it is not very developed and a simple package that is just for you likely does not need that at the outset.

In addition to what @jrlewi recommends, here are a couple of other resources:


#5

So many wholesome replies encouraging people to develop packages! :grin: I love it :blush:


#6

Thanks, everyone. Really appreciate the input. I think it is off to package development school for me! Seems like a useful skill to have regardless of this project.