Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
DSCC
FSO Metadata Auto R
Commits
8a063f48
Commit
8a063f48
authored
Oct 22, 2021
by
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
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
DESCRIPTION
View file @
8a063f48
...
...
@@ -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
NAMESPACE
View file @
8a063f48
...
...
@@ -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)
R/api_call.R
View file @
8a063f48
...
...
@@ -151,7 +151,7 @@ get_nomenclature_path_nodes <- function(identifier,
api_type
=
"nomenclature_path_nodes"
,
id
=
identifier
,
path
=
path
,
parameters
=
string
ify
(
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)}&{string
ify
(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)}&{string
ify
(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}&{string
ify
(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
,
...
...
R/format_request.R
View file @
8a063f48
...
...
@@ -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
R/my_experiment.R
View file @
8a063f48
...
...
@@ -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)}&{string
ify
(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
)
R/utils.R
deleted
100644 → 0
View file @
8402a248
#' 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
}
man/string
ify
.Rd
→
man/
hash_to_
string.Rd
View file @
8a063f48
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/
utils
.R
\name{string
ify
}
\alias{string
ify
}
% 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{
string
ify
(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"))
string
ify
(filters) -> "a=1&b=2&b=3"
hash_to_
string(filters) -> "a=1&b=2&b=3"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment