Change R default library path using .libPaths in Rprofile.site fails to work
I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?
.libPaths( c( .libPaths(), "~/userLibrary") )
# obviously this would need to be a valid file directory in your OS
# min just happened to be on a Mac that day
Or (and this will make the userLibrary the first place to put new packages):
.libPaths( c( "~/userLibrary" , .libPaths() ) )
Then I get (at least back when I wrote this originally):
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"
The .libPaths
function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv
and Sys.setenv
that report and alter the R environment variables have been split apart but .libPaths
can either report or alter its target.
The information about the R startup process can be read at ?Startup
help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio
In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library
and .Library.site
, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.
Since Sys.getenv()
returns the current system environment for the R process, you can see the library and other paths with:
Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
The two that matter for storing and accessing packages are (now different on a Linux box):
R_LIBS_SITE /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
What is a fool-proof way of permanently setting R working directory?
What about something like
set_default_wd <- function(wd = getwd()) {
text <- paste0(
'local({ setwd("', wd, '") })')
##
if (Sys.info()["sysname"] == "Windows") {
write(
text,
file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
append = TRUE)
} else {
write(
text,
file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
append = TRUE)
}
}
##
#R> set_default_wd() #set_default_wd("some/file/path")
This should work on Windows and Unix-like systems, and avoid any permissions issues. Really the only requirement on the user's end is to specify a valid file path, which they should (hopefully) be able to work out.
It may be worthwhile to have the option of overwriting the $HOME/.Rprofile
(instead of forcing lines to be appended) in case a malformed file path is given, etc...
set_default_wd <- function(wd = getwd(), overwrite = FALSE) {
text <- paste0(
'local({ setwd("', wd, '") })')
##
if (Sys.info()["sysname"] == "Windows") {
write(
text,
file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
append = !overwrite)
} else {
write(
text,
file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
append = !overwrite)
}
}
How to set the default working directory of my R project to my project directory?
I found my mistake. For some reason I had setwd("~")
in my Rprofile.site document... I removed it and tried again with starting new projects in RStudio. Now getwd()
shows the correct folder of the current project.
Change home directory for R
This is based on your windows environment variable HOME
. You need to reset HOME
to the path that you want "C:/Users/MyName/Documents"
If you want to do that from within R, you can use:
Sys.setenv(HOME="C:/Users/MyName/Documents")
This change would not be permanent. If you wish to avoid doing this every time you run R, you could put the above statement in your .Rprofile file. There is a nice article on setting up your .Rprofile in the RStudio support
Related Topics
Cannot Read File with "#" and Space Using Read.Table or Read.CSV in R
Mutate Multiple/Consecutive Columns (With Dplyr or Base R)
Plot Separate Years on a Common Day-Month Scale
Group Vector on Conditional Sum
The Representation of an Empty Argument in a "Call"
R + Ggplot2: How to Hide Missing Dates from X-Axis
Back-To-Back Barplot with Independent Axes R
Group Rows in Data Frame Based on Time Difference Between Consecutive Rows
Calculate Percentage for Each Time Series Observations Per Group in R
Removing One Table from Another in R
How to Run a R Language(.R) File Using Batch File
Insert Missing Time Rows into a Dataframe
How to Plot Bars and One Line on Two Y-Axes in the Same Chart, with R-Ggplot
In R, How to Plot into a Memory Buffer Instead of a File
Extract Data Between a Pattern from a Text File
In R, Check If String Appears in Row of Dataframe (In Any Column)
R Data.Table Fread Command:How to Read Large Files with Irregular Separators