Assignment 6: R APIs and Databases

Author

Brendan Chapuis

Published

April 5, 2027

1 Accept Assignment on Github Classroom

  1. Accept Assignment 6 on Github Classroom.

  2. Clone this assignment to your computer.

  3. Intitialize your own renv environment.

2 Set up “.env” file

  • Copy the “.env.example” file to a new “.env” file1

1 Files starting with a “.” are hidden by default in Finder and File Explorer, but VS Code shows them.

This will be where you will store your personal API keys and passwords, one per line as NAME=value.

I have already added “.env” to the “.gitignore”, so it should not be tracked or pushed to Github.

Load the values into your R session with readRenviron(".env"), then read any one of them with Sys.getenv("NAME").

3 FRED API

3.1 Pull FRED Series

You will need to use the fredr package for this section.

  • Pull the following data series
    • Real GDP: “GDPC1”
    • Rhode Island Unemployment Rate: “RIUR”
    • US-Euro Spot Exchange Rate: “DEXUSEU”
    • One more series of your choice, look around FRED’s website
  • For each series, calculate the following:
    • Mean
    • Median
    • 25th Percentile
    • 75th Percentile
  • Now report the same summary statistics, but only with data since 2020

3.2 Vintage FRED

  • Pull Real GDP using a vintage date

    • There is a vintage_dates argument in the fredr() function.
  • Calculate the difference between the current and the vintage data series (if there is no difference, try another vintage date)

  • Print out the date with the maximum difference

4 tidycensus API

  • Request a Census API key: https://api.census.gov/data/key_signup.html

    • Save it to your “.env” file as CENSUS_API_KEY
  • Install the sf package

  • Install the tidycensus package

  • Pull median household income: “B19013_001” from the ACS2

    • But choose a different state than Rhode Island
  • Create a map with the data and ggplot2

    • Follow the example in the slides

2 Same as the example in the slides.

(Optional) Mapview

If you want to try making one of those interactive maps, install the package mapview and give it a go.

5 Connect to WRDS Database

Warning

This can take 1-2 days to get access.

  • Once you have an account, make sure you can log into WRDS online: https://wrds-www.wharton.upenn.edu

  • Then add your account username and password to the “.env” file as WRDS_USERNAME and WRDS_PASSWORD.3

  • Then open up the “get-wrds-data.r” script

    • The connection to the WRDS database should already work
    • Run the example in the code (same as what we saw in class)
    • Find another database to connect to and pull a subset of the data
      • I’d suggest looking around on the WRDS website to see what data is available

3 Sometimes you have to approve the connection through the Duo app on your phone.

6 Submit

  • Take a renv::snapshot() to update your renv.lock file
  • Commit your code and push to Github!