Lecture 5

RStudio; R Basics

Byeong-Hak Choe

SUNY Geneseo

September 6, 2024

Setting the DANL Tools

Setting the Tools

Posit Cloud

  • Posit Cloud (formerly RStudio Cloud) is a web service that delivers a browser-based experience similar to RStudio, the standard IDE for the R language.

  • For our course, we use Posit Cloud for the R programming component.

    • If you want to install R and RStudio on your laptop, you use my office hours.

Setting the Tools

RStudio Environment

  • Script Pane is where you write R commands in a script file that you can save.
    • An R script is simply a text file containing R commands.
    • RStudio will color-code different elements of your code to make it easier to read.

Setting the Tools

RStudio Environment

  • Console Pane allows you to interact directly with the R interpreter and type commands where R will immediately execute them.

Setting the Tools

RStudio Environment

  • Environment Pane is where you can see the values of variables, data frames, and other objects that are currently stored in memory.

  • Type a <- 1 in Console, and then hit Enter.

Setting the Tools

RStudio Environment

  • Plots Pane contains any graphics that you generate from your R code.

Setting the Tools

R Packages and tidyverse

  • R packages are collections of R functions, compiled code, and data that are combined in a structured format.

  • Several R packages come with numerous pre-built functions that can perform a wide range of data analysis tasks.

Setting the Tools

tidyverse

  • The tidyverse is a collection of R packages designed for data science that share an underlying design philosophy, grammar, and data structures.
  • tidyverse includes a lot of R packages, including ggplot2, dplyr, and tidyr.

  • The tidyverse packages work harmoniously together to make data manipulation, exploration, and visualization more.

Setting the Tools

Installing R packages with install.packages("packageName")

install.packages("tidyverse")
  • R packages can be easily installed from within R using a base-R function install.packages("packageName").
    • To install the R package tidyverse, type and run the following command from R console:
  • While running the above codes, you may encounter the pop-up question, and you can answer “No”

Setting the Tools

Loading R packages with library(packageName)

library(tidyverse)
mpg
  • Once installed, a package is loaded into an R session using a base-R function library(packageName) so that R package’s functions and data can be used.
    • To load the R package tidyverse, type and run the following commands from a R script:
  • mpg is the data.frame provided by the R package ggplot2, one of the R pakcages in tidyverse.
    • Since tidyverse is installed, ggplot2 package is already installed.

Setting the Tools

RStudio Options Setting

  • This option menu is found by menus as follows:
    • Tools \(>\) Global Options
  • Check the boxes as in the left.
  • Choose the option Never for Save workspace to .RData on exit:

Setting the Tools

Workflow: Naming and File Management

  • Save your class R script for each class.
  • I use the following style of file name for class R script:
    • e.g., danl-101-lec-04-2024-0905.R
  • Do not have any space when you name a file.
    • It is recommended to use all lower cases.

Setting the Tools

Workflow: Code and comment style

  • The two main principles for coding and managing data are:
    • Make things easier for your future self.
    • Don’t trust your future self.
  • The # mark is R’s comment character.
    • In R scripts (*.R files), # indicates that the rest of the line is to be ignored.
    • Write comments before the line that you want the comment to apply to.

Setting the DANL Tools

Workflow: Shortcuts in Posit Cloud (RStudio)

  • Windows
    • Alt + - adds an assignment operator
    • Ctrl + Enter runs a current line of code
    • Ctrl + Shift + C makes a comment (#)
    • Ctrl + Shift + R makes a section (# Section - - - -)
  • Mac
    • option + - adds an assignment operator
    • command + return runs a current line of code
    • command + shift + C makes a comment (#)
    • command + shift + R makes a section (# Section - - - -)

Setting the Tools

Workflow: Shortcuts in Posit Cloud (RStudio)

  • Ctrl (command for Mac Users) + Z undoes the previous action.
  • Ctrl (command for Mac Users) + Shift + Z redoes when undo is executed.

Setting the Tools

Workflow: Shortcuts in Posit Cloud (RStudio)

  • Ctrl (command for Mac Users) + F is useful when finding a phrase (and replace the phrase) in the RScript.

Setting the Tools

Workflow: Auto-completion

libr

  • Auto-completion of command is useful.
    • Type libr in the RScript in RStudio and wait for a second.

Setting the Tools

Workflow: STOP icon

  • When the code is running, RStudio shows the STOP icon ( 🛑 ) at the top right corner in the Console Pane.
    • Do not click it unless if you want to stop running the code.

R Basics

R Basics

Values, Variables, and Types

  • A value is datum (literal) such as a number or text.

  • There are different types of values:

    • 352.3 is known as a float or double;
    • 22 is an integer;
    • “Hello World!” is a string.

R Basics

Values, Variables, and Types

a <- 10    # The most popular assignment operator in R is `<-`.
a

  • A variable is a name that refers to a value.
    • We can think of a variable as a box that has a value, or multiple values, packed inside it.
  • A variable is just a name!

R Basics

Objects

  • Sometimes you will hear variables referred to as objects.

  • Everything that is not a literal value, such as 10, is an object.

R Basics

Assignment

x <- 2
x < - 3
  • What is going on here?

  • The shortcut for the assignment <- is:

    • Windows: Alt + -
    • Mac: option + -

R Basics

Assignment

x <- 2
y <- x + 12
  • In programming code, everything on the right side needs to have a value.
    • The right side can be a literal value, or a variable that has already been assigned a value, or a combination.
  • When R reads y <- x + 12, it does the following:
    1. Sees the <- in the middle.
    2. Knows that this is an assignment.
    3. Calculates the right side (gets the value of the object referred to by x and adds it to 12).
    4. Assigns the result to the left-side variable, y.