First steps in R

Author

Marie-Hélène Burle

In this section, we take our first few steps in R: we will access the R documentation, see how to set R options, and talk about a few concepts.

Help and documentation

For some general documentation on R, you can run:

help.start()

To get help on a function (e.g. sum), you can run:

help(sum)

Depending on your settings, this will open a documentation for sum in a pager or in your browser.

R settings

Settings are saved in a .Rprofile file. You can edit the file directly in any text editor or from within R.

List all options:

options()

Return the value of a particular option:

getOption("help_type")
[1] "html"

Set an option:

options(help_type = "html")

Assignment

R can accept the equal sign (=) for assignments, but it is more idiomatic to use the assignment sign (<-) whenever you bind a name to a value and to use the equal sign everywhere else.

a <- 3

Once you have bound a name to a value, you can recall the value with that name:

a  # Note that you do not need to use a print() function in R
[1] 3

You can remove an object from the environment by deleting its name:

rm(a)

Let’s confirm that a doesn’t exist anymore in the environment:

a
Error in eval(expr, envir, enclos): object 'a' not found

The garbage collector will take care of deleting the object itself from memory.

Comments

Anything to the left of # is a comment and is ignored by R:

# This is an inline comment

a <- 3  # This is also a comment