Re: [Rd] Best practices for writing R functions

From: Gabriel Becker <>
Date: Fri, 22 Jul 2011 08:38:26 -0700

On Fri, Jul 22, 2011 at 8:14 AM, Spencer Graves <
> wrote:

> From my personal experience and following this list some for a few
> years, the best practice is initially to ignore the compute time question,
> because the cost of your time getting it to do what you want is far greater,
> at least initially. Don't worry about compute time until it becomes an
> issue. When it does, the standard advice I've seen on this list is to
> experiment with different ways of writing the same thing in R, guided by
> "profiling R code", as described in the "Writing R Extensions" manual.
> (Googling for "profiling R code" identified examples.)
> Hope this helps.
> Spencer Graves
> On 7/22/2011 6: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.
AFAIK R does not automatically copy function arguments. R actually tries very hard to avoid copying while maintaining "pass by value" functionality. Consider the following functions and their output:

nomod = function(dat)

mod = function(dat, i)
    dat[5] = 5

> vec = rep(0, times = 10)
> tracemem(vec)

[1] "<0x8c85978>"
> nomod(vec)

[1] TRUE
> mod(vec)

tracemem[0x8c85978 -> 0x8c85c70]: mod
[1] TRUE So in the nomod function, the argument never actually gets copied (that is what tracemem tracks). R only copies data when you modify an object, not when you simply pass it to a function


>> Is the above assessment fair? 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:**
>> Best-practices-for-writing-R-**functions-tp3686674p3686674.**html<>
>> Sent from the R devel mailing list archive at
>> ______________________________**________________
>> mailing list
> ______________________________**________________
> mailing list

Gabriel Becker
Graduate Student
Statistics Department
University of California, Davis

	[[alternative HTML version deleted]]

______________________________________________ mailing list
Received on Fri 22 Jul 2011 - 16:03:57 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:50:13 GMT.

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

list of date sections of archive