Skip to content

Help with saving user defined functions

6 messages · George Trojan - NOAA Federal, Jeff Newmiller, Bert Gunter

#
I can't figure out how to save functions to RDS file. Here is an example
what I am trying to achieve:
[1] 0.59
Save workspace image? [y/n/c]: n
[gtrojan at asok petproject]$ R
Empirical CDF
Call: ecdf(t)
x[1:100] = -2.8881, -2.2054, -2.0026,  ..., 2.0367, 2.0414

This works. However when instead of saving cdf() I try to save function
after restoring object from file I get an error:
Error in qnorm(cdf(x) * 0.99) : could not find function "cdf"

I tried to define and call cdf within the definition of trans, without
success:
Save workspace image? [y/n/c]: n
function(x) { cdf <- ecdf(tmp); cdf(0); qnorm(cdf(x)) * 0.99 }
Error in sort(x) : object 'tmp' not found

So, here the call cdf(0) did not force evaluation of my random sample. What
am I missing?

George
#
It worked fine for me:
[1] 1.042457
[1] 0.1117773


Of course, if I remove cdf() from the global environment, it will fail:
Error in qnorm(cdf(x) * 0.99) : could not find function "cdf"

So it looks like you're clearing you global workspace in between
saving and loading?

You may need to read up on function closures/lexical scoping : A
user-defined function in R includes not only code but also a pointer
to the environment in which it was defined, in your case, the global
environment from which you apparently removed cdf(). Note that
functions are not evauated until called, so free variables in the
functions that do not or will not exist in the function's lexical
scope when called will not trigger any errors until the function *is*
called.

Same comments for your second version -- if tmp is removed the
function will fail.



Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Feb 12, 2017 at 2:11 PM, George Trojan - NOAA Federal
<george.trojan at noaa.gov> wrote:
#
So doesn't the fact that a function contains a reference to an environment suggest that this whole exercise is a really bad idea?
#
Jeff:

Oh yes!-- and I meant to say so and forgot, so I'm glad you did. Not
only might the free variable in the function not be there; worse yet,
it might be there but something else. So it seems like a disaster
waiting to happen. The solution, I would presume, is to have no free
variables (make them arguments). Or save and read the function *and*
its environment.  Namespaces in packages I think would also take care
of this, right?

Note: If my understanding on any of this is incorrect, I would greatly
appreciate someone settting me straight. In particular, as Jeff noted,
my understanding is that saving a function (closure)  with a free
variable in the function depends on the function finding its enclosing
environment when it is read back into R via readRDS() .  Correct?  The
man page is silent on this point.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Feb 12, 2017 at 4:26 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
#
I want to split my computation into parts. The first script processes the
data, the second does the graphics. I want to save  results of
time-consuming calculations. My example tried to simulate this by terminate
the session without saving it, so the environment was lost on purpose. What
confuses me that ecdf can be saved and restored, but not my own derived
function.
Of course I can save parameters and redefine the function in the second
script.

Reading Chapter 8 of Advanced R, hopefully the book will clear my mind.

On Mon, Feb 13, 2017 at 12:05 AM, Bert Gunter <bgunter.4567 at gmail.com>
wrote:

  
  
#
ecdf() is part of the stats package, which is (typically)
automatically attached on startup.

I have no idea what you mean by "splitting" and "saving." This is
basically how all of R works -- e.g. see the value of lm() and the
(S3) plot method, plot.lm, for "lm"  objects. This has nothing to do
with free variables and lexical scoping. Perhaps you need to review
how functions and S3 methods work?

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Feb 12, 2017 at 5:31 PM, George Trojan - NOAA Federal
<george.trojan at noaa.gov> wrote: