TExcelTAD
Home › R › R Get Started

R Get Started

You don't need to install anything to follow this course: every playground runs R in your browser. Still, sooner or later you'll want R on your own computer, so this lesson covers both.

Running R on this site

Type code into any playground and press ▶ Run. The output appears underneath. Try changing the name below and running it again:

r · try it yourself
name <- "Ada"
cat("Hello,", name, "\n")
paste("Welcome to R,", name)
Hello, Ada
[1] "Welcome to R, Ada"

Two ways of producing text are shown here:

  • cat() writes its arguments separated by spaces, exactly as given. It does not add a newline, so we add "\n" ourselves.
  • paste() returns a string. Because that string is not stored anywhere, R auto-prints it, with quotes and the [1] index.

Installing R on your computer

  1. Download R from CRAN (the Comprehensive R Archive Network) at cran.r-project.org. There are installers for Windows, macOS and Linux.
  2. Optionally install an editor. RStudio (by Posit) is the most popular IDE for R; VS Code with the R extension and Positron are also good choices.
  3. Start R. You'll see a console with a > prompt where you can type commands one at a time.
text
R version 4.6.0
> 1 + 1
[1] 2
> sqrt(144)
[1] 12

Scripts and Rscript

Typing into the console is great for experiments. For anything longer, save your code in a file with the .R extension and run the whole file:

bash
Rscript analysis.R

Rscript runs the file from top to bottom and prints each visible value, just like the playgrounds on this site. Inside RStudio you can also run a script with Source or run the current line with Ctrl+Enter.

R as a calculator

The simplest way to start is to use R as a powerful calculator. Each line is evaluated and its result printed:

r · try it yourself
7 + 3
7 * 3
7 / 2
2 ^ 10
(4 + 6) * 3
[1] 10
[1] 21
[1] 3.5
[1] 1024
[1] 30

Getting help

R has built-in documentation for every function. On your own machine you can type:

r
?mean            # help page for mean()
help("paste")    # the same, written out
example(mean)    # run the examples from the help page
apropos("mean")  # functions whose names contain "mean"

The help pages aren't available in the playground, but you can still ask R about objects. class() tells you what kind of value you have, and args() shows the arguments a function accepts:

r · try it yourself
class(42)
class("hello")
args(round)
[1] "numeric"
[1] "character"
function (x, digits = 0, ...)
NULL

Packages

Extra functionality comes in packages. On your computer you install a package once and then load it in each script:

r
install.packages("ggplot2")   # download from CRAN, once
library(ggplot2)              # load it for this session

In this course we stay with base R, which already includes statistics, graphics and dozens of sample datasets.

Test yourself

Where do you download R from?

  1. npm
  2. CRAN
  3. PyPI
  4. Maven Central

What is the difference between cat("hi") and paste("hi") on a line of their own?

  1. None
  2. cat writes the text as-is; paste returns a string that is auto-printed as [1] "hi"
  3. paste writes to a file
  4. cat returns a vector

Which command loads an installed package into the current session?

  1. install.packages()
  2. library()
  3. import()
  4. require.load()