In R, I'm getting an error "could not find function...". The function is present inside the package. Still when I run the package, getting the error.
I am getting this error in the ChainLadder package while running MackChainLadderFunctions.R. For example, the function checktriangle is present inside the package in Chainladder.R. Still, R is not able to recognize the function or call the function.
Two problems here.
function names are case-sensitive (checkTriangle, not checktriangle)
checkTriangle is not exported from the package (i.e., it's a private function intended for use within the package only), so you need ::: to access it ... try ChainLadder:::checkTriangle.
Using private functions is "at your own risk/programmer beware"; private functions are undocumented, may change in future versions, etc.. If you can find a way to do what you need to do with public functions, that is generally preferred.
AFAICT you're running into this problem because you're trying to source() (or cut-and-paste) package code in your R session. This shouldn't happen if you load the package with library("ChainLadder") and use the public functions (if it does, please edit your question to give a little more context about how you're using the package ...)
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 have an R-package ('mill') with a 'suggest' ('hlt') where I would like to register a new class for an S3 method in 'hlt'. Because the 'hlt' package is a suggest I followed the advise of Hadley and copied a function in 'mill' that registers my new mill::html.pdf_diff_df() function but not until after loading the 'hlt' package:
https://github.com/r-lib/vctrs/blob/master/R/register-s3.R
The problem I encounter is that when I load 'mill' and call the html() method on the new class ('pdf_diff_df') defined in the 'mill' package I just get the error:
html(df)
Error in html(df) : could not find function "html"
I would, of course, like to give the warning that one should load the 'hlt' package at that point, otherwise the user doesn't understand the problem. Any ideas? The source in 'mill' of this new function is here:
https://github.com/SVA-SE/mill/blob/136f372f88d794bb6149922c24dd9a4f731e4c7e/R/images.R#L195-L206
If I understand the situation correctly, hlt exports html(), but you don't want a fixed dependency on hlt, so you can't import html from hlt.
In that case, simply use a fully qualified call to it, i.e. hlt::html(...).
Since hlt is only in Suggests, this should only be called conditional on requireNamespace('hlt') returning TRUE.
Edited to add:
If you want a user to be able to call html(...), you have to export it from your package. This is hard, because it is exported from the hlt package, and if the two packages export different versions, you'll get warnings about one hiding the other.
As far as I know there isn't a nice solution to this, but really, it's not your problem. If a user hasn't attached hlt, they have no reason to expect html(...) to do anything. If they have, it will just work.
If you really want to help out the user without generating new messages, you could export your own function with a different name, something like
html2 <- function(...) {
if (requireNamespace("hlt"))
hlt::html(...)
else
warning("Suggested package 'hlt' must be installed for this to work.")
}
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.
my R package has a function (my_func) that uses function(bar) from another package (foo) which only available on Unix. So I write my code this way, following the suggested packages in Writing R extension:
my_func = function(){
....
if (requireNamespace("foo", quietly = TRUE)) {
foo::bar(..) # also tried bar(...)
} else {
# do something else without `bar`
}
...
}
However, I keep getting such warning message when I run R CMD check:
'loadNamespace' or 'requireNamespace' call not declared from: foo
Is there any way to use platform specific packages without gives such warnings? I tried to include foo in the Description file and the warning disappeared, but then the package cannot be installed on windows because foo is not available for the win.
I think you misread that comment, though it is easy to do because the logic is not clear and incontrovertible.
In that first paragraph that talked about calling packages using require(package) they said it was OK for a test environment or vignette but not from inside a finished function to use library(package) or require(package).
This is considered bad form, because the packages you call inside a function will usurp the order of access your user has set up in her work environment when loading packages.
The method you use above:
package::function()
is the approved of way to use a function within a function without altering the current environment.
But in order to use that function you must still have that package installed on your current machine or in your current working environment (such as a virtualenv or ipython or jupytr..and yes R will work in all those environments).
You cannot run a function that R cannot see from the working environment...so you must have it installed, but not necessarily loaded, to call it.
I would like to add an idiosyncratically modified function to package written by someone else, with an R Script, i.e. just for the session, not permanently. The specific example is, let's say, bls_map_county2() added to the blscrapeR package. bls_map_county2 is just a copy of the bls_map_county() function with an added ... argument, for purposes of changing a few of the map drawing parameters. I have not yet inserted the additional parameters. Running the function as-is, I get the error:
Error in BLS_map_county(map_data = df, fill_rate = "unemployed_rate", :
could not find function "geom_map"
I assume this is because my function does not point to the blscrapeR namespace. How do I assign my function to the (installed, loaded) blscrapeR namespace, and is there anything else I need to do to let it access whatever machinery from the package it requires?
When I am hacking on a function in a particular package that in turn calls other functions I often use this form after the definition:
mod_func <- function( args) {body hacked}
environment(mod_func) <- environment(old_func)
But I think the function you might really want is assignInNamespace. These methods will allow the access to non-exported functions in loaded packages. They will not however succeed if the package is not loaded. So you may want to have a stopifnot() check surrounding require(pkgname).
There are two parts to this answer - first a generic answer to your question, and 2nd a specific answer for the particular function that you reference, in which the problem is something slightly different.
1) generic solution to accessing internal functions when you edit a package function
You should already have access to the package namespace, since you loaded it, so it is only the unexported functions that will give you issues.
I usually just prepend the package name with the ::: operator to the non exported functions. I.e., find every instance of a call to some_internal_function(), and replace it with PackageName:::some_internal_function(). If there are several different internal functions called within the function you are editing, you may need to do this a few times for each of the offending function calls.
The help page for ::: does contain these warnings
Beware -- use ':::' at your own risk!
and
It is typically a design mistake to use ::: in your code since the
corresponding object has probably been kept internal for a good
reason. Consider contacting the package maintainer if you feel the
need to access the object for anything but mere inspection.
But for what you are doing, in terms of temporarily hacking another function from the same package for your own use, these warnings should be safe to ignore (at you own risk, of course - as it says in the manual)
2) In the case of blscrapeR ::bls_map_county()
The offending line in this case is
ggplot2::ggplot() + geom_map(...
in which the package writers have specified the ggplot2 namespace for ggplot(), but forgotten to do so for geom_map() which is also part of ggplot2 (and not an internal function in blscrapeR ).
In this case, just load ggplot2, and you should be good to go.
You may also consider contacting the package maintainer to inform them of this error.