RStudio project

Open the RStudio project that we created in the previous session. We recommend using this RStudio project for the entire course and within the RStudio project create separate R scripts for each session.

  • Create a new empty R script by going to the tab “File”, select “New File” and then “R script”. In the new R script, type # Session 2: Grammar and packages and save the file in your folder “scripts” within your project folder, e.g. as “2_GrammarPackages.R”

1 Grammar: functions and arguments

R comes with a huge list of commands, which we call function. To a function, it needs to be followed by brackets ().

sum()
## [1] 0
sum(1, 3)
## [1] 4

Within the brackets, we can provide additional arguments to the function. Some functions always expect additional arguments, and some functions don’t. In the above example, sum() also works without additional arguments. The function’s default assumes that we have provided a zero vector and thus the output is a 0. If we provide a vector of numeric values, the function will return the sum of these values.

Some functions have named arguments. On example is the function rep(), which creates a vector of replicates (we will learn more about vectors in the next section).

rep(x = 2, times = 10)
##  [1] 2 2 2 2 2 2 2 2 2 2
rep(2, 10)
##  [1] 2 2 2 2 2 2 2 2 2 2

In this example, both expressions give the same results because we provided the arguments in the same order as the function expects. With named arguments, we can also change the order.

rep(times = 10, x = 2)
##  [1] 2 2 2 2 2 2 2 2 2 2
rep(10, 2)
## [1] 10 10

2 Load and install packages

There are thousands of extra packages available for R, containing additional functions. Those functions can only be used if you have their respective package installed. You can see the list of your installed packages by typing: library()

The same command is used for loading a specific package into the workspace = make it available for this session:

# loads the package "ggplot2":
library(ggplot2)
# removes package from current workspace:
detach(package:ggplot2)

If you don’t have the packages above, install them in the package manager or by running the command

install.packages("ggplot2", dependencies = TRUE)

3 Getting help

R comes with an extensive, built-in help system, several manuals, help pages for every available function and many additional documents.

You can access the built-in manuals by typing:

help.start()

Help pages for specific functions can be accessed with help() or with abbreviation ?.

# help page for arithmetic mean:
help(mean)
# the same:
?mean

Most functions come with examples:

example(mean)

Some function calls need to be surrounded by quotation marks when calling help, e.g. special characters or expressions with syntactic meaning such as if, for und function.

?"*"
?"if"

A useful short cut to access the help page for a function, is to press F1 after you selected a function within your code by clicking on it. This will automatically open the respective help page.

If you have forgotten the exact name of a function, you can search all function names, titles and keywords by typing:

help.search("mean")
# the same:
??mean

Search through all (installed) functions that contain the word in their function call:

apropos("mean")

Look at all functions contained in base package by typing:

library(help = base)

Some package authors offer additional documents or tutorials called vignettes:

# list all available vignettes of the installed(!) packages:
vignette()

# opens the vignette as pdf or html:
vignette("ggplot2-specs")

Upon opening a new R session, some useful information will be printed to the console, mainly on version number and copyright. You can also extract this and other useful information by typing commands, e.g.

sessionInfo()
citation()

The last command is important for citing the correct packages in your theses or any other publication.

Exercises:

  • The package ‘ggplot2’ is often used to generate graphics. What is the command to plot a graphic with ggplot2? Is there a useful cheatsheet for ggplot2?

References