2
2
3
3
# ' @rdname add_flat_template
4
4
# ' @export
5
- add_additional <- function (
6
- pkg = " ." ,
7
- dev_dir = " dev" ,
8
- flat_name = " additional" ,
9
- overwrite = FALSE ,
10
- open = TRUE ) {
5
+ add_additional <- function (pkg = " ." ,
6
+ dev_dir = " dev" ,
7
+ flat_name = " additional" ,
8
+ overwrite = FALSE ,
9
+ open = TRUE ) {
11
10
add_flat_template(
12
11
template = " additional" ,
13
- pkg = pkg ,
12
+ pkg = pkg ,
14
13
dev_dir = dev_dir ,
15
14
flat_name = flat_name ,
16
15
overwrite = overwrite ,
17
- open = open )
16
+ open = open
17
+ )
18
18
}
19
19
20
20
# ' @rdname add_flat_template
21
21
# ' @export
22
- add_minimal <- function (
23
- pkg = " ." ,
24
- dev_dir = " dev" ,
25
- flat_name = " minimal" ,
26
- overwrite = FALSE ,
27
- open = TRUE ) {
22
+ add_minimal <- function (pkg = " ." ,
23
+ dev_dir = " dev" ,
24
+ flat_name = " minimal" ,
25
+ overwrite = FALSE ,
26
+ open = TRUE ) {
28
27
add_flat_template(
29
28
template = " minimal" ,
30
- pkg = pkg ,
29
+ pkg = pkg ,
31
30
dev_dir = dev_dir ,
32
31
flat_name = flat_name ,
33
32
overwrite = overwrite ,
34
- open = open )
33
+ open = open
34
+ )
35
35
}
36
36
37
37
# ' @rdname add_flat_template
38
38
# ' @export
39
- add_full <- function (
40
- pkg = " ." ,
41
- dev_dir = " dev" ,
42
- flat_name = " full" ,
43
- overwrite = FALSE ,
44
- open = TRUE ) {
39
+ add_full <- function (pkg = " ." ,
40
+ dev_dir = " dev" ,
41
+ flat_name = " full" ,
42
+ overwrite = FALSE ,
43
+ open = TRUE ) {
45
44
add_flat_template(
46
45
template = " full" ,
47
- pkg = pkg ,
46
+ pkg = pkg ,
48
47
dev_dir = dev_dir ,
49
48
flat_name = flat_name ,
50
49
overwrite = overwrite ,
51
- open = open )
50
+ open = open
51
+ )
52
52
}
53
53
54
54
# ' Add flat Rmd file that drives package development
@@ -59,7 +59,7 @@ add_full <- function(
59
59
# ' @param open Logical. Whether to open file after creation
60
60
# ' @param dev_dir Name of directory for development Rmarkdown files. Default to "dev".
61
61
# ' @param flat_name Name of the file to write in dev.
62
- # ' Use the name of the main function of your template to get chunks pre-filled with this function name.
62
+ # ' Use the name of the main function of your template to get chunks pre-filled with this function name.
63
63
# '
64
64
# ' @importFrom tools file_path_sans_ext
65
65
# ' @details
@@ -76,7 +76,7 @@ add_full <- function(
76
76
# '
77
77
# ' Abbreviated names can also be used for the different templates:
78
78
# ' "add" for additional, "min" for minimal, "teach" for teaching, "dev" for "dev_history".
79
- # '
79
+ # '
80
80
# ' `add_additional()`, `add_minimal()`, `add_full()` are wrapper around `add_flat_template("additional")`, ...
81
81
# ' However, `add_dev_history()` is a deprecated function from a previous version.
82
82
# '
@@ -89,62 +89,67 @@ add_full <- function(
89
89
# ' # Create a new project
90
90
# ' dummypackage <- tempfile("dummypackage")
91
91
# ' dir.create(dummypackage)
92
- # '
92
+ # '
93
93
# ' # Add
94
94
# ' add_flat_template(template = "teaching", pkg = dummypackage)
95
95
# ' # Delete dummy package
96
96
# ' unlink(dummypackage, recursive = TRUE)
97
- # '
97
+ # '
98
98
# ' # For classical use in your package
99
99
# ' \dontrun{
100
100
# ' # first time ever using 'fusen'
101
- # ' add_flat_template("full")
102
- # '
101
+ # ' add_flat_template("full")
102
+ # '
103
103
# ' # first time in your new package
104
104
# ' add_flat_template("minimal")
105
- # '
105
+ # '
106
106
# ' # add new flat file for new functions
107
107
# ' add_flat_template("add")
108
- # '
108
+ # '
109
109
# ' # add new flat template for teaching (a reduced full template)
110
110
# ' add_flat_template("teaching")
111
- # ' #'}
112
- add_flat_template <- function (
113
- template = c(" full" , " minimal" , " additional" , " teaching" , " dev_history" ),
114
- pkg = " ." ,
115
- dev_dir = " dev" ,
116
- flat_name = template ,
117
- overwrite = FALSE ,
118
- open = TRUE ) {
119
-
111
+ # ' #'
112
+ # ' }
113
+ add_flat_template <- function (template = c(" full" , " minimal" , " additional" , " teaching" , " dev_history" ),
114
+ pkg = " ." ,
115
+ dev_dir = " dev" ,
116
+ flat_name = template ,
117
+ overwrite = FALSE ,
118
+ open = TRUE ) {
120
119
project_name <- get_pkg_name(pkg = pkg )
121
-
120
+
122
121
if (project_name != asciify_name(project_name , to_pkg = TRUE )) {
123
- stop(" Please rename your project/directory with: `" , asciify_name(project_name , to_pkg = TRUE ),
124
- " ` as a package name should only contain letters, numbers and dots." )
122
+ stop(
123
+ " Please rename your project/directory with: `" , asciify_name(project_name , to_pkg = TRUE ),
124
+ " ` as a package name should only contain letters, numbers and dots."
125
+ )
125
126
}
126
-
127
+
127
128
template <- match.arg(template )
128
- if (! template %in% c(" full" , " teaching" , " dev_history" )
129
- & ! flat_name %in% c(" minimal" , " additional" )) {
129
+ if (! template %in% c(" full" , " teaching" , " dev_history" ) &
130
+ ! flat_name %in% c(" minimal" , " additional" )) {
130
131
fun_name <- gsub(" -" , " _" , asciify_name(flat_name ))
131
132
} else {
132
133
fun_name <- NA
133
134
}
134
- flat_name <- paste0(" flat_" ,
135
- asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd" )
136
-
135
+ flat_name <- paste0(
136
+ " flat_" ,
137
+ asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd"
138
+ )
139
+
137
140
pkg <- normalizePath(pkg )
138
141
full_dev_dir <- file.path(pkg , dev_dir )
139
- if (! dir.exists(full_dev_dir )) {dir.create(full_dev_dir )}
140
- dev_file_path <- file.path(full_dev_dir , flat_name ) # "dev_history.Rmd")
141
-
142
+ if (! dir.exists(full_dev_dir )) {
143
+ dir.create(full_dev_dir )
144
+ }
145
+ dev_file_path <- file.path(full_dev_dir , flat_name ) # "dev_history.Rmd")
146
+
142
147
# Which template ----
143
148
if (template == " dev_history" ) {
144
149
dev_file_path <- character (0 )
145
150
} else {
146
151
template_file <- system.file(paste0(" flat-template-" , template , " .Rmd" ), package = " fusen" )
147
-
152
+
148
153
if (file.exists(dev_file_path ) & overwrite == FALSE ) {
149
154
n <- length(list.files(full_dev_dir , pattern = " ^flat_.*[.]Rmd" ))
150
155
dev_file_path <- file.path(full_dev_dir , paste0(file_path_sans_ext(flat_name ), " _" , n + 1 , " .Rmd" ))
@@ -155,40 +160,50 @@ add_flat_template <- function(
155
160
)
156
161
}
157
162
dev_name <- basename(dev_file_path )
158
-
163
+
159
164
# Change lines asking for pkg name
160
165
lines_template <- readLines(template_file )
161
-
166
+
162
167
lines_template [grepl(" <my_package_name>" , lines_template )] <-
163
- gsub(" <my_package_name>" , project_name ,
164
- lines_template [grepl(" <my_package_name>" , lines_template )])
165
-
168
+ gsub(
169
+ " <my_package_name>" , project_name ,
170
+ lines_template [grepl(" <my_package_name>" , lines_template )]
171
+ )
172
+
166
173
# Change flat_template file name
167
174
# _inflate
168
175
lines_template [grepl(" dev/flat_template.Rmd" , lines_template )] <-
169
- gsub(" dev/flat_template.Rmd" , file.path(dev_dir , dev_name ),
170
- lines_template [grepl(" dev/flat_template.Rmd" , lines_template )])
176
+ gsub(
177
+ " dev/flat_template.Rmd" , file.path(dev_dir , dev_name ),
178
+ lines_template [grepl(" dev/flat_template.Rmd" , lines_template )]
179
+ )
171
180
# _title
172
181
lines_template [grepl(" flat_template.Rmd" , lines_template )] <-
173
- gsub(" flat_template.Rmd" , dev_name ,
174
- lines_template [grepl(" flat_template.Rmd" , lines_template )])
175
-
182
+ gsub(
183
+ " flat_template.Rmd" , dev_name ,
184
+ lines_template [grepl(" flat_template.Rmd" , lines_template )]
185
+ )
186
+
176
187
# Change my_fun to fun_name
177
188
if (! is.na(fun_name )) {
178
189
lines_template [grepl(" my_fun" , lines_template )] <-
179
- gsub(" my_fun" , fun_name ,
180
- lines_template [grepl(" my_fun" , lines_template )])
190
+ gsub(
191
+ " my_fun" , fun_name ,
192
+ lines_template [grepl(" my_fun" , lines_template )]
193
+ )
181
194
}
182
-
195
+
183
196
cat(enc2utf8(lines_template ), file = dev_file_path , sep = " \n " )
184
197
}
185
-
198
+
186
199
# Add the-dev-history when needed ----
187
200
if (template %in% c(" full" , " minimal" , " dev_history" )) {
188
201
dev_file <- file.path(full_dev_dir , " 0-dev_history.Rmd" )
189
202
if (file.exists(dev_file ) & ! isTRUE(overwrite )) {
190
- message(" '0-dev_history.Rmd' already exists. It was not overwritten. " ,
191
- " Set `add_flat_template(overwrite = TRUE)` if you want to do so." )
203
+ message(
204
+ " '0-dev_history.Rmd' already exists. It was not overwritten. " ,
205
+ " Set `add_flat_template(overwrite = TRUE)` if you want to do so."
206
+ )
192
207
} else {
193
208
copy <- file.copy(
194
209
system.file(" the-dev-history.Rmd" , package = " fusen" ),
@@ -200,9 +215,8 @@ add_flat_template <- function(
200
215
}
201
216
dev_file_path <- c(dev_file_path , dev_file )
202
217
}
203
-
204
218
}
205
-
219
+
206
220
# Add data for the full template exemple
207
221
if (template %in% c(" full" )) {
208
222
inst_dir <- file.path(pkg , " inst" )
@@ -213,15 +227,15 @@ add_flat_template <- function(
213
227
# Example dataset
214
228
file.copy(system.file(" nyc_squirrels_sample.csv" , package = " fusen" ), inst_dir )
215
229
}
216
-
230
+
217
231
# .Rbuildignore ----
218
232
# usethis::use_build_ignore(dev_dir) # Cannot be used outside project
219
233
if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
220
234
lines <- c(paste0(" ^" , dev_dir , " $" ), " ^\\ .here$" )
221
235
} else {
222
236
lines <- c(paste0(" ^" , dev_dir , " $" ))
223
237
}
224
-
238
+
225
239
buildfile <- normalizePath(file.path(pkg , " .Rbuildignore" ), mustWork = FALSE )
226
240
if (! file.exists(buildfile )) {
227
241
existing_lines <- " "
@@ -233,11 +247,11 @@ add_flat_template <- function(
233
247
all <- c(existing_lines , new )
234
248
cat(enc2utf8(all ), file = buildfile , sep = " \n " )
235
249
}
236
-
250
+
237
251
# Add a gitignore file in dev_dir ----
238
252
# Files to ignore
239
253
lines <- c(" *.html" , " *.R" )
240
-
254
+
241
255
gitfile <- normalizePath(file.path(full_dev_dir , " .gitignore" ), mustWork = FALSE )
242
256
if (! file.exists(gitfile )) {
243
257
existing_lines <- " "
@@ -249,11 +263,13 @@ add_flat_template <- function(
249
263
all <- c(existing_lines , new )
250
264
cat(enc2utf8(all ), file = gitfile , sep = " \n " )
251
265
}
252
-
266
+
253
267
if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
254
268
here :: set_here(pkg )
255
269
}
256
- if (isTRUE(open ) & interactive()) {usethis :: edit_file(dev_file_path )}
257
-
270
+ if (isTRUE(open ) & interactive()) {
271
+ usethis :: edit_file(dev_file_path )
272
+ }
273
+
258
274
dev_file_path
259
275
}
0 commit comments