[R] function environment

Martin Maechler maechler at stat.math.ethz.ch
Sat Jun 3 12:27:49 CEST 2006


>>>>> "Roger" == Roger D Peng <rdpeng at gmail.com>
>>>>>     on Fri, 02 Jun 2006 13:03:12 -0400 writes:

    Roger> Try

    Roger>   save(A, B, file = "myfun.r")
    Roger>   attach("myfun.r")

    Roger> Your functions will be on the search list.

yes.   But do yourself (and your readers/users/..) a favor by
using a different file extension: ".r" is used in place of ".R"
(mainly by people who work on (non-)operating systems that do not
 properly differentiate between lower and upper case) for  R
source files.
Files resulting from save() typically get an extension ".rda"
(or also ".Rdata").

Martin
    Roger> -roger

    Roger> Matthias Braeunig wrote:
    >> -----BEGIN PGP SIGNED MESSAGE-----
    >> Hash: SHA1
    >> 
    >> Hi,
    >> 
    >> how can I automatically access the functions that I loaded into a
    >> separate environment?
    >> 
    >>> save(A,B,file="myfun.r")
    >>> load("myfun.r",envir=(ENV<-new.env()))
    >>> ls(ENV)
    >> [1] "A" "B"
    >> 
    >> ?"[" turned up that I can access the functions via
    >> 
    >>> ENV$A
    >> function ()
    >> {
    >> }
    >>> ENV$A()
    >> NULL
    >> 
    >> Now, how can they be included in the search() path??
    >> attach() as for data.frames does not work...
    >> 
    >> Furthermore, if I change a functions environment to ENV, why is it not
    >> listed with ls(ENV)?? Instead it still lives in .GlobalEnv
    >> 
    >>> C<-function(){}
    >>> environment(C)<-ENV
    >>> ls(ENV)
    >> [1] "A" "B"
    >>> C
    >> function(){}
    >> <environment: 0x9cbbb58>
    >>> ENV
    >> <environment: 0x9cbbb58>
    >> 
    >> Thanks folks!
    >> I enjoy reading and learning from r-help list!
    >> 
    >> M
    >> -----BEGIN PGP SIGNATURE-----
    >> Version: GnuPG v1.4.2.2 (GNU/Linux)
    >> 
    >> iD8DBQFEgFw2XjamRUP82DkRAooRAJ9sxwERwfXF3l7pssZ081sMC1+nigCgqAPM
    >> OkA1tNJg6MN3l0PQFrwBlIE=
    >> =tGFq
    >> -----END PGP SIGNATURE-----
    >> 
    >> ______________________________________________
    >> R-help at 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
    >> 

    Roger> -- 
    Roger> Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/

    Roger> ______________________________________________
    Roger> R-help at stat.math.ethz.ch mailing list
    Roger> https://stat.ethz.ch/mailman/listinfo/r-help
    Roger> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html



More information about the R-help mailing list