https://github.com/gaborcsardi/disposables
Create disposable R packages, for testing
https://github.com/gaborcsardi/disposables
Last synced: about 1 month ago
JSON representation
Create disposable R packages, for testing
- Host: GitHub
- URL: https://github.com/gaborcsardi/disposables
- Owner: gaborcsardi
- License: other
- Created: 2014-10-11T03:17:23.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-04-25T07:48:02.000Z (12 months ago)
- Last Synced: 2025-03-11T05:48:32.993Z (about 1 month ago)
- Language: R
- Size: 64.5 KB
- Stars: 12
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - gaborcsardi/disposables - Create disposable R packages, for testing (R)
README
```{r, setup, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(
comment = "#>",
tidy = FALSE,
error = FALSE)
```# Disposable R packages, for testing purposes
[](https://github.com/gaborcsardi/disposables/actions/workflows/R-CMD-check.yaml)
[](http://www.r-pkg.org/pkg/disposables)
[](http://www.r-pkg.org/pkg/disposables)
[](https://app.codecov.io/gh/gaborcsardi/disposables?branch=main)## 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
Install the package from CRAN:
```{r eval = FALSE}
install.packages("disposables")
```## Usage
`make_packages()` creates, installs and loads R packages, it takes named
expressions, the names will be used as package names.```{r}
library(disposables)
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.
```{r}
"package:foo1" %in% search()
"package:foo2" %in% search()
```You can dispose them with `dispose_packages()`. This unloads the packages
and deletes them from the library directory.```{r}
dispose_packages(pkgs)
"package:foo1" %in% search()
"package:foo2" %in% search()
file.exists(pkgs$lib_dir)
```Here is a real example that tests cross-package inheritence of
[R6 classes](https://github.com/wch/R6).```{r}
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