-
Notifications
You must be signed in to change notification settings - Fork 4
/
sg_get_data.R
63 lines (56 loc) · 2.33 KB
/
sg_get_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#' @title SurveyToGo rest API: Get individual data
#'
#' @description Función para bajar datos y respuestas considerando el límite de
#' 99 casos por envío. Es un envoltorio de la función `sg_get()`.
#'
#' @param api_operation `chr` Bajar datos según operaciones "SimpleExport"
#' o "OperationData".
#' @param interview_ids `int` Vector con códigos de interviews de los que
#' se quiera bajar información.
#' @param ... `list` pasa a query en sg_get
#' @inheritParams sg_get
#'
#' @seealso sg_get
#'
#' @return data.frame
#' @export
#'
#' @examples
sg_get_data <- function(api_operation = c('SimpleExport',
'OperationData'),
interview_ids,
...,
api_key, user, pass,
type = NA) {
if (missing(api_operation)) {
api_operation <- "SimpleExport"
}
if (sum(api_operation %in% c("SimpleExport", "OperationData")) != length(api_operation)) {
stop("\"api_operation\" must be one of: \"SimpleExport\" or \"OperationData\"")
}
query_additional <- list(...)
# Dividir interviews. en grupos de 99 entrevistas.
df_interviews <- data.frame(group = ceiling(seq_along(interview_ids) / 99),
ids = as.integer(unlist(interview_ids)))
# Colapsar el listado de ids a strings de un máximo de 99.
df_interviews <- df_interviews %>%
group_by(.data$group) %>%
summarize(string = stringr::str_c(.data$ids, collapse = ','),
.groups = 'drop')
# Obtener la información de respuesta de cada grupo de interviews.
df_data <- purrr::map(df_interviews$string,
~desuctools::sg_get(api_operation = api_operation,
query = append(list(subjectIDs = .),
query_additional),
user = user,
pass = pass,
api_key = api_key,
type = type))
# Integrar todas las respuestas en una sola base
if(api_operation == 'SimpleExport'){
df_data <- df_data %>%
purrr::modify_depth(.depth = 1, purrr::pluck, 'Subjects') %>%
purrr::flatten()
}
return(df_data)
}