在R中的另一个函数中定义和调用一个函数有什么好处? [英] What are the benefits of defining and calling a function inside another function in R?

查看:60
本文介绍了在R中的另一个函数中定义和调用一个函数有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法1

f1 <- function(x)
{
    # Do calculation xyz ....

    f2 <- function(y)
    {
        # Do stuff...
        return(some_object)
    }

    return(f2(x))
}

方法2

f2 <- function(y)
{
    # Do stuff...

    return(some_object)
}

f3 <- function(x)
{
    # Do calculation xyz ....
    return(f2(x))
}

假设 f1 f3 都进行相同的计算并得出相同的结果.

Assume f1 and f3 both do the same calculations and give the same result.

使用方法1调用 f1()与使用方法2调用 f3()有什么显着优势吗?

Are there any significant advantages in using approach 1, calling f1(), vs approach 2, calling f3()?

在以下情况下,某种方法更有利吗?

Is a certain approach more favourable when:

  • 正在将大数据传入和/或传出 f2 ?

速度是一个大问题.例如.在仿真中反复调用 f1 f3 .

Speed is a big issue. E.g. f1 or f3 are called repeatedly in simulations.

(方法1在软件包中似乎很常见,在另一个方法中定义)

(Approach 1 seems common in packages, defining inside another)

使用方法 f1 的一个优势是,一旦 f1 具有完成调用(并且仅在 f1 f3 中调用 f2 ).

One advantage of using the approach f1 is that f2 won't exist outside f1 once f1 has finished being called (and f2 is only called in f1 or f3).

推荐答案

f1 内定义 f2 的好处:

  • f2 仅在 f1 中可见,如果 f2 仅在 f1 中使用,则很有用软件包名称空间,这是值得商since的,因为如果在外部定义
  • f2 ,就不会导出
  • f2 可以访问 f1 中的变量,这被认为是好事还是坏事:
    • 很好,因为您不必通过函数接口传递变量,并且可以使用<<-来实现诸如备注等内容.
    • 不好,出于同样的原因...
    • f2 only visible within f1, useful if f2 is only meant for use within f1, though within package namespaces this is debatable since you just wouldn't export f2 if you defined it outside
    • f2 has access to variables within f1, which could be considered a good or a bad thing:
      • good, because you don't have to pass variables through the function interface and you can use <<- to implement stuff like memoization, etc.
      • bad, for the same reasons...

      缺点:

        每次调用 f1 时都需要重新定义
      • f2 ,这会增加一些开销(不是很多开销,但肯定在那里)
      • f2 needs to be redefined every time you call f1, which adds some overhead (not very much overhead, but definitely there)

      数据大小无关紧要,因为R不会复制数据,除非在任何一种情况下都对其进行了修改.正如缺点所指出的那样,在 f1 之外定义 f2 应该会更快一些,特别是如果您要重复多次执行本来相对较低的开销操作.这是一个示例:

      Data size should not matter since R won't copy the data unless it is being modified under either scenario. As noted in disadvantages, defining f2 outside of f1 should be a little faster, especially if you are repeating an otherwise relatively low overhead operation many times. Here is an example:

      > fun1 <- function(x) {
      +   fun2 <- function(x) x
      +   fun2(x)
      + }
      > fun2a <- function(x) x
      > fun3 <- function(x) fun2a(x)
      > 
      > library(microbenchmark)
      > microbenchmark(
      +   fun1(TRUE), fun3(TRUE)
      + )
      Unit: nanoseconds
             expr min    lq median    uq   max neval
       fun1(TRUE) 656 674.5  728.5 859.5 17394   100
       fun3(TRUE) 406 434.5  480.5 563.5  1855   100
      

      在这种情况下,我们节省了250ns(差异实际上是200ns;无论是否相信 fun1 花费的额外的 {} 花费另外50ns),我们都可以做到.数量不多,但如果内部功能更复杂或您重复多次该功能,则可以累加.

      In this case we save 250ns (edit: the difference is actually 200ns; believe it or not the extra set of {} that fun1 has costs another 50ns). Not much, but can add up if the interior function is more complex or you repeat the function many many times.

      这篇关于在R中的另一个函数中定义和调用一个函数有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆