The R Programming Language and its Packages

Let’s get a brief overview of the R programming language and its packages.

R packages

When you download R, you install a base version that only performs core functions. However, thousands of add-on packages can be downloaded from CRAN that offers additional functionalities. It can be helpful to think of CRAN as an app store for R packages. The packages used in each chapter of this course are provided at the beginning of each chapter (which is the recommended way to write R scripts and R Markdown files). Packages contain functions, which are a key feature of the R programming language. Packages can also contain useful datasets and other resources. You can install and update packages using the RStudio Tools menu. Once installed, we need to activate the package. We can activate a package if we tick the box next to the name of the package in the “Packages” tab, shown in the bottom right section of this figurerstudio. We can also activate a package using the library() function.

The R programming language

We’ll now discuss the key elements of the R programming language.

Functions

Functions are a key element of R. They have names and end in a pair of empty parentheses. For example, as we’ve previously noted, we can use the library() function to activate packages for use. Most of the chapters in this course start with the activation of all of the required packages. Nevertheless, the library() function is often included again to indicate where the package is needed. For example, if we want Darwin’s maize data, we need to activate the SMPracticals package that contains it. Be sure to install that package if you haven’t already done so. The code widget below shows the installation process:

install.packages("SMPracticals",dependencies = TRUE))
library("SMPracticals")

For the sake of convenience, we can use the functions of one package in another. This package, as a result, becomes dependent on the other for these actions. When we install packages, it’s important to always download any others that they may depend on. This is to avoid error messages as a result of missing packages. To add additional packages, tick the relevant menu box or set dependencies = TRUE inside the parentheses of the installation function, as shown above. If we still get an error message that says we’re missing some packages, then we can install that package manually to fix it.

Arguments

In R, arguments are given inside the function parentheses and are separated by commas. In the install.packages() function call above, the first argument is the name of the package to be installed, which is SMPracticals.

Note: The argument needs to go in double quotation marks. If you’re using a text editor, be careful not to substitute these for two single quotation marks or use a format of double quotation marks that R doesn’t recognize.

The second argument instructs R to install any additional packages that are required. Missing commas between arguments are another common cause of errors. Many arguments have the binary TRUE or FALSE values.

Objects

Another important feature of R is that it’s an object-oriented programming language. In this case, the darwin dataset is an object of the class data.frame, as shown below:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy