Should one load the dataset before or inside of the function?

Viewed 143

Q. How does one write an R package function that references an R package dataset in a way that is simple/friendly/efficient for an R user. For example, how does one handle afunction() that calls adataset from a package?

What I think may not be simple/friendly/efficient: User is required to run data(adataset) before running afunction(...) or else receiving an Error: ... object 'adataset' not found. I have noticed some packages have built-in datasets that can be called anytime the package is loaded, for example, iris, which one can call without bringing it to the Global Environment.

Possible options which I have entertained:

  1. Write data(NamedDataSet) directly into the function. Is this a bad idea. I thought perhaps it could be, looking at memory and given my limiting understanding of function environments.

  2. Code the structure of the dataset directly into the function. I think this works depending on the size of the data but it makes me wonder about how to go about proper documentation in the package.

  3. Change Nothing. Given a large enough dataset, maybe it does not make sense to implement a way different from reading it before calling the function.

Any comments are appreciated.

1 Answers
Related