Assignment: The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.

Step 1(Library): Trying to use tidyverse/stringr/jsonlite/ggplot to data tidy and structure the data. Then I use jsonlite to do data import, and ggplot to finally visualize the data. https://api.nytimes.com/svc/topstories/v2/science.json?api-key=NU6COmw5yo6fGGMCpsTH4tGYqulDzKLd My API Key: NU6COmw5yo6fGGMCpsTH4tGYqulDzKLd

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
library(stringr)
library(ggplot2)

I will focus on using Top Stories API to filter my favorite section: Science.

my_api <- "https://api.nytimes.com/svc/topstories/v2/science.json?api-key=NU6COmw5yo6fGGMCpsTH4tGYqulDzKLd"
json_object <- fromJSON(my_api)
science_df <- json_object$results
class(science_df)
## [1] "data.frame"

I begin by getting my API key. I proceed with reading JSON data from API using json_object <- FromJSON. Once the data is JSON this can be converted to a DF. #Data Maniupuation

colnames(science_df)
##  [1] "section"             "subsection"          "title"              
##  [4] "abstract"            "url"                 "uri"                
##  [7] "byline"              "item_type"           "updated_date"       
## [10] "created_date"        "published_date"      "material_type_facet"
## [13] "kicker"              "des_facet"           "org_facet"          
## [16] "per_facet"           "geo_facet"           "multimedia"         
## [19] "short_url"
science_df <- subset(science_df, select =c(section,title,abstract))
head(science_df)
##   section
## 1 science
## 2   admin
## 3      us
## 4 climate
## 5 climate
## 6  health
##                                                                             title
## 1                                        Sign Up for the Science Times Newsletter
## 2                                                                                
## 3        Pentagon’s Strategy Says China and Russia Pose Very Different Challenges
## 4 A Power Balance Shifts as Europe, Facing a Gas Crisis, Turns to Africa for Help
## 5    War in Ukraine Likely to Speed, Not Slow, Shift to Clean Energy, I.E.A. Says
## 6                                          W.H.O. Lists Top Fungal Health Threats
##                                                                                                                                                                                          abstract
## 1                                                                                          Every week, we’ll bring you stories that capture the wonders of the human body, nature and the cosmos.
## 2                                                                                                                                                                                                
## 3                                             A new document describes the military’s response to a new era in broad terms and guides Pentagon policy and budget decisions, but it lacks details.
## 4                                                                        Officials from Algeria to Mozambique say they hope to take advantage of an abrupt change in a long-unequal relationship.
## 5                     While some nations are burning more coal this year in response to natural-gas shortages spurred by Russia’s invasion of Ukraine, that effect is expected to be short-lived.
## 6 The pathogens cause infections that kill millions of people each year and often go undiagnosed. Even when identified, a growing number of infections is resistant to the current crop of drugs.

I went to use tidy data to show the science section with title and abstract so the viewer can see which article’s title interested them. Once they find the title, the abstract can give a good overview.