0

I'm collaborating with a few other users on a variety of R projects. In order to re-use as much code as possible we've created a "stdlib.R" file containing many functions used across multiple projects.

We previously had access to a network drive, so the code could take advantage of the common path by calling:

source("f:/code/stdlib.R")

But now that we're growing up I don't want to depend upon the existence of the network drive (or any hardcoded path for that matter). How can I create code that uses a localized path? Something like this:

source("%localusrpath%/stdlib.R")

where each user has a way to define what %localusrpath% means to them.

For me it could be C:/temp, for another it could be C:/users/user123/documents.

And as long as they set up that "environment variable" then it's going to work for them.

One solution is to use relative paths only:

source("./stdlib.R")

But this will fail if the user ever changes their working directory:

swd("c:/anything_else")

How do I set up a user-specific environment variable through the operating system, and then how can I retrieve/make use of that variable in order to concatenate a path? Or is this whole thing a bad idea?

Jonathan
  • 491
  • 5
  • 19
  • 4
    you should make `stdlib.R` a full-on package. it really isn't that much work, esp if you aren't going to submit it to CRAN. – hrbrmstr Oct 15 '15 at 16:35
  • That is a great suggestion, and I'll probably eventually do that for this particular file. But I have severl dozen others I want to share with the relative path problem . . . I just chose to ask about one in this thread to simplify the question. I realize now I should have indicated that I have dozens of files I want to share. – Jonathan Oct 16 '15 at 15:23
  • which makes the creation of a package an even better solution – hrbrmstr Oct 16 '15 at 15:27
  • agreed . . .will certainly create full packages as the code matures, and we figure out what's going to stick and be discarded or seldom used. – Jonathan Oct 16 '15 at 19:10

2 Answers2

2

Why not host the stdlib.R file on Github in a repository that you all have admin privileges to? Then you could use devtools::source_url to point to the Github repo to source the files.

That way you don't have to worry about everyone having the most up-to-date version on their local machine.

tblznbits
  • 6,602
  • 6
  • 36
  • 66
  • A very good idea and I'll try to reasearch that more. However, this is just one of several files I intend to share, and the are all hosted on a private repository. Ultimately I want our programs to be distributed to serveral people who will not have access to those repositories. So this sounds like a good solution for development, but not full deployment. – Jonathan Oct 16 '15 at 15:23
0

I found what I was looking for in this question: setting the home directory in windows R

I did a poor job asking the question, mostly becaue I wasn't sure what I needed. But ultimately I wanted the code to be portable across multiple computers, each of which will have a Github account. So, by setting the R_USER system environment on each computer, I can source the files independent of the end user's directory structure. As long as each end user puts this line in the Rprofile.site file.

Sys.setenv(R_USER="/my/desired/path/to/tilde")
source("~/stdlib.R")
Community
  • 1
  • 1
Jonathan
  • 491
  • 5
  • 19