Commit 78c8c13e authored by Dorchies David's avatar Dorchies David
Browse files

refactor: use internal function convert_list_to_tibble

parent 796f9e03
......@@ -86,8 +86,10 @@ doApiQuery <- function(api,
} else {
l <- httr::content(resp, "parsed")
if (as.numeric(l$count) > 20000) {
stop("The request reach the API limitation of 20000 records.\n",
"Use filter arguments to reduce the number of records of your request.")
stop(
"The request reach the API limitation of 20000 records.\n",
"Use filter arguments to reduce the number of records of your request."
)
}
data <- c(data, l$data)
if (resp$status_code == 206) {
......@@ -104,3 +106,24 @@ doApiQuery <- function(api,
}
return(data)
}
#' Convert list provided by the API into a tibble
#'
#' @param l a [list] provided by the API
#'
#' @return a [tibble:tibble] with one row by list item and one column by list sub-item
#' @noRd
#'
convert_list_to_tibble <- function(l) {
l <-
lapply(l, function(row) {
lapply(row, function(cell) {
if (is.null(unlist(cell)))
NA
else
unlist(cell)
})
})
return(purrr::map_df(l, tibble::as_tibble))
}
......@@ -32,6 +32,5 @@ get_indicateurs_services_communes <- function(params,
x$indicateurs <- NULL
x
})
l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })})
return(purrr::map_df(l, tibble::as_tibble))
convert_list_to_tibble(l)
}
......@@ -26,12 +26,10 @@ get_indicateurs_services_indicateurs <- function(params,
params = params,
cfg = cfg)
li <- lapply(l, function(x) {
l <- lapply(l, function(x) {
x$codes_commune <- NULL
x$noms_commune <- NULL
x
})
li <- lapply(li, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })})
df <- purrr::map_df(li, tibble::as_tibble)
return()
convert_list_to_tibble(l)
}
......@@ -21,6 +21,5 @@ get_prelevements_chroniques <- function(params, cfg = config::get(file = system.
x$geometry <- NULL
x
})
l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })})
return(purrr::map_df(l, tibble::as_tibble))
convert_list_to_tibble(l)
}
......@@ -23,6 +23,5 @@ get_prelevements_ouvrages <- function(params, cfg = config::get(file = system.fi
x
})
l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })})
return(purrr::map_df(l, tibble::as_tibble))
convert_list_to_tibble(l)
}
......@@ -18,6 +18,5 @@ get_prelevements_points_prelevement <- function(params, cfg = config::get(file =
params = params,
cfg = cfg)
l <- lapply(l, function(row) {lapply(row, function(cell) { if(is.null(unlist(cell))) NA else unlist(cell) })})
return(purrr::map_df(l, tibble::as_tibble))
convert_list_to_tibble(l)
}
......@@ -2,7 +2,7 @@
% Please edit documentation in R/get_prelevements_chroniques.R
\name{get_prelevements_chroniques}
\alias{get_prelevements_chroniques}
\title{Retrieve time series of withdrawals from Hub'Eau API}
\title{Retrieve time series of withdrawals from API "Prélèvements en eau"}
\usage{
get_prelevements_chroniques(
params,
......@@ -19,7 +19,7 @@ configuration}
a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns and one row by device, year and usage.
}
\description{
See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-prelevements-eau#/prelevements/chronique}
See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-prelevements-eau}
}
\examples{
# For retrieving the withdrawal time series of the devices located in Romilly-sur-Seine
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment