From b844d6b01641a7f5ca2b9e0f36b762a57ca34fed Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sat, 29 Oct 2016 19:13:14 -0400 Subject: [PATCH 01/11] Address #100 I added functions list_examples_remake, example_remake, and clean_example_remake to generate and clean up remake examples. example_remake(.., clean = TRUE) calls clean_example_remake instead of generating the files. I also added testthat tests (test-examples.R), all of which pass. A call to covr::package_coverage says that 100% of the code in examples.R is covered in tests. To add a new example, simply add a folder in inst/examples with the name of your example. Check the quickstart example for a primer. To take full advantage of the cleanup capabilities of clean_example_remake, each example should have a file named remake.yml file as its root YAML file. I also changed the DESCRIPTION TO better satisfy R CMD check --as-cran. I capitalized more words in the title to satisfy formatting, and substituted the entry in authors@R with a call to the person function. Lastly, I added .DS_Store to .gitignore and .Rbuildignore since I'm coding on a macbook. --- .Rbuildignore | 1 + .gitignore | 1 + DESCRIPTION | 13 +++--- NAMESPACE | 3 ++ R/examples.R | 66 +++++++++++++++++++++++++++++ inst/examples/quickstart/code.R | 11 +++++ inst/examples/quickstart/remake.yml | 16 +++++++ man/clean_example_remake.Rd | 26 ++++++++++++ man/example_remake.Rd | 27 ++++++++++++ man/list_examples_remake.Rd | 18 ++++++++ tests/testthat/test-examples.R | 37 ++++++++++++++++ 11 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 R/examples.R create mode 100644 inst/examples/quickstart/code.R create mode 100644 inst/examples/quickstart/remake.yml create mode 100644 man/clean_example_remake.Rd create mode 100644 man/example_remake.Rd create mode 100644 man/list_examples_remake.Rd create mode 100644 tests/testthat/test-examples.R diff --git a/.Rbuildignore b/.Rbuildignore index 28f2ccb..cdc8423 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -8,3 +8,4 @@ tests/testthat/.remake ^remake_.*\.tar\.gz$ ^.*\.Rproj$ ^\.Rproj\.user$ +.DS_Store diff --git a/.gitignore b/.gitignore index fd55545..856d9cc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ docker/ TODO.md Rplots.pdf .Rproj.user +.DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index 5471a0c..65d3981 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,24 +1,25 @@ Package: remake -Title: Make-like build management +Title: Make-Like Build Management Version: 0.3.0 -Authors@R: "Rich FitzJohn [aut, cre]" +Authors@R: person(family = "FitzJohn", given = "Rich", + email = "rich.fitzjohn@gmail.com", role = c("aut", "cre")) Description: Make-like build management in R. The idea is to have some sort of declarative programming to build analysis pipelines, without having to use/install make. -Depends: +Depends: R (>= 3.0.0) License: BSD_2_clause + file LICENSE LazyData: true -Imports: +Imports: R6 (>= 2.0.0), crayon, digest, optparse, storr (>= 0.5.0), yaml -Suggests: +Suggests: DiagrammeR, devtools, knitr, testthat -RoxygenNote: 5.0.1.9000 +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE index 34affb6..7acc9ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,11 +4,13 @@ S3method(print,target_placeholder) export(archive_export) export(archive_import) export(auto_gitignore) +export(clean_example_remake) export(create_bindings) export(delete) export(delete_bindings) export(diagram) export(dump_environment) +export(example_remake) export(fetch) export(fetch_archive) export(file_extensions) @@ -18,6 +20,7 @@ export(is_archive) export(is_current) export(list_archive) export(list_dependencies) +export(list_examples_remake) export(list_targets) export(make) export(make_environment) diff --git a/R/examples.R b/R/examples.R new file mode 100644 index 0000000..9c1b44a --- /dev/null +++ b/R/examples.R @@ -0,0 +1,66 @@ +#' @title Function \code{list_examples_remake} +#' @description Return a character vector of the names of all the remake examples. +#' @seealso \code{\link{example_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +#' @export +#' @return a character vector of the names of all the remake examples. +list_examples_remake = function(){ + list.files(system.file("examples", package = "remake")) +} + +#' @title Function \code{example_remake} +#' @description Write files for a remake example. +#' To see the names of all the examples, run \code{\link{list_examples_remake}}. +#' @seealso \code{\link{list_examples_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +#' @export +#' @param example name of example. To see all the possible values, +#' run \code{\link{list_examples_remake}}. +#' @param verbose \code{TRUE}/\code{FALSE} option to be verbose +#' @param clean \code{TRUE}/\code{FALSE} option. If \code{TRUE}, +#' the example files and the \code{.remake} \code{storr} cache +#' are cleaned up rather than written. +example_remake = function(example = list_examples_remake(), verbose = TRUE, clean = FALSE){ + example_dir <- file.path("examples", match.arg(example)) + if(clean){ + clean_example_remake(example = example, verbose = verbose) + return(invisible()) + } + dir <- system.file(example_dir, package = "remake") + for(file in list.files(dir)){ + path <- system.file(example_dir, file, package = "remake") + if (!file.exists(path)) stop("File ", file, + " is missing from installed package remake.", call.=FALSE) + contents <- readLines(path) + write(contents, file) + } + if(verbose){ + cat("Wrote files: ", paste0(list.files(dir), collapse = ", "), ".\n", sep = "") + cat("To run the example, open an R session and enter remake::make.\n") + cat("") + } +} + +#' @title Function \code{clean_example_remake} +#' @description Clean up a remake example. +#' @details Removes the files for the example (written by \code{\link{example_remake}}) +#' along with the \code{storr} cache \code{.remake} (if it exists). +#' To see the names of all the examples, run \code{\link{list_examples_remake}}. +#' @seealso \code{\link{list_examples_remake}}, \code{\link{example_remake}}, \code{\link{make}} +#' @export +#' @param example name of example. To see all the possible values, +#' run \code{\link{list_examples_remake}}. +#' @param verbose \code{TRUE}/\code{FALSE} option to be verbose +clean_example_remake = function(example = list_examples_remake(), verbose = TRUE){ + example_dir <- file.path("examples", match.arg(example)) + dir <- system.file(example_dir, package = "remake") + if(file.exists("remake.yml")){ + remake::make("clean", verbose = FALSE) + if(verbose) cat("Ran make(\"clean\", verbose = FALSE).\n") + } + files <- c(".remake", list.files(dir)) + files <- files[file.exists(files)] + unlink(files, recursive = TRUE) + if(verbose){ + cat("Cleaned up example ", example, ".\n", sep = "") + if(length(files)) cat("Removed files and folders: ", paste0(files, collapse = ", "), ".\n", sep = "") + } +} diff --git a/inst/examples/quickstart/code.R b/inst/examples/quickstart/code.R new file mode 100644 index 0000000..fe2becc --- /dev/null +++ b/inst/examples/quickstart/code.R @@ -0,0 +1,11 @@ +get_data = function(){ + random_numbers() +} + +random_numbers = function(){ + rnorm(1000) # Change 1000 to 2000 and rerun remake::make() to see what remake does. +} + +produce_plot = function(data, title){ + hist(data, main = title, col = "black") +} diff --git a/inst/examples/quickstart/remake.yml b/inst/examples/quickstart/remake.yml new file mode 100644 index 0000000..6c913c8 --- /dev/null +++ b/inst/examples/quickstart/remake.yml @@ -0,0 +1,16 @@ +sources: code.R +# packages: MASS + +targets: + all: + depends: + plot.pdf + + plot.pdf: # Change the title and rerun remake::make() to see what remake does. + command: + produce_plot(data = my_data, title = I("Plot title")) # Use I() for character arguments. + plot: TRUE + + my_data: + command: + get_data() diff --git a/man/clean_example_remake.Rd b/man/clean_example_remake.Rd new file mode 100644 index 0000000..97c1ed6 --- /dev/null +++ b/man/clean_example_remake.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/examples.R +\name{clean_example_remake} +\alias{clean_example_remake} +\title{Function \code{clean_example_remake}} +\usage{ +clean_example_remake(example = list_examples_remake(), verbose = TRUE) +} +\arguments{ +\item{example}{name of example. To see all the possible values, +run \code{\link{list_examples_remake}}.} + +\item{verbose}{\code{TRUE}/\code{FALSE} option to be verbose} +} +\description{ +Clean up a remake example. +} +\details{ +Removes the files for the example (written by \code{\link{example_remake}}) +along with the \code{storr} cache \code{.remake} (if it exists). +To see the names of all the examples, run \code{\link{list_examples_remake}}. +} +\seealso{ +\code{\link{list_examples_remake}}, \code{\link{example_remake}}, \code{\link{make}} +} + diff --git a/man/example_remake.Rd b/man/example_remake.Rd new file mode 100644 index 0000000..52a7ae6 --- /dev/null +++ b/man/example_remake.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/examples.R +\name{example_remake} +\alias{example_remake} +\title{Function \code{example_remake}} +\usage{ +example_remake(example = list_examples_remake(), verbose = TRUE, + clean = FALSE) +} +\arguments{ +\item{example}{name of example. To see all the possible values, +run \code{\link{list_examples_remake}}.} + +\item{verbose}{\code{TRUE}/\code{FALSE} option to be verbose} + +\item{clean}{\code{TRUE}/\code{FALSE} option. If \code{TRUE}, +the example files and the \code{.remake} \code{storr} cache +are cleaned up rather than written.} +} +\description{ +Write files for a remake example. +To see the names of all the examples, run \code{\link{list_examples_remake}}. +} +\seealso{ +\code{\link{list_examples_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +} + diff --git a/man/list_examples_remake.Rd b/man/list_examples_remake.Rd new file mode 100644 index 0000000..5acdf25 --- /dev/null +++ b/man/list_examples_remake.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/examples.R +\name{list_examples_remake} +\alias{list_examples_remake} +\title{Function \code{list_examples_remake}} +\usage{ +list_examples_remake() +} +\value{ +a character vector of the names of all the remake examples. +} +\description{ +Return a character vector of the names of all the remake examples. +} +\seealso{ +\code{\link{example_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +} + diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R new file mode 100644 index 0000000..4a5e574 --- /dev/null +++ b/tests/testthat/test-examples.R @@ -0,0 +1,37 @@ +# library(remake); library(testthat); example = "quickstart" +context("examples") + +expect_no_files = function(){ + expect_equal(length(list.files()), 0) + expect_false(file.exists(".remake")) +} + +test_that("Functions to manage remake examples work properly", { + expect_silent(list_examples_remake()) + expect_gt(length(list_examples_remake()), 0) + for(example in list_examples_remake()){ + dir = paste0("RUNNNING_TEST_examples_", example) + if(file.exists(dir)) stop(paste("Test directory", dir, "already exists.")) + dir.create(dir) + setwd(dir) + expect_no_files() + nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" + expect_error(example_remake(nonsense)) + expect_error(clean_example_remake(nonsense)) + expect_no_files() + expect_silent(example_remake(verbose = F)) + expect_silent(example_remake(verbose = F, clean = T)) + expect_no_files() + expect_silent(example_remake(example = example, verbose = F)) + expect_gt(length(list.files()), 0) + for(i in 1:2) expect_silent(example_remake(example = example, verbose = F, clean = T)) + expect_no_files() + expect_false(file.exists(".remake")) + expect_output(example_remake(example = example)) + expect_silent(remake::make(verbose = F)) + for(i in 1:2) expect_output(example_remake(example = example, clean = T)) + expect_no_files() + setwd("..") + unlink(dir, recursive = T) + } +}) From f97ff468b30cb5a6d0db13957514a6845d2cb01f Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 08:46:03 -0400 Subject: [PATCH 02/11] Add inst/examples/README.md Explains how to use and create examples. --- inst/examples/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 inst/examples/README.md diff --git a/inst/examples/README.md b/inst/examples/README.md new file mode 100644 index 0000000..8bf6a15 --- /dev/null +++ b/inst/examples/README.md @@ -0,0 +1,5 @@ +These are the `remake` examples managed by functions `list_examples_remake()` and `example_remake()`. To add your own example, simply make a new folder in `inst/examples` and put your files inside. If you create and populate `inst/examples/my_example`, then + +1. `"my_example"` will be automatically included in the output of `list_examples_remake()`. +2. `example_remake(example = "my_example")` will write the contents of `inst/examples/my_example` to the current working directory of your R session. +3. `example_remake(example = "my_example", clean = TRUE)` will first call `make("clean")` (if there is a file called `remake.yml`) and then remove the files in the example from the current working directory. From 2300a150482111e5b86cea91b3a33be041118a01 Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 09:05:14 -0400 Subject: [PATCH 03/11] Fix last commit The previous commit of the PR was broken because I added README.md. Fixed now. --- R/examples.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/examples.R b/R/examples.R index 9c1b44a..b6c273c 100644 --- a/R/examples.R +++ b/R/examples.R @@ -4,7 +4,7 @@ #' @export #' @return a character vector of the names of all the remake examples. list_examples_remake = function(){ - list.files(system.file("examples", package = "remake")) + list.dirs(system.file("examples", package = "remake"), full.names = FALSE, recursive = FALSE) } #' @title Function \code{example_remake} From 1962d8f1dc9737b48887393b2920a8bce14cda75 Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 09:13:54 -0400 Subject: [PATCH 04/11] Fix issue with nested folders in examples. Examples can now have nested folders (example_remake()). --- R/examples.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/examples.R b/R/examples.R index b6c273c..16f41f6 100644 --- a/R/examples.R +++ b/R/examples.R @@ -29,12 +29,11 @@ example_remake = function(example = list_examples_remake(), verbose = TRUE, clea path <- system.file(example_dir, file, package = "remake") if (!file.exists(path)) stop("File ", file, " is missing from installed package remake.", call.=FALSE) - contents <- readLines(path) - write(contents, file) + file.copy(from = path, to = getwd(), recursive = TRUE) } if(verbose){ cat("Wrote files: ", paste0(list.files(dir), collapse = ", "), ".\n", sep = "") - cat("To run the example, open an R session and enter remake::make.\n") + cat("To run the example, open an R session and enter remake::make().\n") cat("") } } From c824b9f42c3408e6112c62ee8f61ed7e9ff024b6 Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 09:16:34 -0400 Subject: [PATCH 05/11] Add to inst/examples/README.md Point out that hidden files in examples will not be copied over by `example_remake()`. --- inst/examples/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inst/examples/README.md b/inst/examples/README.md index 8bf6a15..fcb23dd 100644 --- a/inst/examples/README.md +++ b/inst/examples/README.md @@ -3,3 +3,5 @@ These are the `remake` examples managed by functions `list_examples_remake()` an 1. `"my_example"` will be automatically included in the output of `list_examples_remake()`. 2. `example_remake(example = "my_example")` will write the contents of `inst/examples/my_example` to the current working directory of your R session. 3. `example_remake(example = "my_example", clean = TRUE)` will first call `make("clean")` (if there is a file called `remake.yml`) and then remove the files in the example from the current working directory. + +NOTE: Hidden files in `inst/examples/my_example` will not be copied over by `example_remake()` becasue this may have unintended effects on the user's system. From 8aad2d882ef31b8f92e4cf039b46c0a1050b0de2 Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 09:37:26 -0400 Subject: [PATCH 06/11] Change console output of example_remake() Make it show the name of the example if verbose = T. --- R/examples.R | 15 +++++++++------ tests/testthat/test-examples.R | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/R/examples.R b/R/examples.R index 16f41f6..063121b 100644 --- a/R/examples.R +++ b/R/examples.R @@ -24,6 +24,7 @@ example_remake = function(example = list_examples_remake(), verbose = TRUE, clea clean_example_remake(example = example, verbose = verbose) return(invisible()) } + if(verbose) cat("Creating example: ", example, "\n", sep = "") dir <- system.file(example_dir, package = "remake") for(file in list.files(dir)){ path <- system.file(example_dir, file, package = "remake") @@ -32,9 +33,9 @@ example_remake = function(example = list_examples_remake(), verbose = TRUE, clea file.copy(from = path, to = getwd(), recursive = TRUE) } if(verbose){ - cat("Wrote files: ", paste0(list.files(dir), collapse = ", "), ".\n", sep = "") + cat("Wrote files: ", paste0(list.files(dir), collapse = ", "), "\n", sep = "") cat("To run the example, open an R session and enter remake::make().\n") - cat("") + cat("Done.\n") } } @@ -49,17 +50,19 @@ example_remake = function(example = list_examples_remake(), verbose = TRUE, clea #' run \code{\link{list_examples_remake}}. #' @param verbose \code{TRUE}/\code{FALSE} option to be verbose clean_example_remake = function(example = list_examples_remake(), verbose = TRUE){ + if(verbose) cat("Cleaning up example: ", example, "\n", sep = "") example_dir <- file.path("examples", match.arg(example)) dir <- system.file(example_dir, package = "remake") if(file.exists("remake.yml")){ + if(verbose) cat("Running make(\"clean\", verbose = FALSE).\n") remake::make("clean", verbose = FALSE) - if(verbose) cat("Ran make(\"clean\", verbose = FALSE).\n") } files <- c(".remake", list.files(dir)) files <- files[file.exists(files)] unlink(files, recursive = TRUE) - if(verbose){ - cat("Cleaned up example ", example, ".\n", sep = "") - if(length(files)) cat("Removed files and folders: ", paste0(files, collapse = ", "), ".\n", sep = "") + if(verbose){ + if(length(files)) + cat("Removed files and folders: ", paste0(files, collapse = ", "), ".\n", sep = "") + cat("Done.\n") } } diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index 4a5e574..506616d 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -16,8 +16,8 @@ test_that("Functions to manage remake examples work properly", { setwd(dir) expect_no_files() nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" - expect_error(example_remake(nonsense)) - expect_error(clean_example_remake(nonsense)) + expect_error(example_remake(nonsense, verbose = F)) + expect_error(clean_example_remake(nonsense, verbose = F)) expect_no_files() expect_silent(example_remake(verbose = F)) expect_silent(example_remake(verbose = F, clean = T)) From 7c0b4ebeff05bdefa2da3e13bc946c8b4d4df47e Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 10:20:09 -0400 Subject: [PATCH 07/11] Make examples more elegant Just copy the whole example folder rather than trying to write the files and then clean them up programatically --- .Rbuildignore | 2 + .gitignore | 2 + NAMESPACE | 1 - R/examples.R | 73 ++++++++-------------------------- inst/examples/README.md | 8 +--- man/clean_example_remake.Rd | 26 ------------ man/example_remake.Rd | 13 ++---- man/list_examples_remake.Rd | 6 +-- tests/testthat/test-examples.R | 36 +++++------------ 9 files changed, 38 insertions(+), 129 deletions(-) delete mode 100644 man/clean_example_remake.Rd diff --git a/.Rbuildignore b/.Rbuildignore index cdc8423..f8aa16f 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,5 @@ tests/testthat/.remake ^.*\.Rproj$ ^\.Rproj\.user$ .DS_Store +.Rapp.history + diff --git a/.gitignore b/.gitignore index 856d9cc..f13c623 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ TODO.md Rplots.pdf .Rproj.user .DS_Store +.Rapp.history + diff --git a/NAMESPACE b/NAMESPACE index 7acc9ad..61b0318 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,6 @@ S3method(print,target_placeholder) export(archive_export) export(archive_import) export(auto_gitignore) -export(clean_example_remake) export(create_bindings) export(delete) export(delete_bindings) diff --git a/R/examples.R b/R/examples.R index 063121b..ffbcf30 100644 --- a/R/examples.R +++ b/R/examples.R @@ -1,68 +1,29 @@ -#' @title Function \code{list_examples_remake} -#' @description Return a character vector of the names of all the remake examples. -#' @seealso \code{\link{example_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} -#' @export -#' @return a character vector of the names of all the remake examples. -list_examples_remake = function(){ - list.dirs(system.file("examples", package = "remake"), full.names = FALSE, recursive = FALSE) -} - #' @title Function \code{example_remake} -#' @description Write files for a remake example. +#' @description Copy a remake example to the current working directory. #' To see the names of all the examples, run \code{\link{list_examples_remake}}. -#' @seealso \code{\link{list_examples_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +#' @seealso \code{\link{list_examples_remake}}, \code{\link{make}} #' @export -#' @param example name of example. To see all the possible values, +#' @param example name of the example. To see all the available example names, #' run \code{\link{list_examples_remake}}. #' @param verbose \code{TRUE}/\code{FALSE} option to be verbose -#' @param clean \code{TRUE}/\code{FALSE} option. If \code{TRUE}, -#' the example files and the \code{.remake} \code{storr} cache -#' are cleaned up rather than written. -example_remake = function(example = list_examples_remake(), verbose = TRUE, clean = FALSE){ - example_dir <- file.path("examples", match.arg(example)) - if(clean){ - clean_example_remake(example = example, verbose = verbose) - return(invisible()) - } +example_remake = function(example = list_examples_remake(), verbose = TRUE){ + example <- match.arg(example) if(verbose) cat("Creating example: ", example, "\n", sep = "") - dir <- system.file(example_dir, package = "remake") - for(file in list.files(dir)){ - path <- system.file(example_dir, file, package = "remake") - if (!file.exists(path)) stop("File ", file, - " is missing from installed package remake.", call.=FALSE) - file.copy(from = path, to = getwd(), recursive = TRUE) - } + dir <- system.file(file.path("examples", example), package = "remake") + if(file.exists(example)) + stop("There is already a file or folder named ", example, ".", sep = "") + file.copy(from = dir, to = getwd(), recursive = TRUE) if(verbose){ - cat("Wrote files: ", paste0(list.files(dir), collapse = ", "), "\n", sep = "") - cat("To run the example, open an R session and enter remake::make().\n") - cat("Done.\n") + cat("Wrote example: ", paste0(list.files(dir), collapse = ", "), "\n", sep = "") + cat("To run the example, open an R session in", dir, "and enter remake::make().\n") } } -#' @title Function \code{clean_example_remake} -#' @description Clean up a remake example. -#' @details Removes the files for the example (written by \code{\link{example_remake}}) -#' along with the \code{storr} cache \code{.remake} (if it exists). -#' To see the names of all the examples, run \code{\link{list_examples_remake}}. -#' @seealso \code{\link{list_examples_remake}}, \code{\link{example_remake}}, \code{\link{make}} +#' @title Function \code{list_examples_remake} +#' @description Return the names of all the remake examples. +#' @seealso \code{\link{example_remake}}, \code{\link{make}} #' @export -#' @param example name of example. To see all the possible values, -#' run \code{\link{list_examples_remake}}. -#' @param verbose \code{TRUE}/\code{FALSE} option to be verbose -clean_example_remake = function(example = list_examples_remake(), verbose = TRUE){ - if(verbose) cat("Cleaning up example: ", example, "\n", sep = "") - example_dir <- file.path("examples", match.arg(example)) - dir <- system.file(example_dir, package = "remake") - if(file.exists("remake.yml")){ - if(verbose) cat("Running make(\"clean\", verbose = FALSE).\n") - remake::make("clean", verbose = FALSE) - } - files <- c(".remake", list.files(dir)) - files <- files[file.exists(files)] - unlink(files, recursive = TRUE) - if(verbose){ - if(length(files)) - cat("Removed files and folders: ", paste0(files, collapse = ", "), ".\n", sep = "") - cat("Done.\n") - } +#' @return a names of all the remake examples. +list_examples_remake = function(){ + list.dirs(system.file("examples", package = "remake"), full.names = FALSE, recursive = FALSE) } diff --git a/inst/examples/README.md b/inst/examples/README.md index fcb23dd..65434e0 100644 --- a/inst/examples/README.md +++ b/inst/examples/README.md @@ -1,7 +1 @@ -These are the `remake` examples managed by functions `list_examples_remake()` and `example_remake()`. To add your own example, simply make a new folder in `inst/examples` and put your files inside. If you create and populate `inst/examples/my_example`, then - -1. `"my_example"` will be automatically included in the output of `list_examples_remake()`. -2. `example_remake(example = "my_example")` will write the contents of `inst/examples/my_example` to the current working directory of your R session. -3. `example_remake(example = "my_example", clean = TRUE)` will first call `make("clean")` (if there is a file called `remake.yml`) and then remove the files in the example from the current working directory. - -NOTE: Hidden files in `inst/examples/my_example` will not be copied over by `example_remake()` becasue this may have unintended effects on the user's system. +These are the `remake` examples managed by functions `example_remake()` and `list_examples_remake()`. To add your own example, simply make a new folder in `inst/examples` and put your files inside. If you create (and hopefully populate) `inst/examples/my_example`, then, `"my_example"` will be automatically included in the output of `list_examples_remake()`, and `example_remake(example = "my_example")` will copy `inst/examples/my_example` to the current working directory of your R session. diff --git a/man/clean_example_remake.Rd b/man/clean_example_remake.Rd deleted file mode 100644 index 97c1ed6..0000000 --- a/man/clean_example_remake.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/examples.R -\name{clean_example_remake} -\alias{clean_example_remake} -\title{Function \code{clean_example_remake}} -\usage{ -clean_example_remake(example = list_examples_remake(), verbose = TRUE) -} -\arguments{ -\item{example}{name of example. To see all the possible values, -run \code{\link{list_examples_remake}}.} - -\item{verbose}{\code{TRUE}/\code{FALSE} option to be verbose} -} -\description{ -Clean up a remake example. -} -\details{ -Removes the files for the example (written by \code{\link{example_remake}}) -along with the \code{storr} cache \code{.remake} (if it exists). -To see the names of all the examples, run \code{\link{list_examples_remake}}. -} -\seealso{ -\code{\link{list_examples_remake}}, \code{\link{example_remake}}, \code{\link{make}} -} - diff --git a/man/example_remake.Rd b/man/example_remake.Rd index 52a7ae6..22fc50a 100644 --- a/man/example_remake.Rd +++ b/man/example_remake.Rd @@ -4,24 +4,19 @@ \alias{example_remake} \title{Function \code{example_remake}} \usage{ -example_remake(example = list_examples_remake(), verbose = TRUE, - clean = FALSE) +example_remake(example = list_examples_remake(), verbose = TRUE) } \arguments{ -\item{example}{name of example. To see all the possible values, +\item{example}{name of the example. To see all the available example names, run \code{\link{list_examples_remake}}.} \item{verbose}{\code{TRUE}/\code{FALSE} option to be verbose} - -\item{clean}{\code{TRUE}/\code{FALSE} option. If \code{TRUE}, -the example files and the \code{.remake} \code{storr} cache -are cleaned up rather than written.} } \description{ -Write files for a remake example. +Copy a remake example to the current working directory. To see the names of all the examples, run \code{\link{list_examples_remake}}. } \seealso{ -\code{\link{list_examples_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +\code{\link{list_examples_remake}}, \code{\link{make}} } diff --git a/man/list_examples_remake.Rd b/man/list_examples_remake.Rd index 5acdf25..3988414 100644 --- a/man/list_examples_remake.Rd +++ b/man/list_examples_remake.Rd @@ -7,12 +7,12 @@ list_examples_remake() } \value{ -a character vector of the names of all the remake examples. +a names of all the remake examples. } \description{ -Return a character vector of the names of all the remake examples. +Return the names of all the remake examples. } \seealso{ -\code{\link{example_remake}}, \code{\link{clean_example_remake}}, \code{\link{make}} +\code{\link{example_remake}}, \code{\link{make}} } diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index 506616d..9570b71 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -1,37 +1,19 @@ # library(remake); library(testthat); example = "quickstart" context("examples") -expect_no_files = function(){ - expect_equal(length(list.files()), 0) - expect_false(file.exists(".remake")) -} - test_that("Functions to manage remake examples work properly", { expect_silent(list_examples_remake()) expect_gt(length(list_examples_remake()), 0) + nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" + expect_error(example_remake(nonsense, verbose = F)) for(example in list_examples_remake()){ - dir = paste0("RUNNNING_TEST_examples_", example) - if(file.exists(dir)) stop(paste("Test directory", dir, "already exists.")) - dir.create(dir) - setwd(dir) - expect_no_files() - nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" - expect_error(example_remake(nonsense, verbose = F)) - expect_error(clean_example_remake(nonsense, verbose = F)) - expect_no_files() - expect_silent(example_remake(verbose = F)) - expect_silent(example_remake(verbose = F, clean = T)) - expect_no_files() - expect_silent(example_remake(example = example, verbose = F)) - expect_gt(length(list.files()), 0) - for(i in 1:2) expect_silent(example_remake(example = example, verbose = F, clean = T)) - expect_no_files() - expect_false(file.exists(".remake")) - expect_output(example_remake(example = example)) - expect_silent(remake::make(verbose = F)) - for(i in 1:2) expect_output(example_remake(example = example, clean = T)) - expect_no_files() + expect_silent(example_remake("quickstart", verbose = F)) + expect_error(example_remake("quickstart", verbose = F)) + unlink("quickstart", recursive = TRUE) + expect_output(example_remake("quickstart")) + setwd("quickstart") + make(verbose = F) setwd("..") - unlink(dir, recursive = T) + unlink("quickstart", recursive = TRUE) } }) From 210a97ebf4a5fafe93efac64f4b4852f8ff5550b Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 10:32:52 -0400 Subject: [PATCH 08/11] Do away with verbosity in examples. Verbosity is unnecessary since cleaning is dropped. --- R/examples.R | 9 ++------- man/example_remake.Rd | 4 +--- tests/testthat/test-examples.R | 10 ++++------ 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/R/examples.R b/R/examples.R index ffbcf30..d5397a4 100644 --- a/R/examples.R +++ b/R/examples.R @@ -5,18 +5,13 @@ #' @export #' @param example name of the example. To see all the available example names, #' run \code{\link{list_examples_remake}}. -#' @param verbose \code{TRUE}/\code{FALSE} option to be verbose -example_remake = function(example = list_examples_remake(), verbose = TRUE){ +example_remake = function(example = list_examples_remake()){ example <- match.arg(example) - if(verbose) cat("Creating example: ", example, "\n", sep = "") dir <- system.file(file.path("examples", example), package = "remake") if(file.exists(example)) stop("There is already a file or folder named ", example, ".", sep = "") file.copy(from = dir, to = getwd(), recursive = TRUE) - if(verbose){ - cat("Wrote example: ", paste0(list.files(dir), collapse = ", "), "\n", sep = "") - cat("To run the example, open an R session in", dir, "and enter remake::make().\n") - } + invisible() } #' @title Function \code{list_examples_remake} diff --git a/man/example_remake.Rd b/man/example_remake.Rd index 22fc50a..5191445 100644 --- a/man/example_remake.Rd +++ b/man/example_remake.Rd @@ -4,13 +4,11 @@ \alias{example_remake} \title{Function \code{example_remake}} \usage{ -example_remake(example = list_examples_remake(), verbose = TRUE) +example_remake(example = list_examples_remake()) } \arguments{ \item{example}{name of the example. To see all the available example names, run \code{\link{list_examples_remake}}.} - -\item{verbose}{\code{TRUE}/\code{FALSE} option to be verbose} } \description{ Copy a remake example to the current working directory. diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index 9570b71..b90f65b 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -7,13 +7,11 @@ test_that("Functions to manage remake examples work properly", { nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" expect_error(example_remake(nonsense, verbose = F)) for(example in list_examples_remake()){ - expect_silent(example_remake("quickstart", verbose = F)) - expect_error(example_remake("quickstart", verbose = F)) - unlink("quickstart", recursive = TRUE) - expect_output(example_remake("quickstart")) - setwd("quickstart") + expect_silent(example_remake(example)) + expect_error(example_remake(example)) + setwd(example) make(verbose = F) setwd("..") - unlink("quickstart", recursive = TRUE) + unlink(example, recursive = TRUE) } }) From cc0d95a647e81187af0b30e33c83862921544b8a Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 10:36:41 -0400 Subject: [PATCH 09/11] Remove verbosity from tests of examples --- tests/testthat/test-examples.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index b90f65b..5c84a0f 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -5,7 +5,7 @@ test_that("Functions to manage remake examples work properly", { expect_silent(list_examples_remake()) expect_gt(length(list_examples_remake()), 0) nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" - expect_error(example_remake(nonsense, verbose = F)) + expect_error(example_remake(nonsense)) for(example in list_examples_remake()){ expect_silent(example_remake(example)) expect_error(example_remake(example)) From f22e4cb7919269f00f18d3fe64099181959c4e01 Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sun, 30 Oct 2016 10:41:42 -0400 Subject: [PATCH 10/11] Test quickstart example separately. Other examples will need their own tests. --- tests/testthat/test-examples.R | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-examples.R b/tests/testthat/test-examples.R index 5c84a0f..a0fc13a 100644 --- a/tests/testthat/test-examples.R +++ b/tests/testthat/test-examples.R @@ -1,17 +1,19 @@ -# library(remake); library(testthat); example = "quickstart" +# library(remake); library(testthat); context("examples") -test_that("Functions to manage remake examples work properly", { +test_that("Basic checks on functions for examples are met.", { expect_silent(list_examples_remake()) expect_gt(length(list_examples_remake()), 0) nonsense = "alskdjfoijpaskjdfhasdhflkajhsdfkahdfkdsjf" expect_error(example_remake(nonsense)) - for(example in list_examples_remake()){ - expect_silent(example_remake(example)) - expect_error(example_remake(example)) - setwd(example) - make(verbose = F) - setwd("..") - unlink(example, recursive = TRUE) - } +}) + +test_that("Quickstart example runs properly.", { + example = "quickstart" + expect_silent(example_remake(example)) + expect_error(example_remake(example)) + setwd(example) + make(verbose = F) + setwd("..") + unlink(example, recursive = TRUE) }) From af0a2c7e1508421639536a8daac02810ca0debeb Mon Sep 17 00:00:00 2001 From: Will Landau Date: Sat, 5 Nov 2016 19:44:52 -0400 Subject: [PATCH 11/11] Add mustWork=TRUE in system.file() in examples.R --- R/examples.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/examples.R b/R/examples.R index d5397a4..2fef9bd 100644 --- a/R/examples.R +++ b/R/examples.R @@ -7,7 +7,7 @@ #' run \code{\link{list_examples_remake}}. example_remake = function(example = list_examples_remake()){ example <- match.arg(example) - dir <- system.file(file.path("examples", example), package = "remake") + dir <- system.file(file.path("examples", example), package = "remake", mustWork = TRUE) if(file.exists(example)) stop("There is already a file or folder named ", example, ".", sep = "") file.copy(from = dir, to = getwd(), recursive = TRUE)