Re: [R] A question about R environment

2007-01-10 Thread François Pinard
[Philippe Grosjean]

Please, don't reinvent the wheel: putting functions in a dedicated 
environment is one of the things done by R packages (together with a 
good documentation of the function, and making them easily installable 
on any R implementation).

[...] this is probably the time for you to read the Writing 
R extensions manual, and to start implementing your own R package!

Hi, Philippe, and gang!

I read this manual long ago and used it to create packages already.  You 
really got the impression I did not read it? :-)

You know, there are small wheels, and huge wheels.  I do not see why 
I would use complex devices for tiny problems, merely because those 
complex devices exist.  R packages undoubtedly have their virtues, of 
course.  But just like many statistical tests, they do not always apply.

Why go at length organising package directories populated with many 
files, resorting to initialisation scripts, using package validators,
creating documentation files and processing them, go through the cycle 
of creating a package and installing it, all that merely for a few small 
quickies that fit very well in the ubiquitous .Rprofile file?  Why worry 
about installation on any R implementation, for little things only meant 
for myself, and too simple to warrant publication anyway?

Keep happy, all.

-- 
François Pinard   http://pinard.progiciels-bpi.ca

__
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.


Re: [R] A question about R environment

2007-01-09 Thread Tong Wang
Francois (sorry can't type the right letter) , thanks a million for the 
detailed response,  you have really cool functions there !

Philippe :  I will definitely follow your advice later,  but I got some time 
pressure from the current project, so have to go with the easy sol. now ,   
thanks a lot. 

Happy new year every one!

cheers

tong

- Original Message -
From: Philippe Grosjean [EMAIL PROTECTED]
Date: Monday, January 8, 2007 10:09 pm
Subject: Re: [R] A question about R environment
To: François Pinard [EMAIL PROTECTED], Tong Wang [EMAIL PROTECTED], R help 
r-help@stat.math.ethz.ch

 Please, don't reinvent the wheel: putting functions in a dedicated 
 environment is one of the things done by R packages (together with 
 a 
 good documentation of the function, and making them easily 
 installable 
 on any R implementation). So, this is probably the time for you to 
 read 
 the Writing R extensions manual, and to start implementing your 
 own R 
 package!
 Best,
 
 Philippe Grosjean
 
 François Pinard wrote:
  [Tong Wang]
  
  I created environment mytoolbox by : mytoolbox - 
  new.env(parent=baseenv()).  Is there anyway I put it in the 
 search 
  path?  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.
  
  Here is a trick, shown as a fairly simplified copy of my 
 ~/.Rprofile.  
  It allows for a few simple functions always available, yet 
 without 
  having to create a package, and leaving ls() and any later .RData 
 file 
  unencumbered.
  
  The idea is to use local() to prevent any unwanted clutter to 
 leak out 
  (my real ~/.Rprofile holds more than shown below and use 
 temporary 
  variables), to initialise a list meant to hold a bunch of 
 functions or 
  other R things, and to save that list on the search path.
  
  This example also demonstrate a few useful functions for when I 
 read the 
  R mailing list.  I often need to transfer part of emails containing
  code excerpts within the window where R executes, while removing 
  quotation marks, white lines and other noise.  I merely highlight-
 select 
  part of the message with the mouse, and then, within R, do things 
 like: 
 xs()   source the highlighted region
 xd()   read in a data.frame
 xm()   read in a matrix
 xe()   evaluate and print an expression
 xv()   read a list of values as a vector
  
  The list above in decreasing order of usefulness (for me).  
 Except for 
  xs(), which has no automatic printout, you may either let the 
 others 
  print what they got, or assign their value to some variable.  
 Arguments 
  are also possible, for example like this:
  
 xd(T)  read in a data.frame when the first line holds column 
 names 
  
  
  if (interactive()) {
  local({
  
  fp.etc - list()
  
  fp.etc$xsel.vector - function (...) {
  connexion - textConnection(xselection())
  on.exit(close(connexion))
  scan(connexion, ...)
  }
  fp.etc$xsel.dataframe - function (...) {
  connexion - textConnection(xselection())
  on.exit(close(connexion))
  read.table(connexion, ...)
  }
  fp.etc$xsel.matrix - function (...) {
  connexion - textConnection(xselection())
  on.exit(close(connexion))
  data.matrix(read.table(connexion, ...))
  }
  fp.etc$xsel.eval - function (...) {
  connexion - textConnection(xselection())
  on.exit(close(connexion))
  eval(parse(connexion, ...))
  }
  fp.etc$xsel.source - function (...) {
  connexion - textConnection(xselection())
  on.exit(close(connexion))
  source(connexion, ...)
  }
  
  fp.etc$xselection - function ()
  {
  lignes - suppressWarnings(readLines('clipboard'))
  lignes - lignes[lignes != '']
  stopifnot(length(lignes) != 0)
  marge - substr(lignes, 1, 1)
  while (all(marge %in% c('', '+', ':', '|'))
|| all(marge == ' ')) {
  lignes - substring(lignes, 2)
  marge - substr(lignes, 1, 1)
  }
  lignes
  }
  
  fp.etc$xv - fp.etc$xsel.vector
  fp.etc$xd - fp.etc$xsel.dataframe
  fp.etc$xm - fp.etc$xsel.matrix
  fp.etc$xe - fp.etc$xsel.eval
  fp.etc$xs - fp.etc$xsel.source
  
  attach(fp.etc, warn=FALSE)
  
  })
  }
  
  # vim: ft=r

Re: [R] A question about R environment

2007-01-09 Thread Tong Wang
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?


tong

- Original Message -
From: Henrik Bengtsson [EMAIL PROTECTED]
Date: Monday, January 8, 2007 10:40 pm
Subject: Re: [R] A question about R environment
To: Gabor Grothendieck [EMAIL PROTECTED]
Cc: Tong Wang [EMAIL PROTECTED], 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 [EMAIL PROTECTED] wrote:
  Try this:
 
   e - new.env()
   e$f - function(x)x
   attach(e)
   search()
   [1] .GlobalEnve 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 [EMAIL PROTECTED] 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 functionsfun1(), 
 fun2() ..and creat an environment  mytoolbox  which
contains all these functions.  And it should be somewhere in 
 the search path:   .GlobalEnvmytoolbox  
  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.


Re: [R] A question about R environment

2007-01-09 Thread Prof Brian Ripley
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 [EMAIL PROTECTED]
 Date: Monday, January 8, 2007 10:40 pm
 Subject: Re: [R] A question about R environment
 To: Gabor Grothendieck [EMAIL PROTECTED]
 Cc: Tong Wang [EMAIL PROTECTED], 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 [EMAIL PROTECTED] wrote:
 Try this:

 e - new.env()
 e$f - function(x)x
 attach(e)
 search()
  [1] .GlobalEnve 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 [EMAIL PROTECTED] 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 functionsfun1(),
 fun2() ..and creat an environment  mytoolbox  which
  contains all these functions.  And it should be somewhere in
 the search path:   .GlobalEnvmytoolbox
  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,  [EMAIL PROTECTED]
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, UKFax:  +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.


Re: [R] A question about R environment

2007-01-08 Thread Gabor Grothendieck
Try this:

 e - new.env()
 e$f - function(x)x
 attach(e)
 search()
 [1] .GlobalEnve 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 [EMAIL PROTECTED] 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 functionsfun1(), fun2() ..   
  and creat an environment  mytoolbox  which
  contains all these functions.  And it should be somewhere in the search 
 path:   .GlobalEnvmytoolbox
 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.


Re: [R] A question about R environment

2007-01-08 Thread François Pinard
[Tong Wang]

I created environment mytoolbox by : mytoolbox - 
new.env(parent=baseenv()).  Is there anyway I put it in the search 
path?  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.

Here is a trick, shown as a fairly simplified copy of my ~/.Rprofile.  
It allows for a few simple functions always available, yet without 
having to create a package, and leaving ls() and any later .RData file 
unencumbered.

The idea is to use local() to prevent any unwanted clutter to leak out 
(my real ~/.Rprofile holds more than shown below and use temporary 
variables), to initialise a list meant to hold a bunch of functions or 
other R things, and to save that list on the search path.

This example also demonstrate a few useful functions for when I read the 
R mailing list.  I often need to transfer part of emails containing
code excerpts within the window where R executes, while removing 
quotation marks, white lines and other noise.  I merely highlight-select 
part of the message with the mouse, and then, within R, do things like:

   xs()   source the highlighted region
   xd()   read in a data.frame
   xm()   read in a matrix
   xe()   evaluate and print an expression
   xv()   read a list of values as a vector

The list above in decreasing order of usefulness (for me).  Except for 
xs(), which has no automatic printout, you may either let the others 
print what they got, or assign their value to some variable.  Arguments 
are also possible, for example like this:

   xd(T)  read in a data.frame when the first line holds column names



if (interactive()) {
local({

fp.etc - list()

fp.etc$xsel.vector - function (...) {
connexion - textConnection(xselection())
on.exit(close(connexion))
scan(connexion, ...)
}
fp.etc$xsel.dataframe - function (...) {
connexion - textConnection(xselection())
on.exit(close(connexion))
read.table(connexion, ...)
}
fp.etc$xsel.matrix - function (...) {
connexion - textConnection(xselection())
on.exit(close(connexion))
data.matrix(read.table(connexion, ...))
}
fp.etc$xsel.eval - function (...) {
connexion - textConnection(xselection())
on.exit(close(connexion))
eval(parse(connexion, ...))
}
fp.etc$xsel.source - function (...) {
connexion - textConnection(xselection())
on.exit(close(connexion))
source(connexion, ...)
}

fp.etc$xselection - function ()
{
lignes - suppressWarnings(readLines('clipboard'))
lignes - lignes[lignes != '']
stopifnot(length(lignes) != 0)
marge - substr(lignes, 1, 1)
while (all(marge %in% c('', '+', ':', '|'))
  || all(marge == ' ')) {
lignes - substring(lignes, 2)
marge - substr(lignes, 1, 1)
}
lignes
}

fp.etc$xv - fp.etc$xsel.vector
fp.etc$xd - fp.etc$xsel.dataframe
fp.etc$xm - fp.etc$xsel.matrix
fp.etc$xe - fp.etc$xsel.eval
fp.etc$xs - fp.etc$xsel.source

attach(fp.etc, warn=FALSE)

})
}

# vim: ft=r


-- 
François Pinard   http://pinard.progiciels-bpi.ca

__
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.


Re: [R] A question about R environment

2007-01-08 Thread Philippe Grosjean
Please, don't reinvent the wheel: putting functions in a dedicated 
environment is one of the things done by R packages (together with a 
good documentation of the function, and making them easily installable 
on any R implementation). So, this is probably the time for you to read 
the Writing R extensions manual, and to start implementing your own R 
package!
Best,

Philippe Grosjean

François Pinard wrote:
 [Tong Wang]
 
 I created environment mytoolbox by : mytoolbox - 
 new.env(parent=baseenv()).  Is there anyway I put it in the search 
 path?  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.
 
 Here is a trick, shown as a fairly simplified copy of my ~/.Rprofile.  
 It allows for a few simple functions always available, yet without 
 having to create a package, and leaving ls() and any later .RData file 
 unencumbered.
 
 The idea is to use local() to prevent any unwanted clutter to leak out 
 (my real ~/.Rprofile holds more than shown below and use temporary 
 variables), to initialise a list meant to hold a bunch of functions or 
 other R things, and to save that list on the search path.
 
 This example also demonstrate a few useful functions for when I read the 
 R mailing list.  I often need to transfer part of emails containing
 code excerpts within the window where R executes, while removing 
 quotation marks, white lines and other noise.  I merely highlight-select 
 part of the message with the mouse, and then, within R, do things like:
 
xs()   source the highlighted region
xd()   read in a data.frame
xm()   read in a matrix
xe()   evaluate and print an expression
xv()   read a list of values as a vector
 
 The list above in decreasing order of usefulness (for me).  Except for 
 xs(), which has no automatic printout, you may either let the others 
 print what they got, or assign their value to some variable.  Arguments 
 are also possible, for example like this:
 
xd(T)  read in a data.frame when the first line holds column names
 
 
 
 if (interactive()) {
 local({
 
 fp.etc - list()
 
 fp.etc$xsel.vector - function (...) {
 connexion - textConnection(xselection())
 on.exit(close(connexion))
 scan(connexion, ...)
 }
 fp.etc$xsel.dataframe - function (...) {
 connexion - textConnection(xselection())
 on.exit(close(connexion))
 read.table(connexion, ...)
 }
 fp.etc$xsel.matrix - function (...) {
 connexion - textConnection(xselection())
 on.exit(close(connexion))
 data.matrix(read.table(connexion, ...))
 }
 fp.etc$xsel.eval - function (...) {
 connexion - textConnection(xselection())
 on.exit(close(connexion))
 eval(parse(connexion, ...))
 }
 fp.etc$xsel.source - function (...) {
 connexion - textConnection(xselection())
 on.exit(close(connexion))
 source(connexion, ...)
 }
 
 fp.etc$xselection - function ()
 {
 lignes - suppressWarnings(readLines('clipboard'))
 lignes - lignes[lignes != '']
 stopifnot(length(lignes) != 0)
 marge - substr(lignes, 1, 1)
 while (all(marge %in% c('', '+', ':', '|'))
   || all(marge == ' ')) {
 lignes - substring(lignes, 2)
 marge - substr(lignes, 1, 1)
 }
 lignes
 }
 
 fp.etc$xv - fp.etc$xsel.vector
 fp.etc$xd - fp.etc$xsel.dataframe
 fp.etc$xm - fp.etc$xsel.matrix
 fp.etc$xe - fp.etc$xsel.eval
 fp.etc$xs - fp.etc$xsel.source
 
 attach(fp.etc, warn=FALSE)
 
 })
 }
 
 # vim: ft=r
 


__
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.


Re: [R] A question about R environment

2007-01-08 Thread Henrik Bengtsson
sourceTo() in R.utils will allow you to source() a file into an environment.

/Henrik

On 1/9/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 Try this:

  e - new.env()
  e$f - function(x)x
  attach(e)
  search()
  [1] .GlobalEnve 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 [EMAIL PROTECTED] 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 functionsfun1(), fun2() .. 
 and creat an environment  mytoolbox  which
   contains all these functions.  And it should be somewhere in the search 
  path:   .GlobalEnvmytoolbox
  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.