-
Notifications
You must be signed in to change notification settings - Fork 6
/
dl_similar.R
111 lines (96 loc) · 3.67 KB
/
dl_similar.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
#-------------------------------------------------------#
#------- DOWNLOAD ALL TEMPLATE WITH SIMILAR NAMES ------#
#-------------------------------------------------------#
dl_similar <- function(
pattern, # pattern to search for. Can use regex. can be vector of strings
exclude = NULL, # words to exclude, can be vector of strings
export_type = "tabular", # export type
folder, # directory for data download
unzip = TRUE, # option to unzip after download
server,
user = "APIuser", # API user ID
password = "Password123", # password
tries = 10
)
{
# Load required 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(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("Data folder does not exist in the expected location: ", 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)
# make initial download list based on pattern
if (length(pattern) == 1) {
dl_list <- filter(qnrList_all, str_detect(Title, pattern))
} else if (length(pattern) > 1) {
dl_list <- filter(qnrList_all, str_detect(Title, paste(pattern, collapse = "|")))
} else {
stop("Pattern not specified.")
}
# filter download list to exclude titlesi n the list of words to exclude
if (length(exclude) == 1) {
dl_list <- filter(dl_list, !(str_detect(Title, exclude)))
} else if (length(exclude) > 1) {
dl_list <- filter(dl_list, !(str_detect(Title, paste(exclude, collapse = "|"))))
}
if (nrow(dl_list) == 0) {
stop("Pattern and exclusion words did not result in any matches.")
}
for (qnr in seq_len(nrow(dl_list))) {
# download all items in a list
dl_one(
qx_name = dl_list$Title[qnr],
version = dl_list$Version[qnr],
export_type = export_type,
folder = folder,
unzip = unzip,
server = server,
user = user,
password = password,
tries = tries
)
}
}