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


Do you want to edit the function permanently? or just temporarily?

If you want a permanent change then you should get a source version of the package, modify the source code, then install the package from your changed source. Also consider contributing the changes back to the author/maintainer.

If you want a temporary change then try the command:

trace(genomePlot, edit=TRUE)

This will open an editor (you can also specify which editor if you don't want to use the default) with the source code and let you edit it. When you save and exit from the editor it will save your edited version in place of the original taking care of environments/namespaces/etc.

This change will only last for the current R session or until you call the untrace function.

Tags:

R