Skip to content

Commit

Permalink
Checks if the project looks like an R project and asks for confirmati…
Browse files Browse the repository at this point in the history
…on. #227
  • Loading branch information
andrie committed Oct 28, 2016
1 parent 4992857 commit 774c8d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
34 changes: 32 additions & 2 deletions R/checkpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
#'
#' @param verbose If TRUE, displays progress messages.
#'
#' @param forceInstall If TRUE. forces the re-installation of all discovered packages and their dependencies. This is useful if, for some reason, the checkpoint archive becomes corrupted.
#' @param forceInstall If TRUE, forces the re-installation of all discovered packages and their dependencies. This is useful if, for some reason, the checkpoint archive becomes corrupted.
#'
#' @param forceProject If TRUE, forces the checkpoint process, even if the provided project folder doesn't look like an R project. A commonly reported user problem is that they accidentally trigger the checkpoint process from their home folder, resulting in scanning many R files and downloading many packages. To prevent this, we use a heuristic to determine if the project folder looks like an R project. If the project folder is the home folder, and also contains no R files, then \code{checkpoint()} asks for confirmation to continue.

#'
#' @return Checkpoint is called for its side-effects (see the details section), but invisibly returns a list with elements:
#' \itemize{
Expand All @@ -78,7 +80,10 @@ checkpoint <- function(snapshotDate, project = getwd(), R.version, scanForPackag
use.knitr = system.file(package="knitr") != "",
auto.install.knitr = TRUE,
scan.rnw.with.knitr = FALSE,
forceInstall = FALSE) {
forceInstall = FALSE,
forceProject = FALSE) {

if(interactive()) validateProjectFolder(project)

stopIfInvalidDate(snapshotDate)

Expand Down Expand Up @@ -240,3 +245,28 @@ mssg <- function(x, ...) if(x) message(...)

correctR <- function(x) compareVersion(as.character(utils::packageVersion("base")), x) == 0


# Scans for R files in a folder and the first level subfolders.
#
anyRfiles <- function(path = "."){
findRfiles <- function(path = "."){
pattern <- "\\.[rR]$|\\.[rR]nw$|\\.[rR]md$|\\.[rR]pres$|\\.[rR]proj$"
z <- list.files(path = path, pattern = pattern, full.names = TRUE)
normalizePath(z, winslash = "/")
}
dirs <- list.dirs(path = path, recursive = FALSE)
rfiles <- as.vector(unlist(sapply(dirs, findRfiles)))
length(rfiles) > 0
}

validateProjectFolder <- function(project) {
if(normalizePath(project) == normalizePath("~/") && !anyRfiles(project)){
message("This doesn't look like an R project directory.\n",
"Use forceProject = TRUE to force scanning"
)
answer = readline("Continue (y/n)? ")
if(tolower(answer) != "y"){
stop("Scanning stopped.", call. = FALSE)
}
}
}
3 changes: 2 additions & 1 deletion R/checkpoint_paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ authorizeFileSystemUse =
stop("Can't use a non-directory as checkpoint root")}
else {
if(interactive()) {
answer = readline(paste("Can I create directory", checkpointRoot, "for internal checkpoint use?(y/n)\n"))
message(paste("Can I create directory", checkpointRoot, "for internal checkpoint use?\n"))
answer = readline("Continue (y/n)? ")
if(tolower(answer) != "y")
stop("Cannot proceed without access to checkpoint directory")}
else {
Expand Down
6 changes: 4 additions & 2 deletions man/checkpoint.Rd

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

0 comments on commit 774c8d0

Please sign in to comment.