Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/davisvaughan/cross

Run Functions Across Package Versions
https://github.com/davisvaughan/cross

Last synced: 11 days ago
JSON representation

Run Functions Across Package Versions

Awesome Lists containing this project

README

        

---
output: github_document
editor_options:
chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# cross

[![R-CMD-check](https://github.com/DavisVaughan/cross/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/DavisVaughan/cross/actions/workflows/R-CMD-check.yaml) [![Codecov test coverage](https://codecov.io/gh/DavisVaughan/cross/branch/main/graph/badge.svg)](https://app.codecov.io/gh/DavisVaughan/cross?branch=main)

The goal of cross is to rerun a single function across a configuration of package versions or git branches.
The typical use case for this is creating a reprex containing a before and after benchmark, where you'd like to run the same code on both the `main` branch and your `branch/my-fix` branch, or using the CRAN version of a package against the dev version of a package.

## Installation

You can install the development version of cross like so:

``` r
pak::pak("DavisVaughan/cross")
```

## Example

`cross::run()` runs a function across various versions of the same package.
The `pkgs` can be any remote specification supported by `pak::pkg_install()`.

```{r, eval=FALSE}
library(bench) # So bench classes print nicely

out <- cross::run(pkgs = c("vctrs", "r-lib/vctrs"), ~{
library(vctrs)
x <- c(1, NA, 2, 3, NA)
bench::mark(missing = vec_detect_missing(x))
})

out
#> # A tibble: 2 × 2
#> pkg result
#>
#> 1 vctrs
#> 2 r-lib/vctrs

tidyr::unnest(out, result)
#> # A tibble: 2 × 14
#> pkg expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc
#>
#> 1 vctrs missing 816ns 968ns 942535. 2.23KB 0 10000 0
#> 2 r-lib/vctrs missing 760ns 959ns 936836. 2.25KB 0 10000 0
#> # ℹ 5 more variables: total_time , result , memory ,
#> # time , gc
```

`cross::run_branches()` runs a function across various local branches.
It assumes that:

- `usethis::proj_get()` points to an R package using git.
- There are no uncommitted git changes.

If those are true, then it will automatically run the function against the current branch and the `main` branch, but you can change this with the `current` and `branches` arguments.

```{r, eval=FALSE}
# Not run here, but assuming we are in the vctrs repo this would run the
# function against whatever branch we are on and the `main` branch

cross::run_branches(~{
library(vctrs)
x <- c(1, NA, 2, 3, NA)
bench::mark(missing = vec_detect_missing(x))
})
```