-
Notifications
You must be signed in to change notification settings - Fork 6
/
dl_everything.R
92 lines (79 loc) · 2.92 KB
/
dl_everything.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
#---------------------------------------------#
#------- DOWNLOAD ALL DATA FROM SERVER ------#
#---------------------------------------------#
dl_everything <- function(
export_type = "tabular", # export type
folder, # directory for data download
unzip = TRUE,
server,
user = "APIuser", # API user ID
password = "Password123", # password
tries = 10
)
{
# -------------------------------------------------------------
# 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('stringr')
load_pkg('jsonlite')
load_pkg('httr')
load_pkg('lubridate')
load_pkg('here')
# source download one function
source(here::here("get_qx.R"))
source(here::here("dl_one.R"))
# -------------------------------------------------------------
# check function inputs
# -------------------------------------------------------------
# check that server, login, password, and data type are non-missing
for (x in c("server", "user", "password", "export_type", "folder")) {
if (!is.character(get(x))) {
stop("Check that the parameters in the data are the correct data type.")
}
if (nchar(get(x)) == 0) {
stop(paste("The following parameter is not specified in the program:", x))
}
}
# Check if it is a valid data type
if ((tolower(export_type) %in% c("tabular", "stata", "spss", "binary", "paradata")) == FALSE) {
stop("Data type has to be one of the following: Tablular, STATA, SPSS, Binary, paradata")
}
# confirm that expected folders exist
if (!dir.exists(folder)) {
stop("Folder does not exist", folder)
}
# build base URL for API
api_URL <- sprintf("https://%s.mysurvey.solutions/api/v1",
server)
# confirm that server exists
serverCheck <- try(http_error(api_URL), silent = TRUE)
if (class(serverCheck) == "try-error") {
stop("The following server does not exist. Check the server name:",
"\n", api_URL)
}
# -------------------------------------------------------------
# Download data
# -------------------------------------------------------------
# First, get questionnaire information from server
get_qx(server, user, password)
for (qnr in seq_len(nrow(qnrList_all))) {
# download all items in a list
dl_one(
qx_name = qnrList_all$Title[qnr],
version = qnrList_all$Version[qnr],
export_type = export_type,
folder = folder,
unzip = unzip,
server = server,
user = user,
password = password,
tries = tries
)
}
}