0

I am making changes to a global dataframe within my user defined function. The dataframe is created outside of the function.

However, my changes to the dataframe are not visible outside of the function. Only if I use a return option, I end up with the dataframe.

Is there a way to change this?

M--
  • 25,431
  • 8
  • 61
  • 93
Niccola Tartaglia
  • 1,537
  • 2
  • 26
  • 40

1 Answers1

2

Whether you should do "call by reference" functionality in R is one question (addressed in the comments - generally the answer is no).

However, you asked whether you can do it. The answer is yes, you can modify your global dataframe in the local scope of your function. Here is how you do it: 1) Use eval.parent() (set the evaluation scope to the calling scope, which, presumably, is the global scope) and 2) substitute() (to replace the variable reference instead of destroying one and creating a new one).

Here's an example:

> attach(mtcars)
> my_cars <- mtcars[mpg,] #not sorted
> pointless_sort <- function() {
+     eval.parent(substitute(my_cars<-mtcars[order(mpg),]))
+ }
> pointless_sort()
> #here the global my_cars is ordered/sorted by mpg

Important points: 1) You can do it; 2) Good programming generally means not doing it (but we've all been lazy, wanted a convenient way to split up code). Now you have the power.

"With Great Power Comes Great Responsibility."

DarkerIvy
  • 1,477
  • 14
  • 26
  • Fantastic, thank you for demonstrating this. I will try to avoid the approach but keep this in the back of my head (just in case). – Niccola Tartaglia Dec 13 '17 at 22:09
  • In honesty, it depends on the situation. Purists will say to never do it. If it's not production or maintained code, just something you do once, what's the difference? – DarkerIvy Dec 14 '17 at 14:14
  • Agreed. For me as a beginner it is probably helpful though to adhere to 'robust' programming principles early on. – Niccola Tartaglia Dec 15 '17 at 17:09