Modify Package Function

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

Adding and Editing Functions of R-package

Solution: The following steps worked for me (on a Mac) to simultaneously solve both aspects:

  1. I downloaded package ABC as a tar file from the CRAN repository (file: "ABC_1.1-2.tar"). After opening the file by decompressing it via double-click, it shows the typical structure of Packages (metadata, vignettes, namespace, etc.) as described in the link provided by alistaire (see here - very helpful, many thanks).
  2. All the relevant files with the different algorithms (e.g. files "algo-A.R", "algo-B.R") are contained in the "R"-folder and inside the file "algo-A.R", I found the function ex_fct. I opened this file in R-Studio adjusted the ex_fct function as desired and added the nw_fct also to that file (because the ex_fct and nw_fct functions are related) and saved it under the same name, i.e. as "algo-A.R". As a result, I now have an updated package folder that contains my updated version of the "algo-A.R" file.
  3. Finally, I used the build function of the devtools package to create a single bundled ".tar" file (say file "ABC_new.tar") from this updated package folder. Specifically, one may simply use: build(pkg= "path1/ABC_1.1-2", path= "~path2/ABC_new.tar", manual=F, binary=F), where path1 navigates to the place of the updated package folder and path2 says where the bundled package shall be stored. Notice: As I did this on a new machine, this step did not work immediately but required to first install e.g. TeXLive, Java Applications as well as several packages required by the ABC package (simply follow R's error commands).
  4. Finally, I was able to (permanently) install the updated package archive file in RStudio via: install.packages(“~path2/ABC_new.tar", repos = NULL, type=“source”)

Should you wish to undo these changes and have the original package again, you may simply remove the package and re-install the original one from CRAN.

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?

Modify a package function (read.dcf) in R

So turns out pkgAvail calls utils::available.packages, and I need to modify the utils.available.packages. So I downloaded the miniCRAN project, defined a new function called available_packages inside pkgDep.R file which contains the pkgAvail function, modified the pkgAvail function so it calls my own available_packages, and then "clean and rebuild" the new miniCRAN project.

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.

Edit R function within a package

I ended up forking the repository in git, and creating my own branch. You can then download the modified branch from Git, uninstall the old package by simply removing the folder where it was installed - use .libPaths() to find its location - and then use install.packages('/path/to/newpackage', repos=NULL)

How to modify pre-existing function in local environment in R

If New is the new function copied from camtrapR then use

environment(New) <- asNamespace("camtrapR")

to ensure that the function calls in its body are looked up in the correct places.



Related Topics



Leave a reply



Submit