Skip to content

Commit

Permalink
Merge pull request #70 from rticulate/x/library_location
Browse files Browse the repository at this point in the history
Default of .library parameter, and order of loadNamespace() and getNamespaceExports()
  • Loading branch information
torfason committed Jun 5, 2022
2 parents 9ed18c8 + ce3ef0d commit 6e7a418
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,7 +1,7 @@
Package: import
Type: Package
Title: An Import Mechanism for R
Version: 1.3.0
Version: 1.3.0.9001
Authors@R:
c(person(given = "Stefan Milton",
family = "Bache",
Expand Down
15 changes: 15 additions & 0 deletions NEWS.md
@@ -1,3 +1,18 @@

Version 1.3.0.9xxx
=============

* The `import` package will now by default use the full current set of library
paths, i.e. the result of `.libPaths()`, while in versions up to and including
`1.3.0` this defaulted to use only the *first* entry in the library paths, i.e.
`.library=.libPaths()[1L]`.

* The order of `loadNamespace()` and `getNamespaceExports()` has been changed in
`import::from()`. While this is intended to be a bug fix, it is possible that
it affects usage in situations where specificity about the library path is
important.


Version 1.3.0
=============

Expand Down
7 changes: 4 additions & 3 deletions R/from.R
Expand Up @@ -57,7 +57,8 @@
#' \code{.into={environment()}} causes imports to be made into the current
#' environment; \code{.into=""} is an equivalent shorthand value.
#' @param .library character specifying the library to use when importing from
#' packages. Defaults to the latest specified library.
#' packages. Defaults to the current set of library paths (note that the
#' default value was different in versions up to and including \code{1.3.0}).
#' @param .directory character specifying the directory to use when importing
#' from modules. Defaults to the current working directory. If .from is a
#' module specified using an absolute path (i.e. starting with \code{/}),
Expand Down Expand Up @@ -99,7 +100,7 @@
#' }
#' @md
from <- function(.from, ..., .into = "imports",
.library = .libPaths()[1L], .directory=".",
.library = .libPaths(), .directory=".",
.all=(length(.except) > 0), .except=character(),
.chdir = TRUE, .character_only = FALSE, .S3 = FALSE)
{
Expand Down Expand Up @@ -214,12 +215,12 @@ from <- function(.from, ..., .into = "imports",
} else {
# Load the package namespace, which is passed to the import calls.
spec <- package_specs(.from)
all_objects <- getNamespaceExports(spec$pkg)
pkg <- tryCatch(
loadNamespace(spec$pkg, lib.loc = .library,
versionCheck = spec$version_check),
error = function(e) stop(conditionMessage(e), call. = FALSE)
)
all_objects <- getNamespaceExports(spec$pkg)
pkg_name <- spec$pkg
}
# If .all parameter was specified, override with list of all objects
Expand Down
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -113,11 +113,16 @@ section of the package vignette.

### Choosing where import looks for packages or modules

The `import` package will by default only use the latest specified library
(i.e. the result of `.libPaths()[1L]`). It is possible to specify a different
library using the `.library` argument in any of the `import` functions.
One import call can only use *one* library so there will not be ambiguity
as to where imports come from.
The `import` package will by default use the current set of library paths, i.e.
the result of `.libPaths()`. It is, however, possible to specify a different set
of library paths using the `.library` argument in any of the `import` functions,
for example to import packages installed in a custom location, or to remove any
ambiguity as to where imports come from.

Note that in versions up to and including `1.3.0` this defaulted to use only the
*first* entry in the library paths, i.e. `.library=.libPaths()[1L]`. We believe
the new default is applicable in a broader set of circumstances, but if this
change causes any issues, we would very much appreciate hearing about it.

When importing from a module (.R file), the directory where `import` looks for
the module script can be specified with the with `.directory` parameter.
Expand Down
5 changes: 3 additions & 2 deletions man/importfunctions.Rd

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

2 changes: 0 additions & 2 deletions tests/test_import/test_from.R
Expand Up @@ -176,7 +176,6 @@ test_that("Importing .into a symbol works w/ .character_only=TRUE", {
})

test_that("Imports from libraries NOT defined in .libPaths work", {
testthat::skip("Implementation of this fix has been reverted")
tmp_install_dir <- tempdir()
if (!file.exists("packageToTest_0.1.0.tar.gz")) {
system("R CMD build packageToTest")
Expand Down Expand Up @@ -209,7 +208,6 @@ test_that("Functions named `get` in arbitrary environment on search path do not
})

test_that("Functions named `get` exported from packages do not mask base::get", {
skip("Implementation of fix allowing custom .libPaths has been reverted")
tmp_install_dir <- tempdir()
library(packageToTest, lib.loc = tmp_install_dir)
expect_true("get" %in% getNamespaceExports("packageToTest"))
Expand Down
15 changes: 10 additions & 5 deletions vignettes/import.Rmd
Expand Up @@ -352,11 +352,16 @@ cause it to be sourced twice.

### Choosing where import looks for packages or modules

The `import` package will by default only use the latest specified library
(i.e. the result of `.libPaths()[1L]`). It is possible to specify a different
library using the `.library` argument in any of the `import` functions.
One import call can only use *one* library so there will not be ambiguity
as to where imports come from.
The `import` package will by default use the current set of library paths, i.e.
the result of `.libPaths()`. It is, however, possible to specify a different set
of library paths using the `.library` argument in any of the `import` functions,
for example to import packages installed in a custom location, or to remove any
ambiguity as to where imports come from.

Note that in versions up to and including `1.3.0` this defaulted to use only the
*first* entry in the library paths, i.e. `.library=.libPaths()[1L]`. We believe
the new default is applicable in a broader set of circumstances, but if this
change causes any issues, we would very much appreciate hearing about it.

When importing from a module (.R file), the directory where `import` looks for
the module script can be specified with the with `.directory` parameter.
Expand Down

0 comments on commit 6e7a418

Please sign in to comment.