Re: [R] A question about R environment

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Tue 09 Jan 2007 - 09:33:13 GMT

On Tue, 9 Jan 2007, Tong Wang wrote:

> Hi,
> Thanks a lot for your response, but it is strange that when I do attach() , I got the follow error:
> Error in attach(e) : attach only works for lists and data frames
>
> Any suggestions on this?

Update your R, as the posting guide asked you to in this circumstance. This was new in R 2.4.0.

You don't need the baggage of the R.utils package here: the base function sys.source loads into an environment. So the definitive way to do this is

env <- attach(NULL, name = "myenv")
sys.source("some file", env)

and that has worked since well before 2.0.0.

>
>
> tong
>
> ----- Original Message -----
> From: Henrik Bengtsson <hb@stat.berkeley.edu>
> Date: Monday, January 8, 2007 10:40 pm
> Subject: Re: [R] A question about R environment
> To: Gabor Grothendieck <ggrothendieck@gmail.com>
> Cc: Tong Wang <wangtong@usc.edu>, R help <r-help@stat.math.ethz.ch>
>
>> sourceTo() in R.utils will allow you to source() a file into an
>> environment.
>> /Henrik
>>
>> On 1/9/07, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
>>> Try this:
>>>
>>>> e <- new.env()
>>>> e$f <- function(x)x
>>>> attach(e)
>>>> search()
>>> [1] ".GlobalEnv" "e" "package:stats"
>>> [4] "package:graphics" "package:grDevices" "package:utils"
>>> [7] "package:datasets" "package:methods" "Autoloads"
>>> [10] "package:base"
>>>> f
>>> function(x)x
>>>
>>> On 1/8/07, Tong Wang <wangtong@usc.edu> wrote:
>>>> Hi all,
>>>>
>>>> I created environment "mytoolbox" by : mytoolbox <-
>> new.env(parent=baseenv())> > Is there anyway I put it in the search
>> path ?
>>>>
>>>> If you need some background :
>>>> In a project, I often write some small functions, and load
>> them into my workspace directly, so when I list the objects
>>>> with ls(), it looks pretty messy. So I am wondering if it is
>> possible to creat an environment, and put these tools into
>>>> this environment. For example, I have functions fun1(),
>> fun2() ...... and creat an environment mytoolbox which
>>>> contains all these functions. And it should be somewhere in
>> the search path: ".GlobalEnv" "mytoolbox"
>> "package:methods" ........
>>>>
>>>> ______________________________________________
>>>> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Wed Jan 10 03:36:46 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 09 Jan 2007 - 17:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.