Libraries

Loading in my libraries

library(httr2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten

Requesting URL

Requesting the url using the API key to use the API for the world top stories

url <- "https://api.nytimes.com/svc/topstories/v2/world.json?api-key=89hiISQwL7JuDOLqsrrno7ASzC8X3fxv"
req <- request(url)
req
## <httr2_request>
## GET
## https://api.nytimes.com/svc/topstories/v2/world.json?api-key=89hiISQwL7JuDOLqsrrno7ASzC8X3fxv
## Body: empty

Checking the status to make sure there is a connection

resp <- req_perform(req)
resp
## <httr2_response>
## GET
## https://api.nytimes.com/svc/topstories/v2/world.json?api-key=89hiISQwL7JuDOLqsrrno7ASzC8X3fxv
## Status: 200 OK
## Content-Type: application/json
## Body: In memory (73514 bytes)
resp %>% resp_content_type()
## [1] "application/json"

Creating the data as a dataframe

dat <- fromJSON(url) %>% 
  as.data.frame()
dat

Cleaning Data

Getting the column names to only take out the relevant data

colnames(dat)
##  [1] "status"                      "copyright"                  
##  [3] "section"                     "last_updated"               
##  [5] "num_results"                 "results.section"            
##  [7] "results.subsection"          "results.title"              
##  [9] "results.abstract"            "results.url"                
## [11] "results.uri"                 "results.byline"             
## [13] "results.item_type"           "results.updated_date"       
## [15] "results.created_date"        "results.published_date"     
## [17] "results.material_type_facet" "results.kicker"             
## [19] "results.des_facet"           "results.org_facet"          
## [21] "results.per_facet"           "results.geo_facet"          
## [23] "results.multimedia"          "results.short_url"

Using dplyr to get the data

news <- dat %>% 
  select("results.title","results.section","results.byline" )
head(news)

Changing the titles

news_final <- news %>% 
  rename("Article Name" = results.title,
         "Writer" = results.byline,
         "Section" = results.section
         )
news_final