R/utils-loop.R

Defines functions loop_query

## Retrieves data from the API, using pagination
loop_query <- function(query, jpage, verbose) {
  seq_list <- seq(from = 0, to = jpage, by = 1)

  pages <- list()

  for (i in seq_along(seq_list)) {
    mydata <- jsonlite::fromJSON(paste0(
      query, "&_pageSize=100&_page=",
      seq_list[[i]]
    ), flatten = TRUE)
    if (verbose == TRUE) {
      message("Retrieving page ", seq_list[[i]] + 1, " of ", jpage + 1)
    }
    pages[[seq_list[[i]] + 1]] <- mydata$result$items
  }

  df <- tibble::as_tibble(dplyr::bind_rows(pages))

  df
}

Try the hansard package in your browser

Any scripts or data that you put into this service are public.

hansard documentation built on Nov. 13, 2019, 5:06 p.m.