When developing an R package, do I have to recompile the package every time I make a change?

陌路散爱 提交于 2019-12-21 04:23:10

问题


I am developing a package in R

When I am debugging a particular function or set of functions, what is the best way to test the function?

Do I have to either use source('function.R') or R CMD build each time I want to check my changes?

(extra credit for associated emacs ess key-bindings)


回答1:


See also http://github.com/hadley/devtools/ which provides some tools to make this task easier.

for example, after making changes to source code, you build, install, and reload a package with the function install():

library(devtools)
install("package_name")

devtools also makes it easier to:

  • Reload complete package:

    load_all("pkg")
    
  • Create or update documentation using roxygen2

    document("pkg")
    
  • run all scripts in /inst/test/:

    test("pkg")
    
  • build and R CMD check:

    check("pkg")
    



回答2:


Take a look at ?insertSource, which is a new function in R 2.12.0, plus the other functions in the See Also section of that help page. Also, check out ?assignInNamespace if your package has a Namespace.

The above presumes you are talking about updating and debugging R sources, not compiled code.

I generally have used the source() route to load new versions of functions I am improving/debugging, alongside the usual R debugging tools. But I haven't got Namespaces in my packages as yet. My fingers have gotten quite used to the C-c C-l keybinding in emacs+ess for sourcing a buffer over the years.




回答3:


You might want to have a look at the 'mvbutils' package. I use it to live-edit my packages all the time; I can add, remove, and edit functions and documentation while the package is loaded, and the changes are reflected both in the loaded version, in the installed version (so they're kept in the next R session), and [when I tell it] in the "source package". I only re-build via R CMD when I want to distribute a zipped version to someone else. To test code, I use the 'debug' package, which works fine on a loaded package.

I even use 'mvbutils' to live-edit 'mvbutils', which can be a bit hairy sometimes.

The 'mvbutils' documentation could really do with a full demo of this in action, but in theory the existing doco should show you how to proceed.

Can't help you with Emacs, sorry...




回答4:


I had got the same issue and I solved it while using RStudio.

In the editor, I check the option "Source on save" for my R file that contains function. As I'm used to save my file each time I edit it (a good habit I think), the corresponding functions loaded in my R workspace is always up to date.



来源:https://stackoverflow.com/questions/4055614/when-developing-an-r-package-do-i-have-to-recompile-the-package-every-time-i-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!