R dir.create
Create a nested directory tree and verify the final directory.
A simple example
dir.create
- Start with An empty practice folder
- Do this Create a/b including parents
- Result Both directories exist
Syntax
dir.create(week, recursive = TRUE)
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.RPrepare nested hand-in folders
A department creates a hand-in folder for each class and week. The job runs again if the first delivery is interrupted, so existing directories should remain usable on a second run.
run_example <- function() {
base <- tempfile("lesson-")
stopifnot(dir.create(base))
on.exit(unlink(base, recursive = TRUE))
week <- file.path(base, "class", "week1")
for (attempt in 1:2) {
if (!dir.exists(week)) dir.create(week, recursive = TRUE)
stopifnot(dir.exists(week))
}
cat("ready\n")
}
run_example()
ready
The second attempt finds the same directory and the application can use it.
Locate a rehearsal score
The choir stores each score inside a season folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?
Locate a workshop handout
An instructor stores the current template in a class folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?
What to watch for
Handle a regular file occupying any required directory name as an error.