R/dst_value_limit.R

Defines functions dst_value_limit

#' This is a helper function that returns the number
#' of values the call will return.
#' The API has a limit of 100.000 values.
#'
#' @param query Query object to analyse
#' @param dst_meta Meta data to filer query with
#' @noRd
dst_value_limit <- function(query, dst_meta) {
  # is the element in the list a "*"?
  is_star <- lapply(query, function(x) {
    all(stringr::str_detect(string = x, pattern = "[*]"))
  })

  names(dst_meta$values) <- toupper(names(dst_meta$values))

  # If there are any "*", then replace the query with
  # values from dst_meta.
  if (sum(do.call(rbind, is_star)) > 0) {
    for (i in seq_along(query)) {
      if (is_star[[i]]) {
        query[[i]] <- dst_meta$values[[names(query)[i]]]$text
      }
    }
  }

  # Take the query list and find the length of each
  # element in the list. Convert the resulting list to a vector
  # and calculate the product of the vector.
  query_length <- prod(do.call(rbind, as.vector(lapply(query, length))))

  return(query_length)
}
rOpenGov/dkstat documentation built on Dec. 2, 2024, 3:20 p.m.