Chapter 12 Packages
While base R comes loaded with heaps of useful functions, what really distinguishes R comes through its easy extendability. R extensions come as packages that can easily be installed (and uninstalled) and loaded if needed. And since pretty much everyone can contribute packages, there are tons of those out there.
So where to start? The place to go to is the Comprehensive R Archive Network (CRAN) available at https://cran.r-project.org/. However, as with most other things regarding R, finding help in the official documents is often cumbersome - so if you are looking for something specific, an internet search engine is usually your best friend.
Once you have identified a package you would like to install, you can use the function install.packages()
to do so. For illustration, consider the package RColorBrewer that allows you to create nice color gradients that you can use for plotting.
Once a package is installed, you will need to load it to make use of its functions. You do so with the function library()
.
Note that the function install.packages()
requires the name of a package in quotes (i.e. as a string), while library()
requires it without. Don’t ask me why!
Note also that base R already comes with many packages pre-installed. So you not always need to install a package - you might already have it.
12.0.1 Exercises: Installing packages
See Section 18.0.30 for solutions.
Install the package RColorBrewer on your system. Type
display.brewer.all()
into your console. You see all the color gradients RColorBrewer offers. Pick one you like. Generate a color palette withbrewer.pal
(check the help page on how to use this function) such that it contains 10 different colors.Generate a vector
x
with values -4, -3.9, …, 3.9, 4. Plot the normal density ofx
with mean zero and 10 different standard deviations 0.1, 0.2, …, 0.9, 1, as lines inside one plot. Color each line with its own color from the gradient generated above!