-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_asgmts_list.R
212 lines (175 loc) · 6.75 KB
/
get_asgmts_list.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
get_asgmts_list <- function(template_id = NULL, # template id
qx_name = NULL, # questionnaire name
version = NULL, # version
responsible = "", # by default, find all
archived = FALSE, # by default, not archived
output = "df", # options: tab, data frame or excel
output_path = NULL, # output file name
server, # server prefix
user, # API user username
password) # password for API user
{
# -------------------------------------------------------------
# Load all necessary functions and require packages
# -------------------------------------------------------------
load_pkg <- function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, repos = 'https://cloud.r-project.org/', dep = TRUE)
}
require(x, character.only = TRUE)
}
load_pkg('dplyr')
load_pkg('jsonlite')
load_pkg('httr')
load_pkg('tidyr')
load_pkg('readr')
load_pkg('writexl')
load_pkg('here')
# -------------------------------------------------------------
# CHECK ALL INPUTS
# -------------------------------------------------------------
# check that server, login, password are non-missing
for (x in c("server", "user", "password")) {
if (!is.character(get(x))) {
stop(x, "has to be a string.")
}
if (nchar(get(x)) == 0) {
stop(paste("The following parameter is not specified in the program:", x))
}
}
# Check output is a valid output data type
if (tolower(output) %in% c("df", "tab", "excel") == FALSE) {
stop("Output has to be either df (data frame), tab, or excel.")
}
# confirm that output path was specified
if ((tolower(output) %in% c("tab", "excel")) & is.null(output_path)) {
stop("Specify output path for tab or excel output.")
}
# check if archived is logical
if (!is.logical(archived)){
stop("Specify either TRUE or FALSE for archived status.")
}
# check version is numeric
if (!is.numeric(version)) {
if (is.null(version)){
stop("Specify version number.")
} else if (is.na(as.numeric(version))) {
stop("Version number ", version, " is not numeric.")
} else {
version <- as.numeric(version)
}
}
if(is.null(qx_name) & is.null(template_id)){
stop("Either qx_name or template_id must be specified.")
}
# check that not both qx name and template id is specified
if(!is.null(qx_name) & !is.null(template_id)){
stop("Specify only either qx_name or template_id.")
}
# -------------------------------------------------------------
# Get template id if only questionnaire name is specified
# -------------------------------------------------------------
if (!is.null(qx_name) & is.null(template_id)){
# load get_qx
source(here::here("get_qx.R"))
# trim white space around name
qx_name <- trimws(qx_name)
# get the list of questionnaires on the server
all_qx <- get_qx(server=server, user=user,
password=password, put_global=FALSE)
# Get ID of template to get export URL
qx_match <- dplyr::filter(all_qx, Title==qx_name, Version==version)
if (nrow(qx_match)==1) {
qx_id <- qx_match$QuestionnaireIdentity
} else {
stop("Could not find questionnaire on server. Check questionnaire name and version number is correct.")
}
}
# -------------------------------------------------------------
# Call API
# -------------------------------------------------------------
# build base URL for API
server <- tolower(trimws(server))
api_url <- sprintf("https://%s.mysurvey.solutions/api/v1",
server)
# build api endpoint
endpoint <- paste0(api_url, "/assignments")
# build template id for query
# if qx name specified, used qx_id
if (!is.null(template_id)){
qid = paste0(template_id, '$', version)
} else if (!is.null(qx_name)){
qid = qx_id
}
user_query <- list(questionnaireId = qid,
responsible = responsible,
showArchive = archived)
# Send GET request to API
data <- httr::GET(endpoint, authenticate(user, password),
query = user_query)
if (httr::status_code(data)==200){
assignments <- jsonlite::fromJSON(content(data, as = "text"))
# get total count
total_count <- assignments$TotalCount
# get limit
limit <- assignments$Limit
# calculate number of calls that needs to be made
n_calls <- ceiling(total_count/limit)
} else if (httr::status_code(data)==401){
stop("Invalid login or password.")
} else {
stop(paste0("API request failed with code ", status_code(data)))
}
# function to transform the id vars from list into columns
transform_id_vars <- function(df){
Id_vars <- df %>%
dplyr::select(Id, IdentifyingQuestions) %>%
tidyr::unnest(cols = c(IdentifyingQuestions)) %>%
dplyr::select(-Identity) %>%
tidyr::spread(Variable, Answer)
df_with_id <- dplyr::left_join(df, Id_vars, by = 'Id')
df_with_id <- df_with_id %>%
dplyr::select(-ResponsibleId, -QuestionnaireId, -IdentifyingQuestions)
return(df_with_id)
}
# initiate empty list for output
df_list <- list()
# Send post query for all the data
for (i in 1:n_calls){
# build user query with offset
user_query_loop <- list(questionnaireId = qid,
responsible = responsible,
showArchive = archived,
offset = (20 * (i-1))
)
# Send GET request to API
resp <- httr::GET(endpoint, authenticate(user, password),
query = user_query_loop)
# save the list of imported templates from the API as a data frame
assignments <- jsonlite::fromJSON(content(resp, as = "text"))
# if successful, add information to list of data frames
if (httr::status_code(resp)==200){
assignments_temp <- as.data.frame(assignments$Assignments)
# transform identifyng variable from list to column
assignments_id <- transform_id_vars(assignments_temp)
df_list[[i]] <- assignments_id
} else {
stop(paste0("API request failed with code ", status_code(resp)))
}
}
# bind all output together into a big dataframe
if (length(df_list)==1){
all_assignments <- df_list[[1]]
} else {
all_assignments <- dplyr::bind_rows(df_list)
}
if (output == "tab"){
readr::write_tsv(all_assignments, path=output_path)
} else if (output == "excel") {
writexl::write_xlsx(all_assignments, path=output_path,
format_headers=FALSE)
} else {
# return data frame if not exporting to tab or excel
return(all_assignments)
}
}