[R] access object inside a function

Patrick Burns pburns at pburns.seanet.com
Sun Aug 3 18:05:56 CEST 2008


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