Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script R #217

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

# CHANGELOG

1.7.3 (unreleased)
-----------------

**🚀 Nouveautés**

- Documentation sur la récupération des données via logiciel `R`

1.7.2 (2024-10-04)
-----------------

Expand Down
27 changes: 27 additions & 0 deletions r/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Utilisation des données d'export dans les analyses R


## Récupérer les données du module d'export via R

Le fichier `gn2_get_export_api_data.R` offre des fonctions qui permettent de récupérer les données d'une api du modèle d'export.

Il repose sur la librairie `httr2`.



### Utilisation

Au préalable il faut installer le paquet `httr2` : `install.packages("httr2")`

```R
source("gn2_get_export_api_data.R")

# Paramètres
gn2_token <- "34c9eeb80a54336c60e3fd68cbf715e7"
gn2_export_id <- 165
gn2_url <- "https://URL_GN2/api/exports/api"


# Récupération des données
all_data <- get_gn_export_data(gn2_url, gn2_export_id, gn2_token)
```
32 changes: 32 additions & 0 deletions r/gn2_get_export_api_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(httr2)

get_gn_export_data <- function(url, id_export, token, limit = 1000) {
req <- build_request(url, id_export, token, limit)
resps <- req_perform_iterative(
req,
next_req = iterate_with_offset(
"offset",
start = 0,
offset = 1,
resp_complete = is_complete
),
max_reqs = Inf
)

all_data <- resps_data(
resps, \(resp) resp_body_json(resp, simplifyVector = TRUE)$items
)
return(all_data)
}

is_complete <- function(resp) {
length(resp_body_json(resp)$items) < resp_body_json(resp)$limit
}

build_request <- function(url, id_export, token, limit) {
req <- request(url)
req <- req |> req_url_path_append(id_export)
params <- list(token = token, limit = limit)
req <- req |> req_url_query(!!!params)
return(req)
}
Loading