-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-to-file.R
155 lines (147 loc) · 4.81 KB
/
path-to-file.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
#' Get file path to raw data
#'
#' The raw data from the survey is stored in individual files
#' for each survey language. These are not cleaned or harmonised,
#' as there are small inconsistencies in coding between the languages.
#'
#'
#' @param path Name of file in quotes with extension. If `NULL`,
#' the example files will be listed.
#' @param type type of data to look up. Either "clean" (default) or
#' "raw"
#' @param destination optional string indicating where to copy the file to
#' @param ... other arguments to \code{\link[base]{list.files}}
#'
#' @export
#' @return string of file path
#'
#' @examples
#' gbhs_path_data()
#' gbhs_path_data("114338_en.tsv")
#' head(read.delim(gbhs_path_data("114338_en.tsv")))
#' head(read.delim(gbhs_path_data("114338_en.tsv", "raw")))
#' @source This function is adapted from `readxl::readxl_example()`.
gbhs_path_data <-
function(path = NULL,
type = "clean",
destination = NULL,
...) {
type <- match.arg(type, c("clean", "raw"))
if (is.null(path)) {
list.files(system.file("exdata", type, package = "gbhs"), ...)
} else {
file <-
system.file("exdata",
type,
path,
package = "gbhs",
mustWork = TRUE)
if (!is.null(destination))
open_file(file, destination)
file
}
}
#' Get file path to template files
#'
#' There are two basic type of template files, one descriptive and one with models.
#' These are based on the exploration and testing of the data towards our publicised
#' manuscripts and reports. To run the "model" documents, the corresponding "descriptive"
#' document for that paper must have been previously run.
#'
#' @param type either "descriptive" (default) or "model"
#' @param paper an integer of either 1,2 or 3.
#' @param destination optional string indicating where to copy the file to
#' @export
#' @return string of file path
#' @examples
#' gbhs_path_rmd()
#' gbhs_path_rmd("descriptive", 2)
#' gbhs_path_rmd("model", 3)
#' @source This function is adapted from `readxl::readxl_example()`.
gbhs_path_rmd <-
function(type = "descriptives",
paper = 1,
destination = NULL) {
type <- match.arg(type, c("descriptives", "models"))
type <- sprintf("%s.Rmd", type)
paper <- match.arg(as.character(paper),
as.character(1:3))
paper <- sprintf("paper%s", paper)
file <-
system.file("docs", paper, type, package = "gbhs", mustWork = TRUE)
if (!is.null(destination))
open_file(file, destination)
file
}
#' Get path of child document
#'
#' @param path filename of child document to get path of. If NULL, lists possibilities
#' @param ... other arguments to \code{\link[base]{list.files}}
#'
#' @return string of file path
#' @export
#'
#' @examples
#' gbhs_path_child()
#' gbhs_path_child("bin_desc.Rmd")
#' gbhs_path_child("ord_mod.Rmd")
#' @source This function is adapted from `readxl::readxl_example()`.
gbhs_path_child <- function(path = NULL, ...) {
if (is.null(path)) {
list.files(system.file("docs", "child_docs", package = "gbhs"), ...)
} else {
system.file("docs",
"child_docs",
path,
package = "gbhs",
mustWork = TRUE)
}
}
#' Get path of utility functions
#'
#' @param path filename of utility file to get path of. If NULL, lists possibilities
#' @param ... other arguments to \code{\link[base]{list.files}}
#'
#' @return string of file path
#' @export
#'
#' @examples
#' gbhs_path_utilities()
#' gbhs_path_utilities("data-utils.R")
#' gbhs_path_utilities("model-utils.R")
#' @source This function is adapted from `readxl::readxl_example()`.
gbhs_path_utilities <- function(path = NULL, ...) {
if (is.null(path)) {
list.files(system.file("utils", package = "gbhs"), ...)
} else {
system.file("utils", path, package = "gbhs", mustWork = TRUE)
}
}
#' Get path to meta-data and codebooks
#'
#' @param path filename of utility file to get path of. If NULL, lists possibilities
#' @param type either "codebook" or "meta-data"
#' @param ... other arguments to \code{\link[base]{list.files}}
#'
#' @return string of file path
#' @export
#'
#' @examples
#' gbhs_path_meta()
#' gbhs_path_meta("131674_ch.json")
#' gbhs_path_meta(type = "meta-data")
#' gbhs_path_meta("131674_ch.json", type = "meta-data")
#' @source This function is adapted from `readxl::readxl_example()`.
gbhs_path_meta <- function(path = NULL, type = "codebook", ...) {
type <- match.arg(type, c("codebook", "meta-data"))
if (is.null(path)) {
list.files(system.file(type, package = "gbhs"), ...)
} else {
system.file(type, path, package = "gbhs", mustWork = TRUE)
}
}
#' @importFrom utils file.edit
open_file <- function(path, destination) {
invisible(file.copy(path, destination))
file.edit(destination)
}