This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from Edouard-Legoupil/koboloadeR
/
kobo_data_downloader.R
68 lines (63 loc) · 2.11 KB
/
kobo_data_downloader.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
#' @name kobo_data_downloader
#' @rdname kobo_data_downloader
#' @title Retrieve the Data from a Specified Dataset
#'
#' @description Retrieves the data submitted to a specified dataset.
#'
#' @param formid The ID of the form to be accessed (as a character string).
#' @param user Optional. A single string indicating the username and password
#' (in the form of \code{"username:password"}), or a character vector or list,
#' length 2, with the first value being the "username", and the second being
#' the "password".
#' @param api The URL at which the API can be accessed.
#' Defaults to "unhcr", which loads the UNHCR KoBo Toolbox API.
#' @param check Logical. Should the function first check to see whether the
#' data is available offline.
#' @return A "data.table" with the full dataset. If data is already found on
#' disk and the number of rows matches with the online datasets, the local copy
#' would be used. The dataset would be named in the form of "data_formid".
#'
#' @author Ananda Mahto
#' @examples
#' \dontrun{
#' kobo_data_downloader("15051")
#' kobo_data_downloader("31511", api = "unhcr")
#' }
#'
#' @export kobo_data_downloader
#'
kobo_data_downloader <- function(formid, user = NULL, api = "unhcr", check = TRUE) {
locfile <- sprintf(fmt = "data_%s", formid)
if (isTRUE(check)) {
if (exists(locfile)) {
rows <- nrow(get(locfile))
check <- kobo_submission_count(formid, user, api)
if (rows == check) {
message("Number of rows in local and remote file match.")
redownload = FALSE
} else {
message(sprintf(fmt = "Local file found with %d rows. Remote file contains %d rows.", rows, check))
redownload = TRUE
}
} else {
message("No local dataset found.")
redownload = TRUE
}
} else {
redownload = TRUE
}
if (isTRUE(redownload)) {
message("Downloading remote file.")
URL <- sprintf(fmt = '%sdata/%s.csv', kobo_host(api), formid)
x <- get_me(user, URL)
cat("\n\n")
out <- f_csv(x)
assign(locfile, out #, envir = .GlobalEnv
)
out
} else {
message("Using local file.")
get(locfile)
}
}
NULL