Creating Directories in R
dir.create() function.
Original Japanese version: Rでディレクトリを作成する方法
In R, you can create directories with the dir.create() function.
The dir.create() Function
The syntax is as follows.
dir.create(path, showWarnings = TRUE, recursive = FALSE, mode = "0777")path: specifies the path of the directory to create.~represents the home directory.showWarnings: if set toTRUE, a warning is shown when the directory already exists. The default isTRUE.recursive: if set toTRUE, parent directories are also created as needed. The default isFALSE.mode: specifies the access permissions for the directory to create. The default is"0777".
The mode argument is meaningful only on Unix-like systems. It is ignored on Windows. In most cases, the default is fine. This post does not explain it in detail.
If you want to know more, see the help page with ?dir.create, or consult the following documentation.
Example
For example, the following code creates a directory named save/path/to/directory. Even if the parent directories do not exist, specifying recursive = TRUE creates the required parent directories as well. Also, showWarnings = FALSE suppresses warnings if the directory already exists.
save_dir <- "save/path/to/directory"
dir.create(save_dir, showWarnings = FALSE, recursive = TRUE)When this code is executed, save/path/to/directory is created even if the save directory does not yet exist.