Skip to content
This repository
Browse code

Merge pull request #210 from hadley/depends

Require packages in Depends. Fixes #161, #178, #192.
  • Loading branch information...
commit fa70de671b8a76606c3f52e5ab301eb0de233149 2 parents 7479c55 + 1cfcbd3
Hadley Wickham authored
1  DESCRIPTION
@@ -82,3 +82,4 @@ Collate:
82 82
     'check_devtools.r'
83 83
     'rcpp-attributes.r'
84 84
     'cran.r'
  85
+    'load-depends.r'
2  NEWS
@@ -5,6 +5,8 @@ NEW FEATURES
5 5
 
6 6
 * Rcpp attributes are now automatically compiled during build.
7 7
 
  8
+* Packages listed in depends are `require()`d (Fixes #161, #178, #192)
  9
+
8 10
 MINOR FEATURES
9 11
 
10 12
 * `check_cran` now downloads packages from cran.rstudio.com.
7  R/imports-env.r
@@ -45,13 +45,11 @@ imports_env_name <- function(pkg = ".") {
45 45
 #' the dependency packages.
46 46
 #'
47 47
 #' @keywords internal
48  
-load_imports <- function(pkg = ".", deps = c("depends", "imports")) {
  48
+load_imports <- function(pkg = ".") {
49 49
   pkg <- as.package(pkg)
50 50
 
51 51
   # Get data frame of dependency names and versions
52  
-  deps <- lapply(pkg[deps], parse_deps)
53  
-  deps <- Reduce(rbind, deps)
54  
-
  52
+  deps <- parse_deps(pkg$imports)
55 53
   if (is.null(deps) || nrow(deps) == 0) return(invisible())
56 54
 
57 55
   # If we've already loaded imports, don't load again (until load_all
@@ -66,7 +64,6 @@ load_imports <- function(pkg = ".", deps = c("depends", "imports")) {
66 64
   invisible(deps)
67 65
 }
68 66
 
69  
-
70 67
 # Load imported objects
71 68
 # The code in this function is taken from base::loadNamespace
72 69
 process_imports <- function(pkg = ".") {
12  R/load-depends.r
... ...
@@ -0,0 +1,12 @@
  1
+load_depends <- function(pkg = ".") {
  2
+  pkg <- as.package(pkg)
  3
+
  4
+  # Get data frame of dependency names and versions
  5
+  deps <- parse_deps(pkg$depends)
  6
+  if (is.null(deps) || nrow(deps) == 0) return(invisible())
  7
+
  8
+  mapply(check_dep_version, deps$name, deps$version, deps$compare)
  9
+  lapply(deps$name, require, character.only = TRUE)
  10
+
  11
+  invisible(deps)
  12
+}
5  R/load.r
@@ -110,7 +110,8 @@ load_all <- function(pkg = ".", reset = FALSE, recompile = FALSE,
110 110
 
111 111
   out <- list(env = ns_env(pkg))
112 112
 
113  
-  # Load dependencies into the imports environment
  113
+  # Load dependencies
  114
+  load_depends(pkg)
114 115
   load_imports(pkg)
115 116
 
116 117
   out$data <- load_data(pkg)
@@ -126,7 +127,7 @@ load_all <- function(pkg = ".", reset = FALSE, recompile = FALSE,
126 127
   # Set up the exports in the namespace metadata (this must happen after
127 128
   # the objects are loaded)
128 129
   setup_ns_exports(pkg, export_all)
129  
-  
  130
+
130 131
   # Set up the package environment ------------------------------------
131 132
   # Create the package environment if needed
132 133
   if (!is_attached(pkg)) attach_ns(pkg)
11  inst/tests/depends/DESCRIPTION
... ...
@@ -0,0 +1,11 @@
  1
+Package: dependmissing
  2
+Title: Tools to make developing R code easier
  3
+License: GPL-2
  4
+Description:
  5
+Author: Hadley <h.wickham@gmail.com>
  6
+Maintainer: Hadley <h.wickham@gmail.com>
  7
+Version: 0.1
  8
+Depends:
  9
+    MASS,
  10
+    R
  11
+Collate: a.r b.r
0  inst/tests/depend-missing/R/a.r → inst/tests/depends/R/a.r
File renamed without changes
0  inst/tests/depend-missing/DESCRIPTION → inst/tests/import-missing/DESCRIPTION
File renamed without changes
0  inst/tests/depend-version/R/a.r → inst/tests/import-missing/R/a.r
File renamed without changes
0  inst/tests/depend-version/DESCRIPTION → inst/tests/import-version/DESCRIPTION
File renamed without changes
0  inst/tests/depend-version/NAMESPACE → inst/tests/import-version/NAMESPACE
File renamed without changes
1  inst/tests/import-version/R/a.r
... ...
@@ -0,0 +1 @@
  1
+a <- 1
0  inst/tests/depend-version/R/b.r → inst/tests/import-version/R/b.r
File renamed without changes
14  inst/tests/test-depend.r
@@ -2,8 +2,8 @@ context("Dependencies")
2 2
 
3 3
 test_that("Warned about dependency versions", {
4 4
   # Should give a warning about grid version
5  
-  expect_warning(load_all("depend-version"), "Need grid >=")
6  
-  unload("depend-version")
  5
+  expect_warning(load_all("import-version"), "Need grid >=")
  6
+  unload("import-version")
7 7
 
8 8
   # TODO: Add check for NOT giving a warning about compiler version
9 9
   # Not possible with testthat?
@@ -12,12 +12,18 @@ test_that("Warned about dependency versions", {
12 12
 
13 13
 test_that("Error on missing dependencies", {
14 14
   # Should give a warning about grid version
15  
-  expect_error(load_all("depend-missing"), "missingpackage not available")
  15
+  expect_error(load_all("import-missing"), "missingpackage not available")
16 16
 
17 17
   # Loading process will be partially done; unload it
18  
-  unload("depend-missing")
  18
+  unload("import-missing")
19 19
 })
20 20
 
  21
+test_that("Packages in depends are required", {
  22
+  load_all("depends")
  23
+  expect_true("package:MASS" %in% search())
  24
+  unload("depends")
  25
+  detach("package:MASS", unload = TRUE)
  26
+})
21 27
 
22 28
 test_that("Parse dependencies", {
23 29
   deps <- parse_deps("\nhttr (< 2.1),\nRCurl (>= 3),\nutils (== 2.12.1),\ntools,\nR (>= 2.10),\nmemoise")
2  man/load_imports.Rd
@@ -2,7 +2,7 @@
2 2
 \alias{load_imports}
3 3
 \title{Load all of the imports for a package}
4 4
 \usage{
5  
-  load_imports(pkg = ".", deps = c("depends", "imports"))
  5
+  load_imports(pkg = ".")
6 6
 }
7 7
 \description{
8 8
   The imported objects are copied to the imports

0 notes on commit fa70de6

Please sign in to comment.
Something went wrong with that request. Please try again.