diff --git a/DESCRIPTION b/DESCRIPTION index c204215..9331e81 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: modules Title: Self Contained Units of Source Code -Version: 0.8.3 +Version: 0.8.4 Authors@R: person("Sebastian", "Warnholz", email = "wahani@gmail.com", role = c("aut", "cre")) Description: Provides modules as an organizational unit for source code. Modules enforce to be more rigorous when defining dependencies and have diff --git a/NAMESPACE b/NAMESPACE index a1ae764..780b203 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -21,6 +21,7 @@ export(getSearchPathNames) export(import) export(module) export(use) +importFrom(utils,data) importFrom(utils,download.file) importFrom(utils,install.packages) importFrom(utils,installed.packages) diff --git a/NEWS b/NEWS index 603a42e..4713352 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,8 @@ Version 0.9.0 - Bugfix from issue #16 on Github: extend a module which has been loaded from a file now actually works - Extending the export mechanism to allow for renaming: see #19 -- Reattaching a module in the .GlobalEnv now actually works. See #24. +- Reattaching a module in the .GlobalEnv now actually works. See #24 +- Importing a complete package now also imports datasets. See #29 Version 0.8.0 - CRAN release diff --git a/R/NAMESPACE.R b/R/NAMESPACE.R index 890c758..f637d76 100644 --- a/R/NAMESPACE.R +++ b/R/NAMESPACE.R @@ -1,4 +1,4 @@ -#' @importFrom utils download.file install.packages installed.packages +#' @importFrom utils data download.file install.packages installed.packages #' packageVersion str NULL diff --git a/R/import.R b/R/import.R index 4bb7ad1..96201aa 100644 --- a/R/import.R +++ b/R/import.R @@ -74,7 +74,7 @@ importCheckInstall <- function(pkg) { importGetSelection <- function(mc, pkg) { objectsToImport <- importDeparseEllipses(mc) - if (length(objectsToImport) == 0) getNamespaceExports(pkg) + if (length(objectsToImport) == 0) importGetNamespaceExports(pkg) else objectsToImport } @@ -87,3 +87,11 @@ importDeparseEllipses <- function(mc) { args <- unlist(args) deleteQuotes(args) } + +importGetNamespaceExports <- function(pkg) { + nsExports <- getNamespaceExports(pkg) + nsDatasets <- data(package = pkg) + nsDatasets <- nsDatasets$results[, "Item"] + nsDatasets <- gsub(" .*", "", nsDatasets) + c(nsExports, nsDatasets) +} diff --git a/man/amodule.Rd b/man/amodule.Rd index c0d4afd..87a8423 100644 --- a/man/amodule.Rd +++ b/man/amodule.Rd @@ -4,8 +4,8 @@ \alias{amodule} \title{Define Augmented and Parameterized Modules} \usage{ -amodule(expr = { }, envir = parent.frame(), enclos = baseenv(), - class = NULL) +amodule(expr = { +}, envir = parent.frame(), enclos = baseenv(), class = NULL) } \arguments{ \item{expr}{(expression) a module declaration, same as \link{module}} diff --git a/man/export.Rd b/man/export.Rd index 5953074..4350562 100644 --- a/man/export.Rd +++ b/man/export.Rd @@ -9,7 +9,7 @@ export(..., where = parent.frame()) \arguments{ \item{...}{(character, or unquoted expression) names to export from module. A character of length 1 with a leading "^" is interpreted as regular -expression.} +expression. Arguments can be named and used for renaming exports.} \item{where}{(environment) typically the calling environment. Should only be relevant for testing.} @@ -52,4 +52,9 @@ module({ bar <- function() "bar" }) +module({ + export(bar = foo) + foo <- function() "foo" +}) + } diff --git a/man/module.Rd b/man/module.Rd index f523b75..bc218e3 100644 --- a/man/module.Rd +++ b/man/module.Rd @@ -5,8 +5,8 @@ \alias{autoTopEncl} \title{Define Modules in R} \usage{ -module(expr = { }, topEncl = autoTopEncl(envir), - envir = parent.frame()) +module(expr = { +}, topEncl = autoTopEncl(envir), envir = parent.frame()) autoTopEncl(where) } diff --git a/man/modulecoerce.Rd b/man/modulecoerce.Rd index 9361a71..83d1c15 100644 --- a/man/modulecoerce.Rd +++ b/man/modulecoerce.Rd @@ -8,8 +8,7 @@ \usage{ as.module(x, ...) -\method{as.module}{character}(x, topEncl = baseenv(), reInit = TRUE, - ..., envir = parent.frame()) +\method{as.module}{character}(x, topEncl = baseenv(), reInit = TRUE, ..., envir = parent.frame()) \method{as.module}{module}(x, reInit = TRUE, ...) } diff --git a/man/use.Rd b/man/use.Rd index 1bdfa4a..b64373b 100644 --- a/man/use.Rd +++ b/man/use.Rd @@ -4,8 +4,7 @@ \alias{use} \title{Use a module as dependency} \usage{ -use(module, ..., attach = FALSE, reInit = TRUE, - where = parent.frame()) +use(module, ..., attach = FALSE, reInit = TRUE, where = parent.frame()) } \arguments{ \item{module}{(character, module) a file or folder name, or an object that diff --git a/tests/testthat/test-import.R b/tests/testthat/test-import.R index 32c6961..24f7ac8 100644 --- a/tests/testthat/test-import.R +++ b/tests/testthat/test-import.R @@ -1,3 +1,22 @@ +test_that("Import of datasets: #29", { + # import all datasets from a package + m <- module({ + import("datasets") + getIris <- function() iris + }) + data("iris", envir = environment()) + expect_equal(m$getIris(), iris) + expect_true("iris" %in% getSearchPathContent(m)[["modules:datasets"]]) + # import just one dataset, like any other object + m <- module({ + import("datasets", "iris") + getIris <- function() iris + }) + data("iris", envir = environment()) + expect_equal(m$getIris(), iris) + expect_true("iris" %in% getSearchPathContent(m)[["modules:datasets"]]) +}) + test_that("Imports of module", { # import and related functions are part of the parent scope. Not the module # itself.