Home » How to set the package directory in R

How to set the package directory in R

  • by
  • 2 min read
Tags:

When I got my new company computer, out service desk installed R in a network folder, which made installing and loading R libraries extremely slow. That’s why I reinstalled R and needed to set my library directory explicitly, so that R doesn’t look in the network folder with every package operation. Here’s how to set the library directory in R.

Change the directory via the libpaths function

Finding out where R checks for your packages can be done by using the libpaths function. By not providing it any arguments, it simply returns a vector of all the directories.

.libPaths()

Setting the directory for your current session can be done by providing a directory to the new argument.

.libPaths(new = 'c:/new/directory')

You can run this command every time you start R, by putting it in the .Rprofile file.

Change the directory permanently via an environment variable

To change the library directory permanently on your system (or for your user), you should edit an environment variable. Environment variables can easily be edited by running the following command in your command prompt.

Set R_LIBS_USER if you only want to change it fo your user or use R_LIBS if you want to make the change for your machine.

rundll32 sysdm.cpl,EditEnvironmentVariables

If you can’t set environment variables on your machine, you can set it for your session via the .Renviron file.

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Leave a Reply

Your email address will not be published. Required fields are marked *