Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How do I add an extra function to a package?

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?

like image 313
andrewH Avatar asked Oct 27 '16 22:10

andrewH


1 Answers

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).

like image 163
IRTFM Avatar answered Nov 14 '22 23:11

IRTFM