Introduction to RStudio

Slides in full screen     Download PDF slides

1 Task

1.1 Change settings

Before you get started, there is an important setting that you should change in RStudio. By default, RStudio will save the workspace of your current session in an .Rdata file. This would allow you to start the next session exactly where you left it by loading the .Rdata file.

This is not a good default. We always want to start R from a clean slate to ensure reproducibility and minimize error potential.

In RStudio go to Tools -> Global Options -> General and

  • Remove the check mark for “Restore .RData into workspace at startup”
  • Never “Save workspace to .RData on exit”

1.2 Create an RStudio project

Create an RStudio project for all the scripts, notes, data, etc. from this workshop:

  1. Create a project in a directory of your choice following the instructions from the slides
  2. Use the Files pane in RStudio to create a basic folder structure in your project which will be filled with files in the next days:
  • Folder data/ for all data files
  • Folder R/ for all R scripts
  • Folder docs/ for other documents (e.g. lecture notes or slides)
  • You can always change the structure of your project later

You can add files to your project either directly in RStudio, or in the file explorer of your operating system.

1.3 Add an R script to the project

  • Create a new R script and save it in the R/ folder of your project
  • Copy and paste the code from below into your script
    • Don’t worry if you don’t understand the code yet, we will learn all this later
  • Run the code in the script line by line. Try both, running code using the Run button (in the top right corner of your script pane) and the keyboard shortcut Ctrl/Cmd + Enter
    • For each line that you run, observe what is happening to the different panes (console, environment, …) in RStudio. Can you explain what is happening?
# Look at the first lines of the iris dataset
head(iris)
# What is the iris dataset -> Call the help
?iris
# How many rows and columns does the data set have?
rownum <- nrow(iris)
colnum <- ncol(iris)
print(paste0("The iris dataset has ", rownum, " rows and ", colnum, " columns."))
# Some summary statistics on the iris data set
summary(iris)

# create a plot
plot(iris$Petal.Length, iris$Petal.Width,
  xlab = "Petal Length",
  ylab = "Petal Width",
  main = "Petal Width vs Petal Length",
  pch = 20,
  col = ifelse(iris$Species == "setosa", "coral1",
    ifelse(iris$Species == "virginica", "cyan4",
      ifelse(iris$Species == "versicolor",
        "darkgoldenrod2", "grey"
      )
    )
  )
)
# add a legend
legend("bottomright", c("setosa", "virginica", "versicolor"),
  col = c("coral1", "cyan4", "darkgoldenrod2"), pch = 20
)

1.4 Extras

  • Go back to the Global options and check out the Appearance section. If you want you can change the look of RStudio there