Dirk
Dirk

Reputation: 1314

R with testthat: Get list of exported functions

I ship a text file with all exported functions listed. To make sure, that all functions are listed, I would like to create a unit test via testthat and compare all exported function with the one in the text file. My current approach reads in the file and compares it with ls("package:myPackage"). But this call returns a long list of all functions of all imported packages. Any ideas how to solve this?

A complete different approach would be to generate this file automatically. But I think the first approach is easier to realise. Hopefully.

Upvotes: 4

Views: 1404

Answers (1)

Dirk
Dirk

Reputation: 1314

Thanks to @Emmanuel-Lin here is my solution:

require(data.table)
test_that("Function List", {
  # read namespace and extract exported functions
  funnamespace = data.table(read.table(system.file("NAMESPACE", package = "PackageName"), stringsAsFactors = FALSE))
  funnamespace[, c("status", "fun") := tstrsplit(V1, "\\(")]
  funnamespace[, fun := tstrsplit(fun, "\\)")]
  # read function list
  funlist = read.csv2(system.file("subdirectory", "functionList.txt", package = "PackageName"), stringsAsFactors = FALSE)
  # test
  expect_equal(funnamespace[status == "export", fun], funlist[, 1])
})

Obviously, I was too lazy to work out the correct regular expression to replace the two tstrsplit by one row.

Upvotes: 2

Related Questions