Commit 8a063f48 authored by Pauline Maury Laribière's avatar Pauline Maury Laribière
Browse files

adding the postprocessing for csv multiple levels

parent 8402a248
Pipeline #270008 passed with stage
in 3 minutes and 10 seconds
......@@ -7,8 +7,9 @@ Depends: R (>= 3.1.0)
License: What license is it under?
LazyData: true
Imports:
dplyr,
glue,
hash,
httr,
jsonlite,
jsonlite
RoxygenNote: 7.1.1
......@@ -18,4 +18,5 @@ export(get_identifier_content)
export(get_nomenclature_multiple_levels)
export(get_nomenclature_one_level)
export(get_nomenclature_path_nodes)
export(hash_to_string)
export(query_nomenclature)
......@@ -151,7 +151,7 @@ get_nomenclature_path_nodes <- function(identifier,
api_type = "nomenclature_path_nodes",
id = identifier,
path = path,
parameters = stringify(filters),
parameters = hash_to_string(filters),
language = language
)
api$get_response()
......@@ -178,7 +178,7 @@ get_nomenclature_one_level <- function(identifier,
level_number = 1,
language = "en",
annotations = FALSE) {
parameters <- glue::glue("language={language}&level={level_number}&annotations={tolower(annotations)}&{stringify(filters)}")
parameters <- glue::glue("language={language}&level={level_number}&annotations={tolower(annotations)}&{hash_to_string(filters)}")
print(parameters)
api <- api_class(
api_type = "nomenclature_one_level",
......@@ -212,17 +212,28 @@ get_nomenclature_multiple_levels <- function(identifier,
level_to = 2,
language = "en",
annotations = FALSE) {
parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{stringify(filters)}")
parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{hash_to_string(filters)}")
api <- api_class(
api_type = "nomenclature_multiple_levels",
id = identifier,
parameters = parameters,
export_format = "CSV"
)
api$get_response()
###############################################################
# TODO: post processing in R
###############################################################
res <- api$get_response()
# Post-processing
# fill sub groups rows with parent group's values (instead of NA)
nb_col <- level_to - level_from + 1
group_columns <- names(res)[1:nb_col]
res[[group_columns[[1]]]] <- data.table::nafill(res[[group_columns[[1]]]], type="locf")
res[group_columns[(level_from + 1):level_to]] <- res %>%
group_by(group_columns[[level_from]]) %>%
subset(select=group_columns[(level_from + 1):level_to]) %>%
data.table::nafill(type="locf")
res
}
......@@ -247,7 +258,7 @@ query_nomenclature <- function(identifier,
page_size,
filters,
language = "en") {
parameters <- glue::glue("query={URLencode(query)}&page={page_number}&pageSize={page_size}&language={language}&{stringify(filters)}")
parameters <- glue::glue("query={URLencode(query)}&page={page_number}&pageSize={page_size}&language={language}&{hash_to_string(filters)}")
api <- api_class(
api_type = "nomenclature_search",
id = identifier,
......
......@@ -32,3 +32,26 @@ REQUEST_FUNCTION_MAPPING <- hash::hash(
"JSON" = json_request,
"CSV" = csv_request
)
#' Transform a hash object into a string of parameters
#'
#' @param filters hash object
#'
#' @return formatted string of parameters
#' @examples
#' filters <- hash::hash("a" = list("1"), "b" = list("2", "3"))
#' hash_to_string(filters) -> "a=1&b=2&b=3"
hash_to_string <- function(filters) {
string <- ""
for (prop in ls(filters)) {
for (value in filters[[prop]]) {
if (string == "") {
string <- glue::glue("{prop}={value}")
} else {
string <- paste(string, glue::glue("{prop}={value}"), sep = "&")
}
}
}
string
}
\ No newline at end of file
......@@ -3,12 +3,9 @@ language <- 'fr'
level_from <- 1
level_to <- 4
annotations <- FALSE
my_filters <- hash::hash(
'prop1'= list("string"),
'prop2' = list("string", "string2")
)
my_filters <- hash::hash('prop1'= list("string"))
parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{stringify(my_filters)}")
parameters <- glue::glue("language={language}&levelFrom={level_from}&levelTo={level_to}&annotations={tolower(annotations)}&{hash_to_string(my_filters)}")
print(parameters)
......@@ -20,27 +17,15 @@ api <- api_class(
)
res <- api$get_response()
names(res)
nb_col <- level_to - level_from + 1
group_columns <- names(res)[1:nb_col]
group_columns
group_columns[[1]]
res[[group_columns[[1]]]] = data.table::nafill(res[[group_columns[[1]]]], type="locf")
res[[group_columns[[1]]]]
# df[group_columns[1:]] = (
# df.groupby([group_columns[0]])[group_columns[1:]]
# ).apply(lambda x: x.ffill())
group_columns[[1]] # group_columns[0]
group_columns[2:4] # df[group_columns[1:]]
res
res[[group_columns[[1]]]] <- data.table::nafill(res[[group_columns[[1]]]], type="locf")
res[group_columns[(level_from + 1):level_to]] <- res %>%
group_by(group_columns[[level_from]]) %>%
subset(select=group_columns[(level_from + 1):level_to]) %>%
data.table::nafill(type="locf")
str(res)
#' Transform a hash object into a string of parameters
#'
#' @param filters hash object
#'
#' @return formatted string of parameters
#'
#' @examples
#' filters <- hash::hash("a" = list("1"), "b" = list("2", "3"))
#' stringify(filters) -> "a=1&b=2&b=3"
stringify <- function(filters) {
string <- ""
for (prop in ls(filters)) {
for (value in filters[[prop]]) {
if (string == "") {
string <- glue::glue("{prop}={value}")
} else {
string <- paste(string, glue("{prop}={value}"), sep = "&")
}
}
}
string
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{stringify}
\alias{stringify}
% Please edit documentation in R/format_request.R
\name{hash_to_string}
\alias{hash_to_string}
\title{Transform a hash object into a string of parameters}
\usage{
stringify(filters)
hash_to_string(filters)
}
\arguments{
\item{filters}{hash object}
......@@ -17,5 +17,5 @@ Transform a hash object into a string of parameters
}
\examples{
filters <- hash::hash("a" = list("1"), "b" = list("2", "3"))
stringify(filters) -> "a=1&b=2&b=3"
hash_to_string(filters) -> "a=1&b=2&b=3"
}
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