Module 1: Introduction to RStudio and R Basics

Learning Objectives

After module 1, you should be able to…

  • Create and save an R script
  • Describe the utility and differences b/w the Console and the Source panes
  • Modify R Studio panes
  • Create objects
  • Describe the difference b/w character, numeric, list, and matrix objects
  • Reference objects in the RStudio Environment pane
  • Use basic arithmetic operators in R
  • Use comments within an R script to create header, sections, and make notes

Working with R – RStudio

RStudio is an Integrated Development Environment (IDE) for R

  • It helps the user effectively use R
  • Makes things easier
  • Is NOT a dropdown statistical tool (such as Stata)
RStudio logo

RStudio

Easier working with R

  • Syntax highlighting, code completion, and smart indentation
  • Easily manage multiple working directories and projects

More information

  • Workspace browser and data viewer
  • Plot history, zooming, and flexible image and file export
  • Integrated R help and documentation
  • Searchable command history

RStudio

RStudio

Getting the editor

Working with R in RStudio - 2 major panes:

  1. The Source/Editor:


  • “Analysis” Script

  • Static copy of what you did (reproducibility)

  • Top by default

  1. The R Console: “interprets” whatever you type:
  • Calculator
  • Try things out interactively, then add to your editor
  • Bottom by default

Source / Editor

  • Where files open to
  • Have R code and comments in them
  • Where code is saved

R Console

  • Where code is executed (where things happen)
  • You can type here for things interactively
  • Code is not saved

RStudio

Useful RStudio “cheat sheet”: https://github.com/rstudio/cheatsheets/blob/main/rstudio-ide.pdf

RStudio

RStudio Layout

If RStudio doesn’t look the way you want (or like our RStudio), then do:

In R Studio Menu Bar go to View Menu –> Panes –> Pane Layout

Workspace/Environment

  • Tells you what objects are in R
  • What exists in memory/what is loaded?/what did I read in?

Workspace/History

  • Shows previous commands. Good to look at for debugging, but don’t rely on it.
  • Also type the “up” and “down” key in the Console to scroll through previous commands

Workspace/Other Panes

  • Files - shows the files on your computer of the directory you are working in
  • Viewer - can view data or R objects
  • Help - shows help of R commands
  • Plots - pictures and figures
  • Packages - list of R packages that are loaded in memory

Getting Started

  • In R Studio Menu Bar go to File Menu –> New File –> R Script
  • Save the blank R script as Module1.R

Explaining output on slides

In slides, the R command/code will be in a box, and then directly after it, will be the output of the code starting with [1]

print("I'm code")
[1] "I'm code"

So print("I'm code") is the command and [1] "I'm code" is the output.


Commands/code and output written as inline text will be typewriter blue font. For example code

R as a calculator

You can do basic arithmetic in R, which I surprisingly use all the time.

2 + 2
[1] 4
2 * 4
[1] 8
2^3
[1] 8

R as a calculator

  • The R console is a full calculator
  • Arithmetic operators:
    • +, -, /, * are add, subtract, divide and multiply
    • ^ or ** is power
    • parentheses – ( and ) – work with order of operations
    • %% finds the remainder

Execute / Run Code

To execute or run a line of code (i.e., command), you just put your cursor on the command and then:

  1. Press Run (which you will find at the top of your Console pane)

OR

  1. Press Cmd + Return (iOS) OR Ctrl + Enter (Windows).

To execute or run multiple lines of code, you need to highlight the code you want to run and then follow option 1 or 2.

Mini exercise

Execute 5+4 from your .R file, and then find the answer 9 in the Console.

Commenting in Scripts

The syntax # creates a comment, which means anything to the right of # will not be executed / run

Commenting is useful to:

  1. Create headers for R Scripts
  2. Create sections within an R Script
  3. Explain what is happening in your code

Commenting an R Script header

Add a comment header to Module1.R. This is the one I typically use, but you may have your own preference. The goal is that you are consistent so that future you / collaborators can make sense of your code.

### Title: Module 1
### Author: Amy Winter 
### Objective: Mini Exercise - Developing first R Script
### Date: 15 July 2024

Commenting to create sections

You can also create sections within your code by ending a comment with 4 hash marks. This is very useful for creating an outline of your R Script. The “Outline” can be found in the top right of the your Source pane

# Section 1 Header ####
## Section 2 Sub-header ####
### Section 3 Sub-sub-header ####
#### Section 4 Sub-sub-sub-header ####

Commenting to explain code

## this # is still a comment
### you can use many #'s as you want

# sometimes you have a really long comment,
#    like explaining what you are doing
#    for a step in analysis. 
# Take it to another line

I tend to use:

  • One hash mark with a space to describe what is happening in the following few lines of code
  • One hash mark with no space after a command to list specifics
# Practicing my arithmetic
5+2
3*5
9/8

5+2 #5 plus 2 

Object - Basic terms

Object - an object is something that can be worked with in R - can be lots of different things!

  • a scalar / number
  • a vector
  • a matrix of numbers
  • a list
  • a plot
  • a function

… many more

Objects

  • You can create objects from within the R environment and from files on your computer
  • R uses <- to assign values to an object name
  • Note: Object names are case-sensitive, i.e. X and x are different
  • Here are examples of creating five different objects:
number.object <- 3
character.object <- "blue"
vector.object1 <- c(2,3,4,5)
vector.object2 <- paste0(c("b", "t", "u"), c(8,4,2))
matrix.object <- matrix(data=vector.object1, nrow=2, ncol=2, byrow=TRUE)

Note, c(), paste0(), and matrix() are functions, which we will talk more about in module 2.

Object names - Good coding

  • In general, any object name can be typed into R.
  • However, only some are considered “valid”. If you use a non-valid object name, you will have to enclose it in backticks `like this` for R to recognize it.
  • From the R documentation:

A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number. Names such as “.2way” are not valid, and neither are the reserved words.

  • Reserved words: if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_Complex_, _NA_Character, ..., ..1, ..2, ..3, and so on.

Object names - Good coding

Valid Invalid
my_object my-data
the.vector 2data
num12 for
measles_data .9data
.calc xX~mŷ_δätą~Xx

Object assingment - Good coding

= and <- can both be used for assignment, but <- is better coding practice, because sometimes = doesn’t work and we want to distinguish between the logical operator ==. We will talk about this more, later.

Mini Exercise

Try creating one or two of these objects in your R script

number.object <- 3
character.object <- "blue"
vector.object1 <- c(2,3,4,5)
vector.object2 <- paste0(c("b", "t", "u"), c(8,4,2))
matrix.object <- matrix(data=vector.object1, nrow=2, ncol=2, byrow=TRUE)

Objects

Note, you can find these objects now in your Environment pane.

Also, you can print them anytime (i.e, see them in the Console) by executing (running) the object. For example,

character.object
[1] "blue"
matrix.object
     [,1] [,2]
[1,]    2    3
[2,]    4    5

Lists

List is a special data class, that can hold vectors, strings, matrices, models, list of other lists.

list.object <- list(number.object, vector.object2, matrix.object)
list.object
[[1]]
[1] 3

[[2]]
[1] "b8" "t4" "u2"

[[3]]
     [,1] [,2]
[1,]    2    3
[2,]    4    5

Useful R Studio Shortcuts

Will certainly save you time

  • Cmd + Return (iOS) OR Ctrl + Enter (Windows) in your script evaluates current line/selection
    • It’s like copying and pasting the code into the console for it to run.
  • pressing Up/Down in the Console allows you to navigate command history

See http://www.rstudio.com/ide/docs/using/keyboard_shortcuts for many more

RStudio helps with “tab completion”

If you start typing a object, RStudio will show you options that you can choose without typing out the whole object.

Summary

  • RStudio makes working in R easier
  • The Editor is for static code like R Scripts
  • The Console is for testing code that can’t be saved
  • Commenting is your new best friend
  • In R we create objects that can be viewed in the Environment pane and used anytime
  • An object is something that can be worked with in R
  • Use <- syntax to create objects

Mini Exercise

  1. Create a new number object and name it my.object
  2. Create a vector of 4 numbers and name it my.vector using the c() function
  3. Add my.object and my.vector together using an arithmetic operator

Acknowledgements

These are the materials we looked through, modified, or extracted to complete this module’s lecture.