Recently I was doing R package development, I used 3 packages. But the problem is that I have to run library()
command every time I load the project, which annoyed me a lot. Then I found out that I can set the default packages loaded when R start up.
1. First Let’s check what default packages are:
> (.packages())
(Note that the parenthesis can not be omitted.)
2. Files in three folders are important in this process:
i>. R_HOME
, the directory in which R is installed. The etc sub-directory can contain start-up files read early on in the start-up process. Find out where your R_HOME
is with the R.home()
command.
ii>. HOME, the user’s home directory. Typically this is /home/username on Unix machines or C:\Users\username on Windows
(since Windows 7). Ask R where your home directory with, path.expand("~")
(note the use of the Unix-like tilde to represent the home directory).
iii>. R’s current working directory. This is reported by getwd()
.
3. Then we need to locate the .Rprofile
file, which is the R project default start up file.
file.path(Sys.getenv("HOME"), ".Rprofile")
can help us locate the file directory.
For more information please refer to r-startup.
4. Rewrite this startup file.
> file.edit("~/.Rprofile")
# This opens up a script, within which you can enter in your library commands
library(ggplot2)
library(Rcpp)
library(plyr)
library(reshape2)
Finally you can run library()
again to check whether these packages are loaded.
Thank you for your reading, This site is actively updated. Keep an eye on it if interested.