Steph Locke
Steph Locke

Reputation: 6146

How to test and compare multiple versions of R functions for a package?

I use unit tests via testthat for many of my simpler functions in R but where I have more complex functions combining simple functions and business logic. I'd like to test the cumulative impact via a before and after view for a sample of inputs. Ideally, I'd like to do this for a variety of candidate changes.

At the moment I'm:

  1. Using Rmarkdown documents
  2. Loading the package as-is
  3. Getting my sample
  4. Running my sample through the package as-is and outputting table of results
  5. sourceing new copies of functions
  6. Running my sample again and outputting table of results
  7. Reloading package and sourceing different copies of functions as required

This has proven difficult due to some functions that sit in the package namespace still running the package versions of functions, making results unreliable unless I thoroughly reload all downstream dependencies of functions. Additionally, the mechanism is complex to manage and difficult to reuse.

Is there a better strategy for testing candidate changes in R packages?

Upvotes: 4

Views: 492

Answers (2)

Steph Locke
Steph Locke

Reputation: 6146

I've reduced this issue by creating a sub-package in my impact analysis folder that contains the amended functions.

I then use devtools::load_all to load these new function versions up.

I can then compare and contrast results by accessing the originals via the namespace e.g. myoriginalpackage:::testfunction whilst looking at the new ones with testfunction

Upvotes: 1

Berry Boessenkool
Berry Boessenkool

Reputation: 1538

Maybe you can replace steps 5 and 7 with "Rcmd build YourPackage" on each version. Then with

install.packages("Path/To/MyPackage_1.1.tar.gz", type="source")

test the old version, and then replace 1.1 with 1.2

Upvotes: 0

Related Questions