<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/examining-potus-executive-orders/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/04/18/examining-potus-executive-orders/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This week’s edition of <a rel="nofollow" href="https://tinyletter.com/data-is-plural/letters/data-is-plural-2018-04-18-edition" target="_blank">Data is Plural</a> had two <em>really</em> fun data sets. One is serious fun (the first comprehensive data set on U.S. <a rel="nofollow" href="https://evictionlab.org/map/#/2016?geography=states" target="_blank">evictions</a>, and the other I knew about but had forgotten: The <a rel="nofollow" href="https://www.federalregister.gov/executive-orders" target="_blank">Federal Register Executive Order (EO) data set(s)</a>.</p>
<p>The EO data is also comprehensive as the summary JSON (or CSV) files have links to more metadata and even more links to the full-text in various formats.</p>
<p>What follows is a quick post to help bootstrap folks who may want to do some tidy text mining on this data. We’ll look at EOs-per-year (per-POTUS) and also take a look at the “top 5 ‘first words’” in the titles of the EOS (also by POTUS).</p>
<h3>Ingesting the Data</h3>
<p>The EO main page has a list of EO JSON files by POTUS. We’re going to scrape this so we can classify the EOs by POTUS (we could also just use the Federal Register API since <a rel="nofollow" href="http://twitter.com/thosjleeper" target="_blank">@thosjleeper</a> wrote a <a rel="nofollow" href="https://cran.r-project.org/web/packages/federalregister/index.html" target="_blank">spiffy package</a> to access it):</p>
<pre><code>library(rvest)
library(stringi)
library(pluralize) # devtools::install_github(&quot;hrbrmstr/pluralize&quot;)
library(hrbrthemes)
library(tidyverse)

#' Retrieve the Federal Register main EO page so we can get the links for each POTUS
pg &lt;- read_html(&quot;https://www.federalregister.gov/executive-orders&quot;) 

#' Find the POTUS EO data nodes, excluding the one for &quot;All&quot;
html_nodes(pg, &quot;ul.bulk-files&quot;) %>% 
  html_nodes(xpath = &quot;.//li[span[a[contains(@href, 'json')]] and 
                            not(span[contains(., 'All')])]&quot;) -> potus_nodes

#' Turn the POTUS info into a data frame with the POTUS name and EO JSON link,
#' then retrieve the JSON file and make a data frame of individual data elements
data_frame(
  potus = html_nodes(potus_nodes, &quot;span:nth-of-type(1)&quot;) %>% html_text(),
  eo_link = html_nodes(potus_nodes, &quot;a[href *= 'json']&quot;) %>% 
    html_attr(&quot;href&quot;) %>% 
    sprintf(&quot;https://www.federalregister.gov%s&quot;, .)
) %>% 
  mutate(eo = map(eo_link, jsonlite::fromJSON)) %>% 
  mutate(eo = map(eo, &quot;results&quot;)) %>% 
  unnest() -> eo_df

glimpse(eo_df)
## Observations: 887
## Variables: 16
## $ potus                  </code></pre>