2/18/2019
a6-api-report
Submit Assignment
a6-api-report
(https://canvas.uw.edu/courses/1256484/assignments/4523290#)
Points 100 Submitting a website url
In this challenge, you’ll write the scripts to build a report about congressional representatives using the ProPublica Congress API (https://projects.propublica.org/apidocs/congressapi/) and the Google Civic Information API (https://developers.google.com/civicinformation/) . 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 (https://canvas.uw.edu/courses/1256484/files/52772069/download? verifier=MXWkCEVj7VLRoI0xNlLzr9WmINn2B5QIqBkSaqE1)
(https://canvas.uw.edu/courses/1256484/assignments/4523290#) 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 skillsets: 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 ghpages 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
Due Friday by 10pm
Available Feb 5 at 3:30pm Mar 13 at 3:30pm about 1 month
Overview
file:///Users/ivywu/Desktop/a6-api-report.htm 1/6
2/18/2019 a6-api-report
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 (https://classroom.github.com/a/TgmQjzjf) 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:
which will load (source) the other scripts, and will be compiled into your report
as . 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 (https://support.google.com/cloud/answer/6158862) 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
index.Rmd
index.html
file:///Users/ivywu/Desktop/a6-api-report.htm 2/6
2/18/2019 a6-api-report
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 your index.Rmd file using the kable() 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-num ber-of-replications-for-each
(https://stackoverflow.com/questions/2894775/replicate-each-row-of-data-frame-and-specify-the-number-of-replica tions-for-each)
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….
file:///Users/ivywu/Desktop/a6-api-report.htm 3/6
2/18/2019 a6-api-report
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 twoletter 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
file:///Users/ivywu/Desktop/a6-api-report.htm 4/6
2/18/2019 a6-api-report
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.
Knitting your document
Now that your document is complete, you can use the built in Knit button in RStudio to render to .Rmd file into a .html file. Simply click the Knit button at the top of the page, and your index.html file should be saved in the same directory as your .Rmd file (you'll likely do this repeatedly as you work through the assignment).
Submission
Please submit the URL of your GitHub repository to Canvas. In your README.md file, please include a link to the compiled version of your webpage. Also, make sure to add your api-key.R file to your
file. Because you're working on the ghpages branch, you should be able to see your compiled
file up on the web at info201b-s18.github.io/a5-report-your-username .
.gitignore
index.html
a5reportrubric
file:///Users/ivywu/Desktop/a6-api-report.htm 5/6
2/18/2019 a6-api-report
Criteria
Ratings
Pts
Introduction
Show address Provide links to APIs Use bold Use italics
5.0 pts Full Marks
4.0 pts Minor issues
3.0 pts Some issues
2.0 pts Various issues
0.0 pts No Marks
5.0 pts
Table of Political Representatives
Names are displayed, linked to website Shows photos Replaces photos with NA if missing Links emails Replaces emails with NA if missing Includes Phone, Party, Position
25.0 pts Full Marks
23.0 pts Minor issues
21.0 pts Some issues
19.0 pts Various issues
0.0 pts No Marks
25.0 pts
House of Representatives Charts
Horizontal barplot by gender (appropriate labels, margins) Horizontal barplot by party (appropriate labels, margins)
20.0 pts Full Marks
18.0 pts Minor issues
16.0 pts Some issues
14.0 pts Various issues
0.0 pts No Marks
20.0 pts
Selected Representative Information
Calculates age in years Includes twitter link Calculates recent vote agreement
20.0 pts Full Marks
18.0 pts Minor issues
16.0 pts Some issues
14.0 pts Various issues
0.0 pts No Marks
20.0 pts
Data Wrangling
Data processing is clear, logical, and efficient. Appropriate use of libraries.
10.0 pts Full Marks
9.0 pts Minor issues
8.0 pts Some issues
7.0 pts Various issues
0.0 pts No Marks
10.0 pts
Code Structure
Report regenerates with a different address Hide apikey file using .gitignore Code is well organized into different files/functions Passes all linting tests
20.0 pts Full Marks
18.0 pts Minor issues
16.0 pts Some issues
14.0 pts Various issues
0.0 pts No Marks
20.0 pts
Total Points: 100.0
file:///Users/ivywu/Desktop/a6-api-report.htm
6/6