Access over 2 million pieces of content from The Guardian.

Reference Repository

Installation

You can install the package with remotes from Github, see changes.

# install.packages("remotes")
remotes::install_github("news-r/pkg") # github

Setup

To get started, You need a free API key. Then either specify the aforementioned key using guardian_key or specify it as environment variable (likely in your .Renviron) as GUARDIAN_API_KEY.

guardian_key("xxXXxxXx")

Example

The package revolves around the following principle, first create your API calls then execute them with gd_call. This is because the package is built upon the async package which lets you execute API calls asynchonously: the (free) developer plan allows you to do up to 12 calls per second.

Below we look for 15 pages of articles on “Brexit”.

library(guardian)
#> API key loaded!

# search for brexit articles
(to_search <- gd_search("brexit", pages = 15))
#> ℹ 15 calls

# actually execute 15 calls (1 per page)
results <- gd_call(to_search)
#> ℹ Making 15 calls in 2 batches of 12
head(results)
#> # A tibble: 6 x 11
#>   id    type  sectionId sectionName webPublicationD… webTitle webUrl apiUrl
#>   <chr> <chr> <chr>     <chr>       <chr>            <chr>    <chr>  <chr> 
#> 1 poli… arti… politics  Politics    2019-06-18T16:2… Brexit … https… https…
#> 2 game… arti… games     Games       2019-06-26T12:3… Watch D… https… https…
#> 3 poli… arti… politics  Politics    2019-06-24T16:5… Has Bre… https… https…
#> 4 poli… arti… politics  Politics    2019-06-20T17:2… The lim… https… https…
#> 5 educ… arti… education Education   2019-07-02T23:0… Brexit … https… https…
#> 6 busi… arti… business  Business    2019-03-16T17:0… Brexit … https… https…
#> # … with 3 more variables: isHosted <lgl>, pillarId <chr>,
#> #   pillarName <chr>