0

I am writing an R package which has internal variables stored in pkg/R/sysdata.rda. In the package, I have a function useVar(x) that should recognise if the string x is a variable name in sysdata.rda and if so return the corresponding variable.

What is best way(s) to do this?

Using ::: with strings

pkg:::var would help me but only if var can be specified as a string.

Listing internal variables

Another option would be to list the contents of sysdata.rda but I have not found a solution to this that feels right.

While this SO question is related, the answers are directed at package users rather than at package developers. Edit: I am assuming that internal variables should be called differently from within a package since they are not supposed to be seen by users.

Julie
  • 1
  • 3
  • Why not have one of the internal variables be a character vector with all the other internal variable names? Then you can check `var %in% pkg:::internal_vars` – Gregor Thomas Aug 25 '19 at 19:10
  • I believe what you're asking for in the question is `getFromNamespace`, e.g., `try(getFromNamespace(var, ns = "pkg"))`. That's not very different from the answer you link to--so it would be nice also if you would explain *why* the answer addressed at a package user is not working for you. – Gregor Thomas Aug 25 '19 at 19:17
  • @Gregor I did think of this at some point, but since I will add to the variables in sysdata.rda with time, it felt a bit inelegant to me to have to also update ```pkg:::internal_vars``` each time. Could be wrong. – Julie Aug 25 '19 at 19:19
  • @Gregor edited. I was unsure whether internal variables should be called differently from within a package since they are not supposed to be seen by users. Assuming that this isn't a worry, ```try(getFromNamespace(var, ns = "pkg"))``` does what I need. Thanks. – Julie Aug 25 '19 at 20:02

0 Answers0