[R] access object inside a function

Duncan Murdoch murdoch at stats.uwo.ca
Sun Aug 3 18:43:47 CEST 2008


On 03/08/2008 12:13 PM, Jonas wrote:
> Duncan, Patrick,
> 
> Sorry for not being very clear (and having non-working code in my
> example). What I'm actually trying to achieve is creating a wrapper
> around the NelsonSiegel function in the fBonds package in order to
> (among other things) create my own plot function. However, all of the
> objects i would like to access inside of my.function are not returned
> by the NelsonSiegel function. The are only local inside the
> NelsonSiegel function. Is there a way to inside my wrapper function
> access these objects?
> 
>  my.function() <- {
>     z <- NelsonSiegel(Yield, Maturity, doplot=FALSE)
>     #here i would like to access objects not returned by NelsonSiegel
> }

No, as Patrick said, once the function returns all the locals (other 
than z) disappear.  You need to get the source to the function and 
modify it.

Duncan Murdoch

> 
> 
> //Jonas
> 
> On Sun, Aug 3, 2008 at 6:05 PM, Patrick Burns <pburns at pburns.seanet.com> wrote:
>> Duncan Murdoch wrote:
>>> On 03/08/2008 11:29 AM, Jonas wrote:
>>>> hi,
>>>>
>>>> my apologies if this has been covered numerous times before and it's
>>>> only my lack of search and/or R skills that is  stopping me from
>>>> finding the solution.
>>>> i'm trying to access an object defined and created inside a function
>>>> (the object is not returned by the function), but i can't seem to get
>>>> it to work, i keep getting an "object not found" error message. i
>>>> thought the solution where to be found in changing the
>>>> frame/environment. but i can't seem to understand how do do it
>>>> correctly. what i want to do is something like:
>>>>
>>>> my.function() <- {
>>>>   function.from.package(x,y)
>>>>   plot (object.inside.package.function)
>>>> }
>>> That pseudo-code doesn't really make sense: you didn't save the result of
>>> function.from.package() anywhere.  If you change it to
>>>
>>>  my.function() <- {
>>>    z <- function.from.package(x,y)
>>>    plot (z)
>>>  }
>>>
>>> then it's perfectly standard.  So the question is:  where are you
>>> expecting R to look to find object.inside.package.function?
>> I suspect the OP doesn't care where R looks as
>> long as it finds it.  The fact that everything about
>> 'function.from.package' except 'z' has been sucked
>> down a black hole by the time 'plot' is on the scene
>> is going to make that search impossible.
>>
>> One solution is to create 'my.function.from.package'
>> and change the return value to include
>> 'object.inside.package.function'.
>>
>> Patrick Burns
>> patrick at burns-stat.com
>> +44 (0)20 8525 0696
>> http://www.burns-stat.com
>> (home of S Poetry and "A Guide for the Unwilling S User")
>>> Duncan Murdoch
>>>
>>>> i'm using R 2.7.1 on linux
>>>>
>>>> sincerely,
>>>> jonas
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>



More information about the R-help mailing list