-
Notifications
You must be signed in to change notification settings - Fork 1
/
boilerplate.R
98 lines (78 loc) · 2.29 KB
/
boilerplate.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
find_pub_dir <- function(pub_id) {
pub_dir <- system.file("publications", package = "allometric")
pub_dir_names <- list.files(pub_dir)
pub_char <- substr(pub_id, 1, 1)
matched_dir <- ""
for(dir_name in pub_dir_names) {
first_char <- substr(dir_name, 1, 1)
second_char <- substr(dir_name, 3, 3)
seq <- letters[(letters >= first_char) & (letters <= second_char)]
if(any(grepl(pub_char, seq, fixed = TRUE))) {
return(dir_name)
}
}
stop(paste("No matching directory found for pub_id:"), pub_id)
}
generate_pub_obj <- function(pub_id, bibtype) {
pub_id_quo <- paste("\"", pub_id, "\"", sep = "")
bibtype_quo <- paste("\"", bibtype, "\"", sep = "")
pub_obj <- paste(c(
paste(pub_id, "<-", "Publication("),
" citation = RefManageR::BibEntry(",
paste(" key = ", pub_id_quo, ",", sep = ""),
paste(" bibtype = ", bibtype_quo, ",", sep = ""),
paste(" title = ", "<title>", ",", sep = ""),
paste(" author = ", "<author>", ",", sep = ""),
paste(" year = ", "<year>", ",", sep = ""),
" )",
")"
), collapse = "\n")
pub_obj
}
generate_fixef_model <- function() {
fixef_model <- paste(c(
"<model> <- FixedEffectsModel(",
" response = list(",
" <res> = units::as_units(<res_unit>)",
" )",
" covariates = list(",
" <covt_unit1> = units::as_units(<covt_unit>)",
" )",
" parameters = list(",
" <parameters>",
" )",
" predict_fn = function(<covts>) {",
" <predict_fn>",
" }",
")"
), collapse = "\n")
fixef_model
}
generate_footer <- function(pub_id, n_fixef_models) {
out <- c(
paste(pub_id, "%>%")
)
for(i in 1:n_fixef_models) {
out <- c(
out,
paste(" add_model(<model_", i, ">)", sep="")
)
}
out
}
generate_pub <- function(models_dir, pub_id, bibtype, n_fixef_models = 0) {
pub_obj <- generate_pub_obj(pub_id, bibtype)
text <- c(pub_obj, "")
if (n_fixef_models > 0) {
for (i in 1:n_fixef_models) {
text <- c(text, generate_fixef_model(), "")
}
text <- c(text, generate_footer(pub_id, n_fixef_models))
}
pub_dir <- find_pub_dir(pub_id)
pub_file_name <- paste(pub_id, ".R", sep = "")
out_path <- file.path(
models_dir, "/publications/", pub_dir, pub_file_name
)
write(text, out_path)
}