R-package code: find out * which * package / namespace is in

Is there any path code in the R package that can determine which package or namespace it belongs to?


Reference Information. I find that I have common code between packages that just differs from the package name. One common example: tests/testthat.R :

 library(testthat) library(ShiftedExcitation) test_check("ShiftedExcitation") 

If the code can find out which package or namespace it belongs to, I could avoid a number of places where the package name is now indicated.


Now I am defining a hidden variable containing the package name, say

.PKG <- "ShiftedExcitation"

and then use something along the lines *

 library(testthat) library(.PKG, character.only = TRUE) test_check(.PKG) 

but I'm curious if there is a more elegant solution.

* I did not get this while testthat.R is evaluated outside the package namespace. However, it works to define the unittest function inside the package code.

+6
source share
1 answer

Approximation of the answer:

@MartinMorgan hint at using topenv () pretty close. But it turns out that when performing unit tests with testthat , testthat is in front of the package namespace in the search path.

So this is the current state:

 .findmyname <- function() { pkgs <- .packages () if (pkgs [1] == "testthat") pkgs [2] else pkgs [1] } 

This function finds the name of the package in question both inside the package and in tests/testthat.R . (Of course, any .findmyname () defined in the package is not known in tests/testthat.R before calling library ...)

0
source

All Articles