R has been around for ages. It remains popular because it’s easy for people to add to it.

You can run almost any statistical model and produce many different plots in R because users write ‘packages’ which extend the base language. For now we assume someone has helped you install all the packages you need5.

To access features in packages, you normally load the package with the library() function. Running library(<packagename>) loads all the new functions within it, and it is then possible to call them from your code. For example, typing:


Will load the ggplot2 package. You can then call the qplot function it provides:

qplot(mtcars$mpg, bins=7)

It’s good style to load packages at the top of an R script, or in the first chunk of an RMarkdown document. This makes it easy for others to see what packages they need to install, and helps avoid certain sorts of errors in your code.

You don’t strictly need to load packages to use their features. If a package is installed on your system you can also call a function it provides directly. In the example below we call the from the Hmisc package, and obtain histograms of all the variables in the mtcars dataset:

The rule is to type package::function(parameters), where :: separates the package and function names. Parameters are just the inputs to the function.

There are two reasons not to load a package before using it:

  1. Laziness: it can save typing if you just want to use one function from a package, and only once.

  2. Explicitness: It’s an unfortunate truth that some function names are repeated in different packages. This can be confusing if they work differently or do comepletely different things. If you don’t know which package the version you are using comes from. Using package_name:function_name can help make things explicit.

Try using the function in the Hmisc package on the mtcars data.

  • First using the :: syntax
  • Then load the Hmisc package, and repeat without using the ::.

  1. See the installation guide if this isn’t the case