Data Rich Reports

Session 2–Coding in R

A Bit About R

What is an Object?

  • An object in R is a data structure used to store data
  • It can vary from simple scalar types to more complex data structures like vectors, lists, or data frames
  • Objects hold not only data but also information about the type of data and the operations that can be performed on them
  • Every entity in R is considered an object, making R a language based around the manipulation of objects

How to Store Data

  • In R, you can store data in objects using the assignment operator <-
  • The object name is on the left of <-, and the data or value you wish to assign to the object is on the right
  • Then you can print the object to the console using the object name
# Store the value 42 in the object my_number
my_number <- 42

# Print the value of my_number
my_number 
[1] 42

What Can R Do?


  • R is a powerful language for data analysis and visualization
  • It is also a general-purpose programming language
  • It can be used for web development, machine learning, and more
  • It is open-source and has a large community of users and developers

R as a Calculator


  • R can be used as a simple calculator
  • You can perform arithmetic operations on numbers
# Addi a number and store it to a value
sum_of_2plus2 <- 2 + 2


sum_of_2plus2
[1] 4

When to Store Data in Objects


  • Note that you don’t always have to store data in objects
  • You should mostly store data in objects when you want to use the data later
  • If you only need to use the data once, you can just use the data directly
# Add two numbers without storing them in an object
2 + 2
[1] 4

Some Common Arithmetic Operators


  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ^ exponentiation (also **)

Functions

  • A function is a set of instructions that produces some output
  • In R, you can use built-in functions to perform specific tasks
  • For example, you can use the mean() function to calculate the average of a set of numbers
  • To do this you have to use the combine function c() to create a vector of numbers


Create a vector of numbers and take the mean…


# Create a vector of numbers
numbers <- c(1, 2, 3, 4, 5)

# Calculate the mean of the numbers
mean(numbers)
[1] 3

Some Common Base R Functions

  • mean() calculates the mean of a set of numbers
  • median() calculates the median of a set of numbers
  • sd() calculates the standard deviation of a set of numbers
  • sum() calculates the sum of a set of numbers
  • length() calculates the length of a vector
  • max() and min() calculate the maximum and minimum values of a vector
  • round() rounds a number to a specified number of decimal places
  • sqrt() calculates the square root of a number
  • log() calculates the natural logarithm of a number
  • exp() calculates the exponential of a number
  • abs() calculates the absolute value of a number

Your Turn!


  • Start a new code chunk your Quarto document
  • Try storing some numbers as a function and printing the result
  • Try using some arithmetic operators
  • Try using some of the common base R functions
03:00

R Packages and Functions


  • A function is a set of instructions
    • read_csv() is a function
    • ggplot() is a function
  • A package is a collection of functions
    • readr is a package that contains the read_csv() function
    • ggplot2 is a package that contains the ggplot() function
  • Use install.packages() to install packages
  • Use library() to load packages
  • You can install packages from CRAN

Install key packages

  • Install the Tidyverse group of packages from the console
    • install.packages("tidyverse")
  • Install devtools
    • install.packages("devtools")
  • Install vdemdata
    • devtools::install_github("vdeminstitute/vdemdata")
  • Install leaflet
    • install.packages("leaflet")
  • Install tinytex (for PDF rendering)
    • Go to your terminal and type quarto install tinytex

Illustration

03:00

The Tidyverse

  • The Tidyverse is a collection of data science packages
  • It is also considered a dialect of R
  • In this class, we will be using many Tidyverse packages
    • ggplot2 for data visualization
    • readr for reading data
    • dplyr for data manipulation
    • tidyr for data tidying
    • Etc.
  • At first we will load the packages independently, e.g. library(ggplot2)
  • Later we will load them all at once with library(tidyverse)

Your first data visualization…

  • Create a code chunk
  • Copy this code chunk into your document
library(ggplot2)

ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()
  • Try changing echo to false
  • Then render it again
02:00

Did it Work?

Make a map…

Make a map…

```{r}
#| label: leaflet_map

library(leaflet)
leaflet() %>% 
  addTiles() %>%   # Add default OpenStreetMap map tiles
  addMarkers(lat = 38.90243843683386, lng =  -77.0443814477152, 
             label = "Elliott School of International Affairs")
```

Try it yourself!

  • Create a code chunk
  • Copy this code into the chunk
library(leaflet)
leaflet() %>% 
  addTiles() %>%   # Add default OpenStreetMap map tiles
  addMarkers(lat = 38.90243843683386, lng =  -77.0443814477152, 
             label = "Elliott School of International Affairs")
  • Run the chunk
  • Change the coordinates and run again
  • Try rendering the document
02:00