Getting Started, Part II

R and More Quarto

September 22, 2024

Plan for Today

  • A little more about R
  • Go over three examples on Posit Cloud
  • Discuss advanced use of Quarto
  • Make and publish a Quarto document on Quarto Pub

A Little More 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 vector and printing the result
  • Try using some arithmetic operators
  • Try using some of the common base R functions
03:00

Three Examples

Let’s get going . . .


Let’s open up the Getting Started module on Posit Cloud…

And work through the examples there.

Example: Make a Map!

Example: Make a map!


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

Example: Plotting Democracy Over Time

Example: Plotting Democracy Over Time

# Load the packages
library(vdemlite)
library(ggplot2)

# Use vdemlite to extract democracy scores for France and INdia
dem_data <- fetchdem(indicators = "v2x_polyarchy",
                     countries = c("FRA", "IND"))

# And now we can plot the data
ggplot(dem_data, aes(y = v2x_polyarchy, x = year, color=country_name)) +
  geom_line() +
  theme_minimal() +
  xlab("Year") +
  ylab("Electoral Democracy Index") +
  ggtitle("Electoral Democracy, 1970-2022") +
  geom_hline(yintercept = .5, linetype = "dashed", color = "grey") +
   scale_color_manual(name="Country", values=c("#E69F00", "#56B4E9")) +
  ylim(0, 1)
03:00

More About Quarto

Advanced HTML


  • So far we have been using the basic HTML elements to create our documents
  • Quarto supports a wide range of HTML elements and attributes
  • We can use these to create more complex and interactive documents

Formatting Options

  • We can make our documents have a more sophisticated feel by specifying various options in the YAML header
  • Examples include title, subtitle, date, abstract, toc, etc.
  • We can also specify font styles and colors and control other aspects of the formatting
  • See this page to explore the various formatting options

Themes


  • Quarto supports a wide range of themes
  • We can use these to change the look and feel of our documents
  • Check out this page to explore the various themes available in Quarto

Footnotes


  • You can also add footnotes to your document
  • Use the ^[] or [^1] syntax to add a footnote
  • There are different ways to add footnotes
  • I find that inline notes are the easiest to work with
    • For example ^[This is a footnote]

Generating Lorem Ipsum


  • For working on your document’s format it is helpful to have random text
  • The lorem package is helpful for this
  • You can also install an R Studio Addin

Publishing on Quarto Pub


  • Quarto Pub is a platform for publishing Quarto documents
  • You can publish your documents on Quarto Pub and share them with others
  • Quarto Pub is free to use
  • Sign up for an account, then publish with quarto publish quarto-pub name-of-document.qmd in the terminal

Your Turn!

  • Start a new Quarto project
  • Create a new Quarto document in your project folder
  • Add a YAML header with the following options:
    • title:
    • subtitle:
    • date:
    • date-format:
    • theme:
    • toc:

Your Turn!

  • Try adding at least one more option to your YAML header
  • Generate some sections and some random text using the lorem package
  • Add some code chunks to your document (and some code)
  • Add some footnotes to your document
  • Render the document and see what it looks like
  • Publish to Quarto pub