DavidC
DavidC

Reputation: 1441

Determine name of R package my code is in?

I'm working on an R package and (in the package's code) need to determine the version number when a certain function is called.

packageVersion("mypackage") works, but I'd rather not hard code the name of the package. How can I ask "what's the name of the package I'm in"? (Or directly get the version number of the package I'm in.)

Upvotes: 8

Views: 1280

Answers (3)

fdetsch
fdetsch

Reputation: 5308

While in my case (custom R package managed as .Rproj),

  • packageName() returned NULL, invisibly, and
  • getPackageName() returned ".GlobalEnv",

the following worked:

pkgload::pkg_name()

When developing your own package, bear in mind that {pkgload} itself comes with a handful of dependencies. Also, credit where credit is due – this answer was taken from here.

Upvotes: 0

Kenric D'souza
Kenric D'souza

Reputation: 41

I have not handled working with packages. But I am assuming you can use something like

packageVersion(getPackageName())

While you can supply parameters to getPackageName to search for the package name you are looking for, I think just supplying it without any parameters will get the current environment, (and in your case) the current package.

Source: The R Reference Index, available at https://cran.r-project.org/manuals.html

Upvotes: 3

DavidC
DavidC

Reputation: 1441

This mailing list thread describes packageName().

(As Martin pointed out in comments.)

Upvotes: 11

Related Questions