Change Internal Function of a Package

Change internal function of a package

Try

?assignInNamespace

to replace .HMR.fit1 in the HMR package with your version.

Possible duplicate of :

How do I override a non-visible function in the package namespace?

My modified version of package function can't find the package's other internal functions

If you need your function to access "private" functions from a namespace, you can set the environment of your function. Normally functions automatically take the environment of where they are defined. But you can programatically change that.

If you just do

environment(my.rma.uni) <- as.environment("package:metafor")

Then the my.rma.uni function will look up any free symbols in the metafor namespace rather than the global namespace. You can do this without messing with locking/unlocking the binding and assigning in the namespace. You only need to do what if you want to completely replace the function in the package. Most times it's better just to create your own copy in the global namespace.

Modify package function

I finally found a solution that should work in all situations!

environment(customGenomePlot) <- asNamespace('snapCGH')
assignInNamespace("genomePlot", customGenomePlot, ns = "snapCGH")

The call to environment() assures that the function will be able to call other hidden functions from the package.

The call to assignInNamespace() assures that other functions from the package will call your updated version of the function.

It is possible that in certain situation, you need only one of these, but in general you need both. I struggled to find this general solution, found many other which are not working in some cases, like this (need opposite order), or this (misses the second part), or this (throws the error "cannot add bindings to a locked environment").

Altering internal data in R package

You can use options(). Create an option with

options(myPackageRepositoryPath = "some/path")

and retrieve it

path <- getOption(myPackageRepositoryPath)

The same way as you set an option, you also can overwrite an option:

setpath<-function(path){
options(myPackageRepositoryPath = path)
}

How do I override a non-visible function in the package namespace?

Use fixInNamespace. :)

fixInNamespace("predict.ar", "stats")

or

fixInNamespace("predict.ar", pos="package:stats")

(Several years later...)

From Nicholas H's comment: if you want to push some code to CRAN that depends upon an internal function from another package, it will throw a build warning and be rejected by R-core. If you want that internal function, you should just take a copy of it using the ::: operator and maintain it yourself.

predict.ar <- stats:::predict.ar

How to change internal function in MendelianRandomization function in order to change decimal places in output?

The EggerObject is an "S4" list and so you can access the value of the Pvalue.Est item using the S4 access function "@". (It's like "$" for ordinary S3 objects.) The explicit print function allows you control of the degree of precision:

 print( EggerObject@Pvalue.Est, digits=10)

It's possible that there is an accessor-function for objects of class-"Egger". If such a function exists it would be preferable, since the package author likely knows of potential pitfalls with use of different components of such an object.

Editing a function from a package in R?

In the beginning of the package there is a line

data(sysdata, envir=environment())

See here: https://github.com/cran/referenceIntervals/tree/master/data/sysdata.rda

I suspect that "nonparRanks" is defined there as I don't see it defined anywhere else. So perhaps you could download that file, write your own function, then run that same line before running your function and it may work.

EDIT:
Download the file then run:

load("C:/sysdata.rda")

With your path to the file and then your function will work.



Related Topics



Leave a reply



Submit