Packages/Libraries

These are scripts written by others that deal with a specific task or types of data. For example the “lubridate” package deals entirely with formatting and working with dates and times. The capacity to use scripts contributed by others are one of the things that make R (and Python) so useful. Whatever you are trying to do, someone has probably developed one or more packages to deal with it. If they haven’t and you have to figure it out - make a package out of your code and upload it so others can use it. This is the idea behind CRAN - The Comprehensive R Archive Network. CRAN coordinates the development of R and serves as a moderated repository for the thousands of packages written by contributors. At the moment, CRAN has 18766 packages available. There are also numerous packages available on github and elsewhere.

Using a package requires two actions. First, you have to download and install the package on your system. Assuming you know the name of the package you wish to install you can type: install.packages("packageName") in your RStudio console to install the package on your system. This only needs to be done the first time you use a package. Note the quotation marks. The second step is to load the package into your script. By convention packages need to be loaded at the top of the script - library(packageName). Quotation marks aren’t necessary here. “Running” that line (Ctrl+enter) in your script will load the “packageName” package and make the functions within it available inside your script. You will have to load the library in any script to access the functions. There is a way of just loading a single function though (package::function). Searching on the package name should provide numerous sources of documentation describing how to use the functions within it.