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:
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
- Download R from CRAN (the Comprehensive R Archive Network) at
cran.r-project.org. There are installers for Windows, macOS and Linux. - 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.
- Start R. You'll see a console with a
>prompt where you can type commands one at a time.
R version 4.6.0
> 1 + 1
[1] 2
> sqrt(144)
[1] 12Scripts 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:
Rscript analysis.RRscript 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:
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:
?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:
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:
install.packages("ggplot2") # download from CRAN, once
library(ggplot2) # load it for this sessionIn 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?
- npm
- CRAN
- PyPI
- Maven Central
What is the difference between cat("hi") and paste("hi") on a line of their own?
- None
catwrites the text as-is;pastereturns a string that is auto-printed as[1] "hi"pastewrites to a filecatreturns a vector
Which command loads an installed package into the current session?
- install.packages()
- library()
- import()
- require.load()