I have an R package with bunch of functions (all are exported). One of the function calls another function internally while running. But when I run this function via namespace without loading the entire package, it throws an error: 'could not find function ..'
For e.g:
foo and bar are two functions. foo calls bar internally. But when I call foo via namespace i.e, mypackage::foo() it throws an error could not find function "bar". If I load the library first and run via namespace it runs properly.
#doesnot work
mypackage::foo()
could not find function "bar"
#works
library(mypackage)
mypackage::foo()
I have exported both function and both are present in NAMESPACE file.
The problem with using the :: or ::: fuc[nctions is that only the function code is pulled into the workspace. So none of ancillary or supporting functions in that mypackage are pulled into the "interpreter space", i.e. the objects that the interpreter can see. So you option 2 "works" and your option 1 fails. For what seem to me to be obvious reasons.
When I am hacking a function that I know is derived from a loaded a namespace, I generally finish the hacking process with:
environment(hacked_fun) <- environment(orig_fun)
But that's not an option when you pull code from disk with the "mult-colon-functions". This is also a major reason why experienced R programmmers do their development in packages.The coding discipline of making a package also supports defining package dependencies.
Related
I would like to know of some packages that make use of assignInMyNamespace and what it's used for, if it is even advisable to use this function in production code. The help page gives the following information:
assignInMyNamespace is intended to be called from functions within a package, and chooses the namespace as the environment of the function calling it.
However it also gives the following warning about assignInNamespace:
assignInNamespace should not be used in final code, and will in future throw an error if called from a package. Already certain uses are disallowed.
Presumably this is because packages shouldn't ever try to change the namespaces of other packages, which is why the warning doesn't apply to assignInMyNamespace. Is this true?
NB: I am developing a package with an unexported testing function that allows any unexported function in the package to be temporarily replaced with one that saves its inputs and outputs. I am also considering such a technique for switching between memoised / un-memoised versions of functions.
EDIT: In practise, assignInMyNamespace only changes unexported functions - not 'any function in the package' as previously stated. I only realised this recently, and it's actually thrown a spanner in the works with the package I'm developing. Therefore I would also be very interested to know if there is a solution to the problem that works across both exported and unexported functions during package use.
I am working on a package and currently it has a lot of functions. Inorder to load them every time I open up RStudio, I use this line of code from devtools:
library(devtools)
suppressMessages(load_all("~/Codes/package1/"))
It works fine, but the problem is whenever I change a function that has been used in another function, R doesn't recognize the changes.
For Example if I have:
func1 <- function() {
print("version1")
}
func2 <- function() {
func1()
}
And then change func1 to print("Vesion2"), rerun it and then run func2, it would still print version1 for me.
Anyone knows whats the problem and how can I solve it?
The devtools load_all function simulates loading a package. All functions from a package are stored in a package namespace. Functions remember what namespace they come from via their environment().
Any code you run in the console runs in the global environment. So when you run
func1 <- function() {print("version2")}
you are creating a new function called func1 in your global environment but the func1 from the package namespace is still there. You've created a "shadow" function that masks the original function.
When you got to run func2 which is still in the package namespace, it sees a call to a function named func1. When it goes to look for this function, it looks first in it's own namespace due to R's lexical scoping rules. It finds the original funct1 and not the one you created in the global environment so it runs that.
Packages generally aren't meant to have their functions swapped or altered after they are loaded. You would save to save the source and call load_all to reload that folder as a package with the new changes. If you aren't really trying to simulate a package, importing functions with source() will not create a new namespace and would therefore be easier to edit after import.
I created a package with some functions which are helpful at my company. Recently, I restructered the package such that there are helper functions which need not to be accessible for everyone, but are called internally from other (exported) functions of the package. These helper functions are not exported to the namespace (no #' #export in the respective .R files).
Now, when I call one of the "major" (exported) functions, I get the error message (no real function names):
Error in major_function() : could not find function "helper_function"
Im fairly new in building packages, but from what I understood so far (from https://cran.r-project.org/web/packages/roxygen2/vignettes/namespace.html), it should neither be necessary to export the helper functions, nor to add #' importFrom my_package helper_function to the .R file of the major function.
When I tried this, it actually produced errors when checking the package. I also tried to call the helper functions with my_package:::helper_function, but this lead to the note that it should almost never be necessary to call functions from the same package like this.
Maybe useful information:
The error occurs only when I call a major_function_1 which internally calls major_function_2 which calls a helper_function.
I think there is more to your problem than what you state. As long as all your functions are defined in the same namespace (this also means that all your functions need to live in .R files in the same folder), the calling function should find the helper-functions accordingly.
I suspect you have your helper functions nested in some way, and that is causing the problem.
I recommend to recheck your namespace structure, or post a simplistic outline of your package here.
Another reason that could come to mind, is that you do not export your 'mayor_function2' in your NAMESPACE-file in your package root (maybe you have not recompiled the Roxygen documentation generating this file), and additionally have a local shadow of the the calling function 'mayor_function1'. Try to check this and rerun from a clean compile.
I am developing an R package (let's call it pkg), and I have defined a few generics. The NAMESPACE file contains both
S3method(foo, bar)
export(foo)
However, upon calling pkg::foo.bar, I get the dreaded
Error: 'foo.bar' is not an exported object from 'namespace:pkg'
I am using roxygen2, if that changes anything.
Note: If I explicitly add export(foo.bar), then everything works.
I am writing an R package. Generally, I have some functions that they are not useful for external uses. So when I put them in Namespace file, it causes an error about documentation of functions. On the other hand, if I remove them from Namespace file, it causes another problem, Function not found. So, is there any way of calling a function without a need of writing documentations?
As Andrie commented if you want to include the function in the R package you need to put it inside a folder (e.g. packageparent/R/) and declare in NAMESPACE. You do not put a function in NAMESPACE.
IF you do not want to include it in your package, none of your functions in your package shall call this function, otherwise the package does not compile. You still can include this function in your package and not write any documentation for it.
To use this function outside your package just source it