List objects in sysdata.rda from within an R package - r

I'm building a package that contains a number of look up tables, hidden from the user by storing them in R/sysdata.rda. This works fine and I am able to reference them from internal package functions directly or via get.
Is there a way to get a vector of object names contained in sysdata.rda from within a function inside the package? What about as a user?
The behavior I am looking for would be similar to how ls lists the objects in an environment.

The way I use is to have an internal function to generate the sysdata.R It can then also generate the vector of names within the limited scope of the function. You can then add the list of names to sysdata.R itself.
Or, if it is more complicated, have the function save the tables in a new environment: you can then ls the new environment for the list, and save the contents into sysdata.R.

Related

Update locked dataframe called as follows secondpackage::dataframe1 in R environment using bindenv functions?

I am developping a R package, for example called myfirstpackage.
Inside this package, I load a secondpackage from which I use a dataframe as follows secondpackage::dataframe1.
I call it like this, several times, inside myfirstpackage.
I would like to be able to modify secondpackage::dataframe1 in my main script to add rows to this dataframe.
I want to do it just before being called from functions of myfirstpackage.
In another words, I want to update a dataframe from a package loaded in the environment
secondpackage::dataframe1 <- rbind(secondpackage::dataframe1,aCustomDataframe2)
Is this possible using tricks from the environment like unlockBinding ?
So then secondpackage::dataframe1 is update everywhere in the next functions that are calling it ? Possible ?

Search string against variable names of internal variables in an R package

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.

display a list of R functions, so you don't steal an already used name

What's the command to display a list of all functions in R (baring in mind the packages you have installed)? I want to write a function called new_function and I want to make sure the name is not in use already. I thought it was ls() or ls(function) but these don't work.

Generate a call to a package function programatically given vector of package names

In my work I develop R packages that export R data objects (.RData). The name of these .RData files is always the same (e.g. files.RData). These packages also define and export a function that uploads the data to my database, say upload_data(). Inside upload_data() I first load the data using data(files, package = "PACKAGE NAME") and then push it into my database.
Let's say I have two packages, package1 and package2, which live on my file system. Given a vector of the package names (c("package1", "package2")), how would I go about to call 'upload_data()' programatically? Specifically, inside a script, how would I construct a call using "::" notation that constructs and evaluates a call like this: package1::upload_data()). I tried 'call' but couldn't get it right.
You could go the route of constructing the call using :: notation and evaluating that - but it's probably just easier to directly use get and specify the package you want to grab from.
get("upload_data", envir = asNamespace("package1"))
will return the function the same as using package1::upload_data would but is much easier to deal with programatically.

Creating a data.frame for R package

I am making an R package, and there is a need to keep track of files that were opened using the functions in the package.
What is the recommended procedure for creating R objects (in this case, a data.frame) upon loading the package in a way that is (sufficiently) hidden from the user? I do not want the user to manually edit the data.frame.
One idea I had was to create a data.frame in the options settings inside of an .onLoad call (similar to what Hadley does in his devtools package here), but the list of opened files is not really a configurable "option" in my package. Is there another way?
When you create an R package, unless you're exporting all objects, you have to list which objects are exported in the NAMESPACE file. If you need to maintain a data frame within your package but you don't want it made available to the user, you can choose not to export it by excluding it from the list.

Resources