Create Disposable R Packages for Testing

Create disposable R packages for testing. You can create, install and load multiple R packages with a single function call, and then unload, uninstall and destroy them with another function call. This is handy when testing how some R code or an R package behaves with respect to other packages.


Linux Build Status Windows Build status CRAN RStudio mirror downloads Coverage Status

Features

The disposable packages are installed in R's temporary directory, so they are cleaned up at the end of the R session.

disposables cleans up after itself, if an error happens during the installation or loading of the disposable packages. If make_packages() fails because of an error, it leaves to temporary garbage behind. In particular,

  • it cleans up the library path and restores .libPaths(),
  • removes the temporary source package directories,
  • removes the installes packages from lib_dir, and
  • unloads the packages that it loaded before the error.

Installation

You can install this R package from Github:

devtools::install_github("gaborcsardi/disposables")

Usage

make_packages() creates, installs and loads R packages, it takes named expressions, the names will be used as package names.

library(disposables)
#> Loading required package: methods
pkgs <- make_packages(
  foo1 = { f <- function() print("hello!") ; d <- 1:10 },
  foo2 = { f <- function() print("hello again!") ; d <- 11:20 }
)

The foo1 and foo2 packages are now loaded.

"package:foo1" %in% search()
#> [1] TRUE
"package:foo2" %in% search()
#> [1] TRUE

You can dispose them with dispose_packages(). This unloads the packages and deletes them from the library directory.

dispose_packages(pkgs)
"package:foo1" %in% search()
#> [1] FALSE
"package:foo2" %in% search()
#> [1] FALSE
file.exists(pkgs$lib_dir)
#> [1] FALSE

Here is a real example that tests cross-package inheritence of R6 classes.

library(disposables)
library(testthat)
test_that("inheritance works across packages", {
 
  pkgs <- make_packages(
    imports = "R6",
 
    ## Code to put in package 'R6testA'
    R6testA = {
      AC <- R6Class(
        public = list(
          x = 1
        )
      )
    },
 
    ## Code to put in package 'R6testB'
    R6testB = {
      BC <- R6Class(
        inherit = R6testA::AC,
        public = list(
          y = 2
        )
      )
    }
 
  )
 
  ## In case of an error below
  on.exit(try(dispose_packages(pkgs), silent = TRUE), add = TRUE)
 
  ## Now ready for the tests
  B <- BC$new()
  expect_equal(B$x, 1)
  expect_equal(B$y, 2)
 
})

License

MIT @ Gábor Csárdi

News

1.0.3

  • The imports argument to make_packages() can be a list, with one value for each package.

  • The packages are installed using the --no-test-load argument now. The reason for this, is that install.packages runs in a new R session, with potentially different library directories, and some required might not be available when the load test is performed.

1.0.2

  • make_packages() now has a quiet argument, set to FALSE to see the installation of disposable packages. This is mainly useful for debugging.

1.0.1

  • Fix spurious R CMD check warnings and notes.

1.0.0

First version on CRAN.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("disposables")

1.0.3 by Gábor Csárdi, 2 years ago


https://github.com/gaborcsardi/disposables


Report a bug at https://github.com/gaborcsardi/disposables/issues


Browse source code at https://github.com/cran/disposables


Authors: Gábor Csárdi


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports utils

Depends on methods

Suggests covr, testthat


Suggested by modules, pkgconfig.


See at CRAN