Jazzmatazz
Jazzmatazz

Reputation: 645

How do I pass a numeric vector that is used as part of a function to purrr?

I have a function that I wrote that scrapes JSON from an API and saves the result to my computer. How would I take a numeric vector and pass it to the function to scrape each individual JSON file and save?

scrape_function <- function(period, api_key){

base_url <- "http://www.madeupurl.com/api/figures?"

params <- 
list(
period = period,
response_format = "JSON",
api_key = api_key)

resp <- httr::GET(base_url, params)
  # Save Response in JSON Format
  out <- httr::content(resp, as = "text", encoding = "UTF-8")

  # Read into JSON format
  json <-
    out %>%
    jsonlite::prettify() %>% 
    jsonlite::fromJSON(simplifyDataFrame = TRUE, flatten = TRUE) 

  # Save Raw JSON Output
  jsonlite::write_json(json, here::here("data-raw", "json", paste0("data-", period, ".json" )))

}

I want to run this function for a numeric vector of periods 1 through 28. The result will be the files as outlined in the function. I'm not sure which purrr function to use, as I've only used it for df using map_dfr.

period <- 1:28

Upvotes: 0

Views: 159

Answers (1)

TimTeaFan
TimTeaFan

Reputation: 18551

The simplest way to loop over an integer vector is probably a for loop:

for (x in 1:28) {
 scrape_function(x, api_key)
}

You could translate this in a base R lapply:

lapply(1:28, function(x) {scrape_function(x, api_key)})

Or in a purrr::map call which allows the shorter lambda (~) function notation:

purrr::map(1:28, ~ scrape_function(.x, api_key))

Note that lapply and map will both produce the desired side-effect (of writing the JSON files) and a list as output. If you are only interested in the side-effects you might as well use walk.

purrr::walk(1:28, ~ scrape_function(.x, api_key))

walk not only produces side-effects, it can also return the original object which was passed into it when piping %>% the output into another function or when set visible using (. In our case this would be the integer vector 1:28.

(purrr::walk(1:28, ~ scrape_function(.x, api_key)))

Upvotes: 1

Related Questions