Is there a way code in an R package can find out which package or namespace it belongs to?
Background: I find I have common code between packages that just differs in the package name. One common example is tests/testthat.R
:
library(testthat)
library(ShiftedExcitation)
test_check("ShiftedExcitation")
If the code could find out to which package or namespace it belongs, I could avoid a number of places where the package name is now given.
Right now I define a hidden variable that contains the package name, say
.PKG <- "ShiftedExcitation"
and then use something along the lines of *
library(testthat)
library(.PKG, character.only = TRUE)
test_check(.PKG)
but I'm curious whether a more elegant solution exists.
* I did not get this working so far as testthat.R
is evaluated outside the package namespace. It does work for defining a unittest
function inside the package code, though.