When developing an R package, do I have to recompi

2019-03-26 16:05发布

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)

4条回答
爷的心禁止访问
2楼-- · 2019-03-26 16:43

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.

查看更多
仙女界的扛把子
3楼-- · 2019-03-26 16:53

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-03-26 16:57

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")
    
查看更多
做个烂人
5楼-- · 2019-03-26 17:03

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

查看更多
登录 后发表回答