Skip to content

Commit

Permalink
Merge pull request #210 from hadley/depends
Browse files Browse the repository at this point in the history
Require packages in Depends. Fixes #161, #178, #192.
  • Loading branch information
hadley committed Dec 14, 2012
2 parents 7479c55 + 1cfcbd3 commit fa70de6
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -82,3 +82,4 @@ Collate:
'check_devtools.r'
'rcpp-attributes.r'
'cran.r'
'load-depends.r'
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -5,6 +5,8 @@ NEW FEATURES

* Rcpp attributes are now automatically compiled during build.

* Packages listed in depends are `require()`d (Fixes #161, #178, #192)

MINOR FEATURES

* `check_cran` now downloads packages from cran.rstudio.com.
Expand Down
7 changes: 2 additions & 5 deletions R/imports-env.r
Expand Up @@ -45,13 +45,11 @@ imports_env_name <- function(pkg = ".") {
#' the dependency packages.
#'
#' @keywords internal
load_imports <- function(pkg = ".", deps = c("depends", "imports")) {
load_imports <- function(pkg = ".") {
pkg <- as.package(pkg)

# Get data frame of dependency names and versions
deps <- lapply(pkg[deps], parse_deps)
deps <- Reduce(rbind, deps)

deps <- parse_deps(pkg$imports)
if (is.null(deps) || nrow(deps) == 0) return(invisible())

# If we've already loaded imports, don't load again (until load_all
Expand All @@ -66,7 +64,6 @@ load_imports <- function(pkg = ".", deps = c("depends", "imports")) {
invisible(deps)
}


# Load imported objects
# The code in this function is taken from base::loadNamespace
process_imports <- function(pkg = ".") {
Expand Down
12 changes: 12 additions & 0 deletions R/load-depends.r
@@ -0,0 +1,12 @@
load_depends <- function(pkg = ".") {
pkg <- as.package(pkg)

# Get data frame of dependency names and versions
deps <- parse_deps(pkg$depends)
if (is.null(deps) || nrow(deps) == 0) return(invisible())

mapply(check_dep_version, deps$name, deps$version, deps$compare)
lapply(deps$name, require, character.only = TRUE)

invisible(deps)
}
5 changes: 3 additions & 2 deletions R/load.r
Expand Up @@ -110,7 +110,8 @@ load_all <- function(pkg = ".", reset = FALSE, recompile = FALSE,

out <- list(env = ns_env(pkg))

# Load dependencies into the imports environment
# Load dependencies
load_depends(pkg)
load_imports(pkg)

out$data <- load_data(pkg)
Expand All @@ -126,7 +127,7 @@ load_all <- function(pkg = ".", reset = FALSE, recompile = FALSE,
# Set up the exports in the namespace metadata (this must happen after
# the objects are loaded)
setup_ns_exports(pkg, export_all)

# Set up the package environment ------------------------------------
# Create the package environment if needed
if (!is_attached(pkg)) attach_ns(pkg)
Expand Down
11 changes: 11 additions & 0 deletions inst/tests/depends/DESCRIPTION
@@ -0,0 +1,11 @@
Package: dependmissing
Title: Tools to make developing R code easier
License: GPL-2
Description:
Author: Hadley <h.wickham@gmail.com>
Maintainer: Hadley <h.wickham@gmail.com>
Version: 0.1
Depends:
MASS,
R
Collate: a.r b.r
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions inst/tests/import-version/R/a.r
@@ -0,0 +1 @@
a <- 1
File renamed without changes.
14 changes: 10 additions & 4 deletions inst/tests/test-depend.r
Expand Up @@ -2,8 +2,8 @@ context("Dependencies")

test_that("Warned about dependency versions", {
# Should give a warning about grid version
expect_warning(load_all("depend-version"), "Need grid >=")
unload("depend-version")
expect_warning(load_all("import-version"), "Need grid >=")
unload("import-version")

# TODO: Add check for NOT giving a warning about compiler version
# Not possible with testthat?
Expand All @@ -12,12 +12,18 @@ test_that("Warned about dependency versions", {

test_that("Error on missing dependencies", {
# Should give a warning about grid version
expect_error(load_all("depend-missing"), "missingpackage not available")
expect_error(load_all("import-missing"), "missingpackage not available")

# Loading process will be partially done; unload it
unload("depend-missing")
unload("import-missing")
})

test_that("Packages in depends are required", {
load_all("depends")
expect_true("package:MASS" %in% search())
unload("depends")
detach("package:MASS", unload = TRUE)
})

test_that("Parse dependencies", {
deps <- parse_deps("\nhttr (< 2.1),\nRCurl (>= 3),\nutils (== 2.12.1),\ntools,\nR (>= 2.10),\nmemoise")
Expand Down
2 changes: 1 addition & 1 deletion man/load_imports.Rd
Expand Up @@ -2,7 +2,7 @@
\alias{load_imports}
\title{Load all of the imports for a package}
\usage{
load_imports(pkg = ".", deps = c("depends", "imports"))
load_imports(pkg = ".")
}
\description{
The imported objects are copied to the imports
Expand Down

0 comments on commit fa70de6

Please sign in to comment.