开发 R 包时,每次进行更改都必须重新编译包吗?

发布于 2024-09-30 12:16:57 字数 152 浏览 0 评论 0原文

我正在用 R 开发一个包

当我调试一个特定函数或一组函数时,测试该函数的最佳方法是什么?

每次我想检查我的更改时,是否必须使用 source('function.R') 或 R CMD 构建?

(相关 emacs ess 键绑定的额外积分)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜扑 2024-10-07 12:16:57

另请参阅 http://github.com/hadley/devtools/ 它提供了一些工具来完成此任务更轻松。

例如,在更改源代码后,您可以使用 install() 函数构建、安装和重新加载包:

library(devtools)
install("package_name")

devtools 还可以更轻松地:

  • 重新加载完整的包:

    load_all("pkg")
    
  • 使用 roxygen2 创建或更新文档

    文档(“pkg”)
    
  • 运行 /inst/test/ 中的所有脚本

    <前><代码>测试(“pkg”)

  • 构建和 R CMD 检查:

    <前><代码>检查(“pkg”)

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")
    
月光色 2024-10-07 12:16:57

查看 ?insertSource,这是 R 2.12.0 中的一个新函数,以及该帮助页面“另请参阅”部分中的其他函数。另外,如果您的包有命名空间,请检查?assignInNamespace

上面假设您正在谈论更新和调试 R 源代码,而不是编译的代码。

我通常使用 source() 路径来加载我正在改进/调试的函数的新版本,以及常用的 R 调试工具。但我的包中还没有命名空间。多年来,我的手指已经非常习惯使用 emacs+ess 中的 Cc Cl 键绑定来获取缓冲区。

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.

溺孤伤于心 2024-10-07 12:16:57

您可能想查看“mvbutils”包。我一直用它来实时编辑我的包;我可以在加载包时添加、删除和编辑功能和文档,并且更改会反映在加载的版本、安装的版本中(因此它们保留在下一个 R 会话中)和 [当我告诉它]在“源码包”中。当我想将压缩版本分发给其他人时,我仅通过 R CMD 重新构建。为了测试代码,我使用“调试”包,它在加载的包上运行良好。

我什至使用“mvbutils”来实时编辑“mvbutils”,这有时可能有点毛茸茸的。

“mvbutils”文档确实可以提供完整的演示,但理论上现有的 doco 应该向您展示如何继续。

无法帮助您使用 Emacs,抱歉...

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

悸初 2024-10-07 12:16:57

我遇到了同样的问题,我在使用 RStudio 时解决了它。

在编辑器中,我为包含函数的 R 文件选中“保存时来源”选项。由于我习惯在每次编辑文件时保存文件(我认为这是一个好习惯),因此我的 R 工作区中加载的相应函数始终是最新的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文