The New York Times web site provides a rich set of APIs, as described here:https://developer.nytimes.com/apis We started by signing up for an API key.The task was 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.
Chose the topstories from the HTTP Request section of https://developer.nytimes.com/ Below we imported the apikey and path into respective objects We created the initial dataframe ‘magazine_df’
apikey <- "CwEonEZVnTIZET6h3EHtAKCZF1PmchR7"
url <- paste0("https://api.nytimes.com/svc/topstories/v2/health.json", "?api-key=", apikey)
magazine <- GET(url)
# names of the arrays from the API call
magazine_text <- content(magazine, "text")
## No encoding supplied: defaulting to UTF-8.
magazine_df <- data.frame(fromJSON(magazine_text))
Below we take a brief look at the column names currently of the dataframe
(c_names <- names(magazine_df))
## [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"
Below we tidy/organize the data by selecting the specific information we would like to display We label our column names as desired to match the content
FilteredData = magazine_df[, c("results.section", "results.title","results.abstract","results.url" )]
colnames(FilteredData) = c("Section","Title","Abstract","Link")
colnames(FilteredData)
## [1] "Section" "Title" "Abstract" "Link"
str(FilteredData)
## 'data.frame': 25 obs. of 4 variables:
## $ Section : chr "style" "health" "us" "science" ...
## $ Title : chr "Why Are TikTok Teens Listening to an Album About Dementia?" "Experts Tell F.D.A. It Should Gather More Safety Data on Covid-19 Vaccines" "F.D.A. Approves Remdesivir as First Drug to Treat Covid-19" "No Grumpy Old Men in the World of Chimps" ...
## $ Abstract: chr "Creepypasta meets esoterica in an ongoing meme." "In a highly anticipated meeting of the agency’s vaccine advisory board, some said that the current guidelines, "| __truncated__ "The move indicated that the drug had cleared more rigorous hurdles since it was given emergency authorization in May." "Older male chimps follow a pattern that researchers also see in humans, preferring to have positive relationshi"| __truncated__ ...
## $ Link : chr "https://www.nytimes.com/2020/10/23/style/tiktok-caretaker-challenge-dementia.html" "https://www.nytimes.com/2020/10/22/health/covid-vaccine-fda-advisory-committee.html" "https://www.nytimes.com/2020/10/22/us/remdesivir-fda-approved.html" "https://www.nytimes.com/2020/10/22/science/aging-chimps-friendship.html" ...
Using ‘datatable’ function we pull in our final ‘FilteredData’ frame to organize the information to be displayed. We can also customize coloring of borders as well as alignment here,
datatable(FilteredData, colnames = c('Entry #', 'Section', 'Title', 'Abstract', 'Link'), class = 'cell-border stripe', options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#5a5a5a', 'color': '#fff', 'text-align': 'center !important'});",
"$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
"}")
))
The initial dataframe created after the importing of the API path and url with key is heavily untidy. We need the help of a few tools in order to structure the data. Using library DT ‘datatable’ it makes it much easier to create a pleasant visual distribution of the data including search option