Re: [Rd] Best practices for writing R functions

From: Simon Urbanek <>
Date: Fri, 22 Jul 2011 11:36:27 -0400

On Jul 22, 2011, at 9:26 AM, Alireza Mahani wrote:

> I am developing an R package for internal use, and eventually for public
> release. My understanding is that there is no easy way to avoid copying
> function arguments in R (i.e. we don't have the concept of pointers in R),
> which makes me wary of freely creating chains of function calls since each
> function call implies data copy overhead.
> Is the above assessment fair?

No. Although R maintains the illusion of pass-by-value, it does to considerable length to prevent copying if not needed:

> r=rnorm(1e6)
> tracemem(r)

[1] "<0x103fa2000>"
> f = function(x) x
> invisible(f(f(f(f(f(f(r)))))))

As you see not a single copy is created even though there are 6 function calls.

As Spencer said, you should not worry unless you see a sign of a problem.


> Are there any good write-ups on best practices
> for writing efficient R libraries that take into consideration the
> above-mentioned limitations, and any others that might exist?
> Thank you,
> Alireza
> --
> View this message in context:
> Sent from the R devel mailing list archive at
> ______________________________________________
> mailing list
> mailing list Received on Fri 22 Jul 2011 - 15:38:06 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Jul 2011 - 19:10:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive