Skip to content

save objects of own function to workspace

8 messages · David Winsemius, Jeff Newmiller, Rolf Turner +1 more

#
On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:

            
Why take two steps to change that column name?
You just overwrote an object that you created earklier but have not  
done anything with in the meantime.
Then just assign it:

mz_result <- s_elternmz()

As you have noticed, assignment inside a function is not a durable  
event. The function returns the value of either its first encounter  
with return() or the value
of hte last assignment (but not the name to which that object was  
assigned.) Welcome to R's version of functional programming.
David Winsemius, MD
West Hartford, CT
#
It is possible to do what you ask using <<- instead of <-, but from the perspective of using the function it will be much clearer if you add mz as an argument to the function along with Var, and return mz_int at the end. (The easiest way to return a variable is to type it alone on the last line if your function.) Then you can assign the return value to some variable of your choosing (including possibly one named mz_int in the calling environment) where you call the function.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Marion Wenty <marion.wenty at gmail.com> wrote:

            
#
On Feb 14, 2012, at 10:59 AM, Marion Wenty wrote:

            
The above line seemed to be the first change to make <Var>_m
Then this line changed <Var>_m to become <Var>_v. Why not change it to  
<Var>_v in the first place?
David Winsemius, MD
West Hartford, CT
#
On 16/02/12 03:59, Marion Wenty wrote:
<SNIP>
<SNIP>

This practice is to be *STRONGLY* discouraged.  If you persist,
you will live to regret it.

Write functions that return (arbitrarily complicated) objects and
assign the output of functions to objects.  DO NOT write functions
that mess around with your global environment.  Unless you *really*
know what you're doing --- and it's pretty clear that at this stage of
your development, you don't.

     cheers,

         Rolf Turner