A large part of R’s success is the ecosystem of open-sourced packages that add extra functionality to what R comes with (referred to as “base R”).
CRAN is the official central repository for R packages, where human reviewed packages go through quality checks for publishing on the CRAN network around the world. It is through CRAN that you install packages with the install.package() function, or via the Packages pane in the bottom left of RStudio.
This will become absolutely second nature, but it’s one of those things that is not 100% intuitive at first. When you’re using a package (which, really, means you’re using one or more of the functions in a package), there are two things that have to happen:
The package has to be installed. This is what is described above. The first time you use a package, you will need to type install.packages(\[the package name\]) in the console and press
The package has to be loaded. In your script (or in the console), enter library(\[the package name\]). Typically, you will have a list of these at the beginning of your scripts (although there are other techniques for centralizing a list of packages you commonly use…we’re not going to go there). Once a package is loaded, it will show with a checkbox next to it in the Packages list in your RStudio environment.
When you get a “function not found” error when running a script, 9 times out of 10 that’s because the function you’re calling is in a package that isn’t loaded.
This list simply cannot be comprehensive, but, to give you a sense of the breadth and scope of R Packages, below are a few representative examples:
You will learn to know the name “Hadley Wickham,” as he is quite possibly the most influential R user (and package and content creator) of the modern era. He also now works for RStudio. All of the packages below are part of what has been dubbed “the Hadleyverse,” because Wickham was key in their creation. He’s actually been working to rebrand this “the tidyverse” because, well, that’s how he rolls.
For a more comprehensive list of packages in the Hadley-tidyverse, you can check out The Hitchhiker’s Guide to the Hadleyverse. But, here, we’re just going to list a small handful of those packages that are practically core to using R:
%>% operator (which is available once you’ve loaded dplyr…but actually comes from another package that dplyr uses) can streamline the heck out of your code!There are also thousands of more experimental packages that are only available through Github. These packages have come much easier to work with since the introduction of the package devtools, which via its function install_github means access to this universe.
Beware though that the packages on Github are by their nature more experimental, so due care should be excercised to ensure they are trustworthy.