Learn R Programming

Require (version 0.3.1)

checkPath: Check directory path

Description

Checks the specified path to a directory for formatting consistencies, such as trailing slashes, etc.

Usage

checkPath(path, create)

# S4 method for character,logical checkPath(path, create)

# S4 method for character,missing checkPath(path)

# S4 method for `NULL`,ANY checkPath(path)

# S4 method for missing,ANY checkPath()

Value

Character string denoting the cleaned up filepath.

Arguments

path

A character string corresponding to a directory path.

create

A logical indicating whether the path should be created if it does not exist. Default is FALSE.

See Also

Examples

Run this code
## normalize file paths
paths <- list("./aaa/zzz",
              "./aaa/zzz/",
              ".//aaa//zzz",
              ".//aaa//zzz/",
              ".\\\\aaa\\\\zzz",
              ".\\\\aaa\\\\zzz\\\\",
              file.path(".", "aaa", "zzz"))

checked <- normPath(paths)
length(unique(checked)) ## 1; all of the above are equivalent

## check to see if a path exists
tmpdir <- file.path(tempdir(), "example_checkPath")

dir.exists(tmpdir) ## FALSE
tryCatch(checkPath(tmpdir, create = FALSE), error = function(e) FALSE) ## FALSE

checkPath(tmpdir, create = TRUE)
dir.exists(tmpdir) ## TRUE

unlink(tmpdir, recursive = TRUE) # clean up

Run the code above in your browser using DataLab