Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this challenge, you'll write the scripts to build a report about congressional representatives using the ProPublica Congress API (Links to an external site.)Links to

In this challenge, you'll write the scripts to build a report about congressional representatives using the ProPublica Congress API (Links to an external site.)Links to an external site. and the Google Civic Information API (Links to an external site.)Links to an external site.. The report will provide users information about political representatives based on a selected address. You will to structure your code so that if you change the address in one location in your code, you can rebuild the entire report for a different location (this is one way we will test your code!).

Here is an example

Actions

of what you will build -- you do not need to create a pixel perfect representation of it, but this will give you a sense of what components you'll create. This assignment will evaluate two new skill-sets: using APIs, and creating Markdown documents using knitr. In particular, you'll focus on these areas:

  • Querying data from multiple APIs
  • Rendering R Markdown files as HTML files using knitr
  • Practicing data.frame manipulation and data formatting
  • Hosting a website using GitHub (this will happen automatically when you push your index.html file to your gh-pages branch)

Expectations

This assignment provides far less structure so that you can practice devising your own solutions to coding problems. As in a real world project, you will likely encounter some unexpected frustrations, so plan ahead accordingly, read documentation carefully, and seek help when you're stuck.

At this point in the quarter, we expect you to be able to implement this assignment with well structured R code. This includes, but is not limited to:

  • Code is well formatted: passes all linting tests, clarifies intent with comments
  • Thoughtful use the the dplyr library, and in particular the pipe operator, for working with dataframes
  • Following a consistent process for building an API query, making a GET request, and parsing the returned JSON content

As described above, the expectation is that if you update the address, the entire report will update. This means both structuring your code to be flexible, as well as using inline r markdown to use data to drive information in your report.

Instructions

Set up

As with previous assignments, follow this link (Links to an external site.)Links to an external site. to create your own private repository for this assignment. This will automatically create a private repository which you will submit to Canvas as your assignment. Unlike previous assignments, the repo will not have all necessary starter files. When you clone your repository to your machine, you will create four files:

  • index.Rmd which will load (source) the other scripts, and will be compiled into your report as index.html . All data wrangling / chart building should be done in other files. You should simply display those values in this script.
  • propublica.R which will query the ProPublica API, and store the information/charts you will want to display.
  • civic-info.R which will query the Google Civic Information API, and store the information/charts you will display
  • api-keys.R in which you'll store your API keys (you'll keep these private by adding the filename to your .gitignore file)

A good place to start will the the index.Rmd file: make sure to souce in your other scripts, and begin writing your report. When you get to a value you want to reference or table/chart you want to include, calculate that information in your other scripts (i.e., propublica.R or civic-info.R), then use R Markdown syntax to display the calculated values.

In order to start working, you'll need to register for a key for each API. Here is some additional information (Links to an external site.)Links to an external site. on registering for a key for the Civic Information API. In the appropriate file, you'll complete the following steps (instructions are below, not on GitHub):

Introduction

Your report should begin with an introduction that describes the project. Make sure to include the following:

  • An address that you've selected for the report (you can set this in one of your files)
  • Links to both APIs
  • Bold and Italics to emphasize certain words

Table of Political Representatives

Using the Civic Information API (in your civic-info.R script), you should request the representatives for your address from the /representatives endpoint. The only parameters you will need to specify are your API key and address. Using the returned data, you will create a table (i.e., a data frame that you will then render in yourindex.Rmd file using thekable() function) with the following columns:

  • Name: The name of the representative, which should be hyperlinked to their website (the links are in the data, you'll just need to levereage the appropriate markdown formatting in your dataframe)
  • Position: The current position held (Mayor, Governor, etc.)
  • Party: The party affiliation of the candidate (Democrat, Republican, etc.)
  • Email: The email address of the candidate set up as a link to open an email. If there is no address, you should dispaly "Not available"
  • Phone: The phone number of the candidate
  • Photo*: The photo of the candidate. (the URLs of photos are in the data, you'll just need to levereage the appropriate markdown formatting to display them in your dataframe).

*You'll notice that your photos are different sizes. If you're feeling adventurous (i.e. not required), create a styles.css file and set the height for your images in your table.

I'll warn you that the data wrangling necessary to do this is a bit tricky. You'll need to match information about each official to the office that they hold using the officeIndex (which will correspond to the row number of the official). To do so, this code (an admittedly dense approach) will come in handy (to be described in lab):

# Data wrangling (assuming that `parsed_data` is the parsed JSON response) offices <- parsed_data$offices officials <- parsed_data$officials # Expand officies by the number of elements in the `indices` column # See: https://stackoverflow.com/questions/2894775/replicate-each-row-of-data-frame-and-specify-the-number-of-replications-for-each (Links to an external site.)Links to an external site. num_to_rep <- unlist(lapply(parsed_data$offices$officialIndices, length)) expanded <- offices[rep(row.names(offices), num_to_rep),] officials <- officials %>% mutate(index = row_number() -1) expanded <- expanded %>% mutate(index = row_number() -1) %>% rename(position = name) # Then, join officials and offices by index....

House of Representatives Charts

The second section you create should be on the House of Representatives. Using the ProPublica API(in your propublica.R script), you will get all members of the House of Representatives from the state corresponding to the address indicated. Note: for simplicity's sake, you can just create another variable with the two-letter abbreviation for the state that corresponds with your address.

Begin by making a request for all members of the house of representatives at the /members endpoint. The ProPublica API is a bit particular in how it requires that you make requests. Rather than specify arguments, you'll need to construct a request string. For example, to request members you'll need to construct this URL:

https://api.propublica.org/congress/v1/members/{chamber}/{state}/current.json

You'll replace {state} and {chamber} with your values (the state abbrevaiation, and the word "house" -- removing the curly braces). You'll also need to specify your API key as a header using this syntax (a bit different than what's in the course book):

response <- GET(endpoint, add_headers("X-API-Key" = propublica_key))

Once you have your data, you'll need to calculate the summary information necessary to create two charts (I suggest using ggplot2, meaning that your report won't match the example perfectly):

  • Representatives by Gender: A simple chart showing the number of male v.s. female representatives for the state
  • Representatives by Party: A simple chart showing the number of Democrat v.s. Republican representatives for the state

I suggest that you store each chart in a variable, then simply display that variable in your index.Rmd file.

Selected Representative Information

The final section will be on a specific member from the House of Representatives data (also from the ProPublica API, in your propublica.R script). Using the id of the first of the members that has been returned from your other request, you can make two additional requests about that representative and their voting patterns. You'll want to access these endpoint to get that information:

https://api.propublica.org/congress/v1/members/{member}.json

https://api.propublica.org/congress/v1/members/{member}/votes.json

You'll need to use the same syntax as above to set the header. Using the data returned by those requests, you'll extract and compute necessary variables, then write a paragraph (in your index.Rmd file) that includes the following information:

  • The age of the representatives
  • The link to their twitter account
  • The percentage of time they agree with a vote (i.e., they vote yes on a vote that passes, or they vote no on a vote that fails)

Note, the votes endpoint will only return the most recent 20 votes, which is fine.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions