4

In the function below:

DownloadRawData <- function(fileurl, filename)
{
    download.file(fileurl, destfile=filename)
    dataset = read.csv(filename)
    return(dataset)
}
myDataSet <- downloadRawData(myurl, myname)

Are we going to allocate 2 copies of the dataset in memory at the function return, or the assignment will be by reference.

This thread R, deep vs. shallow copies, pass by reference give some hints about it, but it was not that clear to me.

Another similar example would be:

f <- function(n)
{
    v <- c(1:n)
    v <- sample(v,n)
    return(v)
}
myV <- f(10000)
Community
  • 1
  • 1
Theo
  • 1,385
  • 2
  • 10
  • 19

1 Answers1

3

You can see how return() is internally implemented by taking a look at src/main/eval.c in the R source. It is function do_return(), which also calls eval(). Only SEXPs are passed around, and these are pointers.

So the answer is, no extra copy of the returned value is being created. It is essentially optimized away.

Theodore Lytras
  • 3,955
  • 1
  • 18
  • 25