First we load all necessary packages
library(httr2)
## Warning: package 'httr2' was built under R version 4.3.3
library(httr)
## Warning: package 'httr' was built under R version 4.3.3
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.3.3
We assign the API key we created on NYT developer.
api_nyt <- "VTT0uyVEoyqLOIaRggsdCixwbVl8AzSO"
We specify the endpoint used from the top stories API, es pacifically articles in the real estate section.
realestate_endpoint <- "https://api.nytimes.com/svc/topstories/v2/realestate.json?api-key=VTT0uyVEoyqLOIaRggsdCixwbVl8AzSO"
Next we will use GET function to make a request and transforme the data into raw data object
url <- paste0(realestate_endpoint)
Raw_data <- GET(url)
data <- fromJSON(content(Raw_data, "text"))
## No encoding supplied: defaulting to UTF-8.
articles <- data$results
From the above we can work into setting the parameters of the dataframe and selecting the variables we need to view.
we can use the following code to see how many columns the API returned and keep the variables we need.
colnames(articles)
## [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"
From the above we want to keep Section, Title, abstract and URL. We can do this using the fillowing code.
realestate_articles_df <- subset(articles, select = c(section, title, abstract, url))
rmarkdown::paged_table(realestate_articles_df)