[R-SIG-Finance] Passing variables...

Enrico Schumann es at enricoschumann.net
Mon May 20 15:38:07 CEST 2013


On Mon, 20 May 2013, Dominykas Grigonis <dominykasgrigonis at gmail.com> writes:

> Hello, I am writing a scenario analysis for option strategies. I am
> trying to build a framework, such that new pricing functions can be
> added as well as volatility surface, interest rate term structure,
> etc… But for now I am sticking to ear vanilla case. Anyways, the
> problem is not actually related to finance, but I could not find any
> answer so far.
>
> fun1 <- function(a,b){fun2(a)}
> fun2 <- function(c){fun3(c)}
> fun3 <- function(d){d+b}
>
>
> fun1(2,2) #Error in fun3(c) : object 'b' not found
>
> I have always thought that if functions can not find a variable it
> gradually goes back up through each single environment looking for
> that variable. However, now it is apparent, that it only looks for it
> in .GlobalEnv.

If a variable is not found during function evaluation, R will indeed
move through the environments that enclose the function's environment.
But the function's environment is the one in which the function was
*created*, not where it was *called*.

Example:

  fun1 <- function()
      a + 1

  fun2 <- function(f){
      a <- 5
      f()
  }
  fun2(fun1)  

  ## Error in f() (from #1) : object 'a' not found

This is an error because 'a' was not defined in environment of 'fun1'.
But the following would work, even though 'a' does not exist in the
global environment.

  makefun <- function() {
      a <- 5
      function()
          a + 1
  }
  fun3 <- makefun()
  fun2(fun3)


[...]

> anyways, you got the idea of my issue. I do not want to pass all of
> these via arguments through 2 functions. The main reason is that it
> becomes messy and complicates the code significantly. I was thinking

My suggestion would still be: pass everything.  If you don't like to
have too many arguments, collect all information in one list, 'Data',
say.  Then you only have to pass one object.  In my experience, the
increased 'complication' of adding more arguments to your functions is
nothing compared with the trouble you will have when you need to debug
your code.

>
> Do you have any ideas how to overcome this issue?
> I could share the code, but there is quite a lot of it. I hope you
> understand my problem and I will appreciate any help. Thank you in
> advance.

You could of course assign all variables in the global environment.  But
if you really want to use environments, I would suggest to make this
dependence explicit:

  Data <- new.env()
  Data$a <- 5
  environment(fun1) <- Data
  ls()  

  ## [1] "Data" "fun1" "fun2"

  ls(environment(fun1))
  
  ## [1] "a"

  fun2(fun1)

  ## [1] 6

It is easier then to keep track of what you add to or remove from 'Data'.

Regards,
Enrico

>
> Kind regards,--  
> Dominykas Grigonis
>
>
> 	[[alternative HTML version deleted]]

Please send plain-text messages to this mailing list.

>

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-SIG-Finance mailing list