[R] Accessing objects manipulated in a function
fisher at plessthan.com
Sat May 14 17:46:39 CEST 2016
OS X and Windows
I distribute some code to co-workers and I am trying to simplify their task. The issue is as follows:
1. The code automates an extensive set of processes. Many of the steps are standardized. However, some of the steps may require that users write snippets of code, stored in R scripts.
2. If users write their own code, it might appear in files named UserCode1.R, UserCode2.R, etc.. The master code checks for the existence of this code, then executes
This can occur at many different points in the master code (each time sourcing a different file).
In addition to the command above, there are a variety of other commands testing whether the file exists and whether it contains certain commands that I don’t allow the user to execute.
In order to simplify the code, these commands are embedded in a function (which I will call MODIFYCODE for the moment).
3. Assume that an object within the master code is named TEMP. The user might add a column to TEMP. Since this occurs within a function, there are two ways to get this modification back to the original environment:
a. within the function: TEMP <<- TEMP
b. use the return value from the function:
TEMP <- MODIFYCODE()
4. There are disadvantages to each of these:
a. The user needs to know that the “<<-“ command must be invoked. If they don’t do so, the changes within the function are not available in the master environment
b. I don’t know what code will be written by the user, i.e., they might manipulate TEMP or they might create a new object or something else. So, I don’t know a priori what to return.
So, my question is: is there some way to manipulate environments such that the changes within the function are AUTOMATICALLY transferred to the environment outside the function?
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone / Fax: 1-866-PLessThan (1-866-753-7784)
[[alternative HTML version deleted]]
More information about the R-help