Use seq_file_ops()
to quickly produce sequential
scripts.
# old_wd <- setwd(tempdir())
# create a folder with some sequentially labelled scripts
seq_file_ops(n = 10, target_dir = "munge", filetype = "R")
#> seq_file_ops() created munge/ as it was not found.
#> New files created: munge/01-.R, munge/02-.R, munge/03-.R, munge/04-.R, munge/05-.R, munge/06-.R, munge/07-.R, munge/08-.R, munge/09-.R, munge/10-.R
# take a peek to confirm
list.files("munge")
#> [1] "01-.R" "02-.R" "03-.R" "04-.R" "05-.R" "06-.R" "07-.R" "08-.R" "09-.R"
#> [10] "10-.R"
During your work, as the program you are writing grows it may become necessary to separate code from one script into two or more. This could involve some laborious manual renumbering of the sequential scripts within the munge directory.
Instead, we can use adj_file_nos()
to make room for our
new script(s). In the following example, I will need to add just
one more single script. I achieve this by leaving the
step
and action
parameters as their default
values. I need to insert the new script at “02-.R”.
adj_file_nos(target = 2, directory = "munge")
#> 10 Filenames adjusted.
#> From: 01-.R, 02-.R, 03-.R, 04-.R, 05-.R, 06-.R, 07-.R, 08-.R, 09-.R, 10-.R
#> To: 01-.R, 03-.R, 04-.R, 05-.R, 06-.R, 07-.R, 08-.R, 09-.R, 10-.R, 11-.R
list.files("munge")
#> [1] "01-.R" "03-.R" "04-.R" "05-.R" "06-.R" "07-.R" "08-.R" "09-.R" "10-.R"
#> [10] "11-.R"
Now I can create a new file at “02-.R” without needing to manually relabel anything.
file.create("munge/02-.R")
#> [1] TRUE
list.files("munge")
#> [1] "01-.R" "02-.R" "03-.R" "04-.R" "05-.R" "06-.R" "07-.R" "08-.R" "09-.R"
#> [10] "10-.R" "11-.R"
Your customer has changed their mind about a feature, meaning all your hard work on scripts “06-.R” to “08-.R” are no longer required. Let’s delete those scripts.
rm_these <- c("06-.R", "07-.R", "08-.R")
file.remove(paste("munge", rm_these, sep = "/"))
#> [1] TRUE TRUE TRUE
list.files("munge")
#> [1] "01-.R" "02-.R" "03-.R" "04-.R" "05-.R" "09-.R" "10-.R" "11-.R"
Now there is a gap in the sequence. To fix this could involve more
laborious relabelling. Instead, let’s use adj_file_nos()
to
correct the sequence.
adj_file_nos(target = 9, directory = "munge", action = "down", step = 3)
#> 8 Filenames adjusted.
#> From: 01-.R, 02-.R, 03-.R, 04-.R, 05-.R, 09-.R, 10-.R, 11-.R
#> To: 01-.R, 02-.R, 03-.R, 04-.R, 05-.R, 06-.R, 07-.R, 08-.R
list.files("munge")
#> [1] "01-.R" "02-.R" "03-.R" "04-.R" "05-.R" "06-.R" "07-.R" "08-.R"
Much better!