-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea11b7
commit 6d694c9
Showing
8 changed files
with
222 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
################################################################################ | ||
#' Check filepath. | ||
#' | ||
#' Checks the specified filepath for formatting consistencies, | ||
#' such as trailing slashes, etc. | ||
#' | ||
#' @param path A character string corresponding to a filepath. | ||
#' | ||
#' @param create A logical indicating whether the path should | ||
#' be created if it doesn't exist. Default is \code{FALSE}. | ||
#' | ||
#' @return Character string denoting the cleaned up filepath. | ||
#' | ||
#' @seealso \code{\link{file.exists}}, \code{\link{dir.create}}. | ||
#' | ||
#' @docType methods | ||
#' @rdname checkPath | ||
#' | ||
setGeneric("checkPath", function(path, create) { | ||
standardGeneric("checkPath") | ||
}) | ||
|
||
#' @rdname checkPath | ||
setMethod("checkPath", | ||
signature(path="character", create="logical"), | ||
definition=function(path, create) { | ||
if (length(path)!=1) { | ||
stop("path must be a character vector of length 1.") | ||
} else { | ||
if (is.na(path)) { | ||
stop("Invalid path: cannot be NA.") | ||
} else { | ||
path = normPath(path) | ||
|
||
if (!file.exists(path)) { | ||
if (create==TRUE) { | ||
dir.create(file.path(path), recursive=TRUE, showWarnings=FALSE) | ||
} else { | ||
stop(paste("Specified path", path, "doesn't exist.", | ||
"Create it and try again.")) | ||
} | ||
} | ||
return(path) | ||
} | ||
} | ||
}) | ||
|
||
#' @rdname checkPath | ||
setMethod("checkPath", | ||
signature(path="character", create="missing"), | ||
definition=function(path) { | ||
return(checkPath(path, create=FALSE)) | ||
}) | ||
|
||
#' @rdname checkPath | ||
setMethod("checkPath", | ||
signature(path="NULL", create="ANY"), | ||
definition=function(path) { | ||
stop("Invalid path: cannot be NULL.") | ||
}) | ||
|
||
#' @rdname checkPath | ||
setMethod("checkPath", | ||
signature(path="missing", create="ANY"), | ||
definition=function() { | ||
stop("Invalid path: no path specified.") | ||
}) | ||
|
||
################################################################################ | ||
#' Normalize filepath. | ||
#' | ||
#' Checks the specified filepath for formatting consistencies: | ||
#' 1) use slash instead of backslash; | ||
#' 2) do tilde etc. expansion; | ||
#' 3) remove trailing slash. | ||
#' | ||
#' @param path A character vector of filepaths. | ||
#' | ||
#' @return Character vector of cleaned up filepaths. | ||
#' | ||
#' @docType methods | ||
#' @rdname normPath | ||
#' @importFrom magrittr %>% | ||
#' | ||
setGeneric("normPath", function(path) { | ||
standardGeneric("normPath") | ||
}) | ||
|
||
#' @rdname normPath | ||
setMethod("normPath", | ||
signature(path="character"), | ||
definition=function(path) { | ||
lapply(path, function(x) { | ||
if (is.na(x)) { | ||
NA_character_ | ||
} else { | ||
normalizePath(x, winslash="/", mustWork=FALSE) | ||
} | ||
}) %>% | ||
unlist %>% | ||
gsub("^[.]", paste0(getwd()), .) %>% | ||
gsub("\\\\", "//", .) %>% | ||
gsub("//", "/", .) %>% | ||
gsub("/$", "", .) | ||
}) | ||
|
||
#' @rdname normPath | ||
setMethod("normPath", | ||
signature(path="list"), | ||
definition=function(path) { | ||
return(normPath(unlist(path))) | ||
}) | ||
|
||
#' @rdname normPath | ||
setMethod("normPath", | ||
signature(path="NULL"), | ||
definition=function(path) { | ||
return(character()) | ||
}) | ||
|
||
#' @rdname normPath | ||
setMethod("normPath", | ||
signature(path="missing"), | ||
definition=function() { | ||
return(character()) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters