Skip to content

Commit

Permalink
remove SpaDES dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotmcintire committed Nov 24, 2015
1 parent 9ea11b7 commit 6d694c9
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 86 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ Imports:
lazyeval,
magrittr,
raster,
rgdal,
SpaDES
rgdal
License: GPL-3
BugReports: https://github.com/PredictiveEcology/lazyR/issues
ByteCompile: yes
Collate:
'lazyR-package.R'
'paths.R'
'lazyR.R'
RoxygenNote: 5.0.0
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export("%<%")
export(assignCache)
export(checkLazyDir)
export(copyDir)
export(copyFile)
export(getLazyDir)
export(lazyDir)
export(lazyExists)
Expand All @@ -16,7 +17,6 @@ export(lazyRm)
export(lazySave)
export(saveToRepoRaster)
export(setLazyDir)
importFrom(SpaDES,checkPath)
importFrom(archivist,addTagsRepo)
importFrom(archivist,cache)
importFrom(archivist,createEmptyRepo)
Expand Down
100 changes: 21 additions & 79 deletions R/lazyR.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ lazyLs <- function(tag=NULL, lazyDir=NULL,
#' @importFrom raster raster
#' @importFrom magrittr %>%
#' @examples
#' library(SpaDES)
#' tmpdir <- checkPath(file.path(tempdir(), "lazyDir"), create=TRUE)
#' obj <- rnorm(10)
#' # save the obj
Expand Down Expand Up @@ -843,31 +842,42 @@ copyDir <- function(fromDir=NULL, toDir=NULL, useRobocopy=TRUE,
create=TRUE, silent=FALSE) {

origDir <- getwd()
#fromDir <- checkLazyDir(fromDir)
#toDir <- checkLazyDir(toDir, create=create)
fromDir <- checkLazyDir(fromDir)
toDir <- checkLazyDir(toDir, create=create)
setwd(fromDir)

os <- tolower(Sys.info()[["sysname"]])
if(os=="windows") {
if(useRobocopy) {
if(silent){
system(paste0("robocopy /E ","/purge"[delDestination]," /ETA /NDL /NFL /NJH /NJS ", normalizePath(fromDir, winslash = "\\"),
"\\ ", normalizePath(toDir, winslash = "\\")))
tryCatch(system(paste0("robocopy /E ","/purge"[delDestination]," /ETA /NDL /NFL /NJH /NJS ", normalizePath(fromDir, winslash = "\\"),
"\\ ", normalizePath(toDir, winslash = "\\"))),
error=function(x) file.copy(from = dir(fromDir), to = toDir,
overwrite = overwrite, recursive=TRUE)
)
} else {
system(paste0("robocopy /E ","/purge"[delDestination]," /ETA ", normalizePath(fromDir, winslash = "\\"),
"\\ ", normalizePath(toDir, winslash = "\\")))
# system(paste0("robocopy /E ","/purge"[delDestination]," /ETA ", normalizePath(fromDir, winslash = "\\"),
# "\\ ", normalizePath(toDir, winslash = "\\"), "\\"))
tryCatch(system(paste0("robocopy /E ","/purge"[delDestination]," /ETA ", normalizePath(fromDir, winslash = "\\"),
"\\ ", normalizePath(toDir, winslash = "\\"))),
error=function(x) file.copy(from = dir(fromDir), to = toDir,
overwrite = overwrite, recursive=TRUE)
)

}
} else {
file.copy(from = dir(fromDir), to = toDir,
overwrite = overwrite, recursive=TRUE)
}
} else if(os=="linux" | os == "darwin") {
if(silent){
system(paste0("rsync -aP ","--delete "[delDestination], fromDir, "/ ", toDir))
tryCatch(system(paste0("rsync -aP ","--delete "[delDestination], fromDir, "/ ", toDir)),
error=function(x) file.copy(from = dir(fromDir), to = toDir,
overwrite = overwrite, recursive=TRUE)
)
} else {
system(paste0("rsync -avP ","--delete "[delDestination], fromDir, "/ ", toDir))
tryCatch(system(paste0("rsync -avP ","--delete "[delDestination], fromDir, "/ ", toDir)),
error=function(x) file.copy(from = dir(fromDir), to = toDir,
overwrite = overwrite, recursive=TRUE)
)
}
}
setwd(origDir)
Expand Down Expand Up @@ -1248,71 +1258,3 @@ lazyLoadFromRepo <- function(artifact, lazyDir=lazyDir(), objName, envir=parent.
delayedAssign(objName, value = lazy_eval(loadedObj), assign.env = envir)
}

################################################################################
#' 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
#'
# igraph exports %>% from magrittr
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.")
})
126 changes: 126 additions & 0 deletions R/paths.R
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())
})
38 changes: 38 additions & 0 deletions man/checkPath.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/lazyLoad2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/normPath.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions tests/testthat/test-lazyR.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ test_that("Test raster mechanisms", {
#
# r %<% raster::raster(file.path(tempdir(),"r.tif"))
# ras %<% raster::raster(file.path(tempdir(),"ras.tif"))
# library(SpaDES)
# Plot(r, ras, new=T)
#
# r

})

0 comments on commit 6d694c9

Please sign in to comment.