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:
- 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).
- 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 theex_fct
function as desired and added thenw_fct
also to that file (because theex_fct
andnw_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. - Finally, I used the
build
function of thedevtools
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)
, wherepath1
navigates to the place of the updated package folder andpath2
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). - 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
Can Dplyr Join on Multiple Columns or Composite Key
R Ggplot2: Labelling a Horizontal Line on the Y Axis with a Numeric Value
Function to Split a Matrix into Sub-Matrices in R
Set Ggplot Plots to Have Same X-Axis Width and Same Space Between Dot Plot Rows
Using Two Scale Colour Gradients on One Ggplot
R: Reshaping Multiple Columns from Long to Wide
Best Way to Transpose Data.Table
Reading Text File with Multiple Space as Delimiter in R
Stepwise Regression Using P-Values to Drop Variables with Nonsignificant P-Values
How to Maintain Size of Ggplot with Long Labels
Ggplot2:Plot Mean with Geom_Bar
Sparse Matrix to a Data Frame in R
Rstudio Shiny Error: There Is No Package Called "Shinydashboard"
Subset Xts Object by Time of Day
Sort a String of Comma-Separated Items Alphabetically
Is There Anything Wrong with Using T & F Instead of True & False