Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Part of the reason R has become so popular is the vast array of packages available at the cran and bioconductor repositories. In the last few years, the number of packages has grown exponentially!
This is a short post giving steps on how to actually install R packages. Let’s suppose you want to install the ggplot2 package. Well nothing could be easier. We just fire up an R shell and type:
> install.packages("ggplot2")
In theory the package should just install, however:
- if you are using Linux and don’t have root access, this command won’t work.
- you will be asked to select your local mirror, i.e. which server should you use to download the package.
Installing packages without root access
First, you need to designate a directory where you will store the downloaded packages. On my machine, I use the directory /data/Rpackages/
After creating a package directory, to install a package we use the command:
> install.packages("ggplot2"
, lib="/data/Rpackages/")
> library(ggplot2, lib.loc="/data/Rpackages/")
It’s a bit of a pain having to type /data/Rpackages/
all the time. To avoid this burden, we create a file .Renviron
in our home area, and add the line R_LIBS=/data/Rpackages/
to it. This means that whenever you start R, the directory /data/Rpackages/
is added to the list of places to look for R packages and so:
> install.packages("ggplot2"
)
> library(ggplot2)
just works!
Setting the repository
Every time you install a R package, you are asked which repository R should use. To set the repository and avoid having to specify this at every package install, simply:
- create a file
.Rprofile
in your home area. - Add the following piece of code to it:
cat(".Rprofile: Setting UK repositoryn")
r = getOption("repos") # hard code the UK repo for CRAN
r["CRAN"] = "http://cran.uk.r-project.org"
options(repos = r)
rm(r)
I found this tip in a stackoverflow answer .
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.