Understanding 'lib' vs. 'destdir' in 'install.packages()'

How do I tell install.packages() where to install a package?

I don't understand the difference between the lib and destdir arguments in install.packages()

According to documentation, lib is a

character vector giving the library directories where to install the packages. Recycled as needed. If missing, defaults to the first element of .libPaths() .

while destdir is the

directory where downloaded packages are stored. If it is NULL (the default) a subdirectory downloaded_packages of the session temporary directory will be used (and the files will be deleted at the end of the session).

It seems to me like both of these are the path where the package is installed. What is the difference?

Similarly, which does lib.loc in library() correspond to? That is, will the path given to lib.loc be the same as the path for lib or for destdir?

This is where the "intermediate" installation files are downloaded, either the source file or precompiled binaries which are only needed temporarily for the installation process, that is why the documentation states: "...the files will be deleted at the end of the session)."

This is the location of your package library, where the packages get installed.

Thank you for clarifying. I'm still confused because when I pass the lib directory to library() I'm told there is no package, even though the file (compressed folder) clearly exists.

> install.packages("units", lib = "libs")
> list.files("libs")
# "units_0.7-2.tar.gz"   

> library(units, lib.loc = "libs")
# Error in library(units, lib.loc = "libs") : 
#   there is no package called ‘units’

This makes me feel like I'm putting the wrong file in my library folder.

  • Should the lib.loc directory contain TAR files?
  • Should I be giving library() the specific file name?
  • Really, just why is R telling me the package doesn't exist when I see it listed?

Manually specifying locations for each installation and library call is not the way to manage your package library and it is very prone to failure.

Most like this results in a failed installation process, otherwise you would get a folder called "units" inside libs and not a .gz file.

I would recommend setting your package library location by using the R_LIBS_SITE or R_LIBS_USER environment variables instead.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.