R Modify and Rebuild Package

r modify and rebuild package

Linux environment

Starting with downloading the package source from CRAN.

  • This is the landing page: https://cran.r-project.org/web/packages/SemiMarkov/index.html
  • This is the package source: https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz

Download and extract the source:

wget https://cran.r-project.org/src/contrib/SemiMarkov_1.4.2.tar.gz
tar -xvzf SemiMarkov_1.4.2.tar.gz

This should result in a directory named SemiMarkov. Open up the source (cd SemiMarkov), and modify as necessary.

Next, build the changes:

cd ..
R CMD build SemiMarkov/

This will result in a new archive file named SemiMarkov_1.4.2.tar.gz.

Lastly, install your modified archive:

R CMD INSTALL SemiMarkov_1.4.2.tar.gz

Windows environment

I'm less familiar with the Windows platform. *nix tooling is available in Cygwin, but it's painful. Instead, as Josh O'Brien points out, you should follow the Windows-specific instructions in the R Installation and Administration manual.

R, after modifying a function in a package, modifications are ignored

I just come to solve the same issue. What you have to do is just restart your R session. It seems that R caches the function and it does not matter if you detach and remove (remove.packages) the package, and then install (install.packages) and load (require) it: You need to close your session, and then with your package built with the changes:

install.packages("path_to_package.tar.gz",repos=NULL,type="source")
require("package")

This worked for me. Hope it helps.

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.

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.

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

Can I edit the NAMESPACE file without rebuilding the package?

As Ben Bolker suggested, the answer (Bettridge's Law wins again!) is "no." Despite the existence of an editable text file in a package's tarball, the export list is read from one of the binaries ('foo.rdb' or 'foo.rdx' is my guess) and cannot be changed without rebuilding the package.

How to edit and debug R library sources

Look up the trace and browser functions, they are the basic tools in R for debugging. Say you want to edit the source of function foo, then saying

trace("foo",edit=TRUE)

will open up the source of foo in the editor for you to change. However, this is for interactive debugging; the source files in the packages are not changed. So once you have found the bug, you need to change the package source files for the change to be permanent.

How to edit and debug R library sources

Look up the trace and browser functions, they are the basic tools in R for debugging. Say you want to edit the source of function foo, then saying

trace("foo",edit=TRUE)

will open up the source of foo in the editor for you to change. However, this is for interactive debugging; the source files in the packages are not changed. So once you have found the bug, you need to change the package source files for the change to be permanent.



Related Topics



Leave a reply



Submit