Packages

The R community actively contributes to package development, resulting in a rapidly growing ecosystem of packages that extend the core functionality. This package ecosystem is one of its greatest strengths, providing an extensive collection of tools and functionalities that make R a powerful language for data analysis, statistics, and visualization.

Packages can be found hosted in different repositiories on the Internet. The main ones are:

R packages in CRAN can be easily installed using the install.packages() function. Once installed, packages are loaded into the R environment using the library() function. We are going to see examples of of how to use both functions in this section.

CRAN

The power of R comes from its great wealth of excellent packages. These packages are managed in a central repository called CRAN (the Comprehensive R Archive Network). There are very strict protocols to follow to publish a package in CRAN, which includes an external review stage. As such, publishing an R package is a lot like publishing a paper, and so R packages on CRAN are mostly of a high standard, and come complete with documentation and tests. You can get an idea of what is needed to publish a package on CRAN by reading R Packages by Hadley Wickham and Jennifer Bryan. This excellent online book provides complete detail of how to write and publish an R package.

A good way to find the package you want is to use an R search service, such as rseek. You can search for individual package names, or even the kind of thing you want to do. As most R packages come with vignettes (web pages that show documentation and examples of how to use the package) this means you can quickly find both the package that achieves your goal, plus documentation and examples.

Exercise

Use https://rseek.org to look for packages that help you calculate Pearson’s product-moment correlation. Limit the search to vignettes. Can you find a vignette that shows you how to do this? Do not worry that the R in the vignette is more advanced than you’ve seen so far - it won’t be long before it will make sense.

Go to https://rseek.org and search for the method we are interested in: “pearson product-moment correlation”. Set the search type to “Vignette”, and you should see several results.

This search result shows promise, so take a look at that: broom and dply.

This page contains an example showing a calculation of Pearson’s product-moment correlation. From this we can see that the cor.test from the stats package is used to calculate these correlations. We can get help with cor.test by typing ?cor.test in the console.