2
2
3
3
# ' @rdname add_flat_template
4
4
# ' @export
5
- add_additional <- function (pkg = " ." ,
6
- dev_dir = " dev" ,
7
- flat_name = " additional" ,
8
- overwrite = FALSE ,
9
- open = TRUE ) {
5
+ add_additional <- function (
6
+ pkg = " ." ,
7
+ dev_dir = " dev" ,
8
+ flat_name = " additional" ,
9
+ overwrite = FALSE ,
10
+ open = TRUE ) {
10
11
add_flat_template(
11
12
template = " additional" ,
12
- pkg = pkg ,
13
+ pkg = pkg ,
13
14
dev_dir = dev_dir ,
14
15
flat_name = flat_name ,
15
16
overwrite = overwrite ,
16
- open = open
17
- )
17
+ open = open )
18
18
}
19
19
20
20
# ' @rdname add_flat_template
21
21
# ' @export
22
- add_minimal <- function (pkg = " ." ,
23
- dev_dir = " dev" ,
24
- flat_name = " minimal" ,
25
- overwrite = FALSE ,
26
- open = TRUE ) {
22
+ add_minimal <- function (
23
+ pkg = " ." ,
24
+ dev_dir = " dev" ,
25
+ flat_name = " minimal" ,
26
+ overwrite = FALSE ,
27
+ open = TRUE ) {
27
28
add_flat_template(
28
29
template = " minimal" ,
29
- pkg = pkg ,
30
+ pkg = pkg ,
30
31
dev_dir = dev_dir ,
31
32
flat_name = flat_name ,
32
33
overwrite = overwrite ,
33
- open = open
34
- )
34
+ open = open )
35
35
}
36
36
37
37
# ' @rdname add_flat_template
38
38
# ' @export
39
- add_full <- function (pkg = " ." ,
40
- dev_dir = " dev" ,
41
- flat_name = " full" ,
42
- overwrite = FALSE ,
43
- open = TRUE ) {
39
+ add_full <- function (
40
+ pkg = " ." ,
41
+ dev_dir = " dev" ,
42
+ flat_name = " full" ,
43
+ overwrite = FALSE ,
44
+ open = TRUE ) {
44
45
add_flat_template(
45
46
template = " full" ,
46
- pkg = pkg ,
47
+ pkg = pkg ,
47
48
dev_dir = dev_dir ,
48
49
flat_name = flat_name ,
49
50
overwrite = overwrite ,
50
- open = open
51
- )
51
+ open = open )
52
52
}
53
53
54
54
# ' Add flat Rmd file that drives package development
@@ -59,7 +59,7 @@ add_full <- function(pkg = ".",
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(pkg = ".",
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,67 +89,62 @@ add_full <- function(pkg = ".",
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
- # ' }
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 ) {
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
+
119
120
project_name <- get_pkg_name(pkg = pkg )
120
-
121
+
121
122
if (project_name != asciify_name(project_name , to_pkg = TRUE )) {
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
- )
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." )
126
125
}
127
-
126
+
128
127
template <- match.arg(template )
129
- if (! template %in% c(" full" , " teaching" , " dev_history" ) &
130
- ! flat_name %in% c(" minimal" , " additional" )) {
128
+ if (! template %in% c(" full" , " teaching" , " dev_history" )
129
+ & ! flat_name %in% c(" minimal" , " additional" )) {
131
130
fun_name <- gsub(" -" , " _" , asciify_name(flat_name ))
132
131
} else {
133
132
fun_name <- NA
134
133
}
135
- flat_name <- paste0(
136
- " flat_" ,
137
- asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd"
138
- )
139
-
134
+ flat_name <- paste0(" flat_" ,
135
+ asciify_name(gsub(" [.]Rmd$" , " " , flat_name [1 ])), " .Rmd" )
136
+
140
137
pkg <- normalizePath(pkg )
141
138
full_dev_dir <- file.path(pkg , dev_dir )
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
-
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
+
147
142
# Which template ----
148
143
if (template == " dev_history" ) {
149
144
dev_file_path <- character (0 )
150
145
} else {
151
146
template_file <- system.file(paste0(" flat-template-" , template , " .Rmd" ), package = " fusen" )
152
-
147
+
153
148
if (file.exists(dev_file_path ) & overwrite == FALSE ) {
154
149
n <- length(list.files(full_dev_dir , pattern = " ^flat_.*[.]Rmd" ))
155
150
dev_file_path <- file.path(full_dev_dir , paste0(file_path_sans_ext(flat_name ), " _" , n + 1 , " .Rmd" ))
@@ -160,50 +155,40 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
160
155
)
161
156
}
162
157
dev_name <- basename(dev_file_path )
163
-
158
+
164
159
# Change lines asking for pkg name
165
160
lines_template <- readLines(template_file )
166
-
161
+
167
162
lines_template [grepl(" <my_package_name>" , lines_template )] <-
168
- gsub(
169
- " <my_package_name>" , project_name ,
170
- lines_template [grepl(" <my_package_name>" , lines_template )]
171
- )
172
-
163
+ gsub(" <my_package_name>" , project_name ,
164
+ lines_template [grepl(" <my_package_name>" , lines_template )])
165
+
173
166
# Change flat_template file name
174
167
# _inflate
175
168
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
- )
169
+ gsub(" dev/flat_template.Rmd" , file.path(dev_dir , dev_name ),
170
+ lines_template [grepl(" dev/flat_template.Rmd" , lines_template )])
180
171
# _title
181
172
lines_template [grepl(" flat_template.Rmd" , lines_template )] <-
182
- gsub(
183
- " flat_template.Rmd" , dev_name ,
184
- lines_template [grepl(" flat_template.Rmd" , lines_template )]
185
- )
186
-
173
+ gsub(" flat_template.Rmd" , dev_name ,
174
+ lines_template [grepl(" flat_template.Rmd" , lines_template )])
175
+
187
176
# Change my_fun to fun_name
188
177
if (! is.na(fun_name )) {
189
178
lines_template [grepl(" my_fun" , lines_template )] <-
190
- gsub(
191
- " my_fun" , fun_name ,
192
- lines_template [grepl(" my_fun" , lines_template )]
193
- )
179
+ gsub(" my_fun" , fun_name ,
180
+ lines_template [grepl(" my_fun" , lines_template )])
194
181
}
195
-
182
+
196
183
cat(enc2utf8(lines_template ), file = dev_file_path , sep = " \n " )
197
184
}
198
-
185
+
199
186
# Add the-dev-history when needed ----
200
187
if (template %in% c(" full" , " minimal" , " dev_history" )) {
201
188
dev_file <- file.path(full_dev_dir , " 0-dev_history.Rmd" )
202
189
if (file.exists(dev_file ) & ! isTRUE(overwrite )) {
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
- )
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." )
207
192
} else {
208
193
copy <- file.copy(
209
194
system.file(" the-dev-history.Rmd" , package = " fusen" ),
@@ -215,8 +200,9 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
215
200
}
216
201
dev_file_path <- c(dev_file_path , dev_file )
217
202
}
203
+
218
204
}
219
-
205
+
220
206
# Add data for the full template exemple
221
207
if (template %in% c(" full" )) {
222
208
inst_dir <- file.path(pkg , " inst" )
@@ -227,15 +213,15 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
227
213
# Example dataset
228
214
file.copy(system.file(" nyc_squirrels_sample.csv" , package = " fusen" ), inst_dir )
229
215
}
230
-
216
+
231
217
# .Rbuildignore ----
232
218
# usethis::use_build_ignore(dev_dir) # Cannot be used outside project
233
219
if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
234
220
lines <- c(paste0(" ^" , dev_dir , " $" ), " ^\\ .here$" )
235
221
} else {
236
222
lines <- c(paste0(" ^" , dev_dir , " $" ))
237
223
}
238
-
224
+
239
225
buildfile <- normalizePath(file.path(pkg , " .Rbuildignore" ), mustWork = FALSE )
240
226
if (! file.exists(buildfile )) {
241
227
existing_lines <- " "
@@ -247,11 +233,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
247
233
all <- c(existing_lines , new )
248
234
cat(enc2utf8(all ), file = buildfile , sep = " \n " )
249
235
}
250
-
236
+
251
237
# Add a gitignore file in dev_dir ----
252
238
# Files to ignore
253
- lines <- c(" *.html" )
254
-
239
+ lines <- c(" *.html" , " *.R " )
240
+
255
241
gitfile <- normalizePath(file.path(full_dev_dir , " .gitignore" ), mustWork = FALSE )
256
242
if (! file.exists(gitfile )) {
257
243
existing_lines <- " "
@@ -263,13 +249,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
263
249
all <- c(existing_lines , new )
264
250
cat(enc2utf8(all ), file = gitfile , sep = " \n " )
265
251
}
266
-
252
+
267
253
if (length(list.files(pkg , pattern = " [.]Rproj" )) == 0 ) {
268
254
here :: set_here(pkg )
269
255
}
270
- if (isTRUE(open ) & interactive()) {
271
- usethis :: edit_file(dev_file_path )
272
- }
273
-
256
+ if (isTRUE(open ) & interactive()) {usethis :: edit_file(dev_file_path )}
257
+
274
258
dev_file_path
275
259
}
0 commit comments