Does the function return an extra copy from the function?
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 when the function returns, or the assignment will be by reference.
This R stream , deep and shallow copies, pass by link , give some hints about it, but it wasn't that clear.
Another similar example might be:
f <- function(n)
{
v <- c(1:n)
v <- sample(v,n)
return(v)
}
myV <- f(10000)
+3
source to share
1 answer
You can see how return()
internally it is implemented by looking at src / main / eval.c in source R. This is the function do_return()
that also calls eval()
. Only SEXPs are transmitted and these are pointers .
So the answer is: no additional copy of the return value is created. It has been significantly optimized.
+2
source to share