4

Apologies if someone has asked this before. I've tried Googling and Googling, but no one seems to have reported quite this. I'm trying to add a path to my .libPaths, but .libPaths(new=blah) doesn't seem to alter anything. Here is my code:

packagedir = paste0(getwd(),'/extraRPackages/')
newLibPaths = c(packagedir,.libPaths())
print(newLibPaths)
.libPaths(newLibPaths)
print(.libPaths())

...here is the output from the first print (what I'm passing into .libPaths):

[1] "C:/Users/GCW/Dropbox/Mash share/Phil/R/mashdb/database/extraRPackages/"
[2] "C:/Users/GCW/Documents/R/win-library/3.3"                              
[3] "C:/Program Files/R/R-3.3.1/library"

... and the second print:

[1] "C:/Users/GCW/Documents/R/win-library/3.3" "C:/Program Files/R/R-3.3.1/library"    

Why might my call to .libPaths(new=blah) do absolutely nothing?? I'm stumped! I've tried it in RStudio and RPortable, but both give me the same output... Any help v much appreciated...

(I know from reading other answers that there are "proper" ways to change the paths permanently, but this is something I want to be able to do dynamically in a rather unusual setup, and if .libPaths just did what I thought it would, I'd be done...)

EDIT: It was suggested that this might be a duplicate of this question Changing R default library path using .libPaths in Rprofile.site fails to work -- but I don't think it is. I read this question before posting my own -- but in this question, it says at the end that "if I start RStudio the .libPaths() command seems to work as it is supposed to" -- but it is precisely in RStudio that the command doesn't do anything for me (and this is where I want to use it, not in the RProfile.site file and not in any permanent way)....

Community
  • 1
  • 1
justme
  • 193
  • 1
  • 8
  • http://stackoverflow.com/questions/15170399/changing-r-default-library-path-using-libpaths-in-rprofile-site-fails-to-work – mkt Aug 30 '16 at 10:49
  • 1
    Thanks for looking at this! I've added an edit to explain why I think this is different. Specifically, I want to be able to update my .libPaths as a one-off within some R code. It's hacky, I know, but for this one application it will make my life a lot easier... – justme Aug 30 '16 at 10:59
  • Oh, figured out my silly mistake. Fixed (see below). Thanks! – justme Aug 30 '16 at 12:01

2 Answers2

4

Ah I figured it out! The folder name you pass to .libPaths(blah) should not end with a "/". I changed the first line above to

packagedir = paste0(getwd(),'/extraRPackages')

...and all is well. Apologies for the dumb question!

justme
  • 193
  • 1
  • 8
2

This can also happen if the directory you're trying to add to .libPaths doesn't exist yet.

Peter Harrison
  • 873
  • 1
  • 8
  • 12