[Rd] Example of environment
A minor comment: in help(environment) the example starts with: ##-- all three give the same: environment() environment(environment) .GlobalEnv but the comment is not true. The second returns the "". "R : Copyright 2006, The R Foundation for Statistical Computing Version 2.3.0 alpha (2006-04-02 r37626)" /Henrik __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Integration of C and Fortran
Is the enter point Fortran "orderdata_" absent of the loading table. a manual translation, and was the underscore there in the original (I think it should be, but am just checking). What do nm -g mpackage.so tell you is actually exported from the shared object (here called 'mypackage.so')? I suspect that will reveal a mismatch of names. On Mon, 3 Apr 2006, Sebastien Durand wrote: Dear all, I am running R : Copyright 2005, Version 2.2.1 (2005-12-20 r36812) ISBN 3-900051-07-0 On a mac 10.4.5 I am having trouble with my package. The trouble is the following: In my package I have integrated three files in my src folder. 1- simplematch.C a .C file 2- ordering.f a fortran file 3- Makevars : PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) I am not sure if that last file is really useful!? When I use the R CMD SHLIB command on both these source code files, the resulting compiled .so files when loads fine with the dyn.load function. At last when I use either the .C("simplematch",...) and .Fortran("ordering",...) command everything works... Now here comes trouble: When I do the R CMD INSTALL command, my package install. The .C command works but when I run the .Fortran command I get: Error in .Fortran("orderdata", d = as.double(d), as.integer(n), rowscore = as.double(rowscore)) : the enter point Fortran "orderdata_" absent of the loading table. What should I do to make sure that upon the installation of my package my Fortran function will appear in the load table... Thanks a lot. Sébastien Durand __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- 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-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Example of environment
On 4/4/2006 4:33 AM, Henrik Bengtsson wrote: > A minor comment: in help(environment) the example starts with: > > ##-- all three give the same: > environment() > environment(environment) > .GlobalEnv > > but the comment is not true. The second returns the " namespace:base>". > > "R : Copyright 2006, The R Foundation for Statistical Computing > Version 2.3.0 alpha (2006-04-02 r37626)" You're right, the comment is wrong. I'll fix it. Duncan Murdohc __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Build failure with this morning's R-alpha
On 4 April 2006 at 07:12, Prof Brian Ripley wrote: | Note that libR.so was itself linked against -lm, so should resolve its | dependencies there. The front end has never been linked against -lm, nor | do I see that anything should have changed for a long time in the | front-end. Thanks for confirming -- that is what I was suspecting, little or no change at R's side of this. The problem may be with Debian's "pbuilder" chroot that sets up the build environment. I will contact its author and maintainer. | I was able to build a shared-library version of the current svn 36743 (I | was unable to identify which 'last night' this was, and | 'r-base-2.2.1.svn37626' seemed contradictory) from scratch on FC3 on ix86 | and x86_64 (and I built a slightly earlier version on Solaris yesterday). These are the nightly (in Copenhaguen time) builds provided via http://cran.r-project.org/src/base-prerelease and I just confirmed the problem with today's svn.37642. Dirk | | | On Mon, 3 Apr 2006, Dirk Eddelbuettel wrote: | | > | > Trying to build a Debian snapshot of R-alpha based on last night's tarball, | > it fails with what looks like a missing -lm linking directive: | > | > [...] | > gcc -I. -I../../src/include -I../../src/include -DHAVE_CONFIG_H -fpic -O2 -c version.c -o version.o | > gcc -I. -I../../src/include -I../../src/include -DHAVE_CONFIG_H -fpic -O2 -c vfonts.c -o vfonts.o | > g77 -fpic -O2 -c xxxpr.f -o xxxpr.o | > gcc -shared -o libR.so Rembedded.o CConverters.o CommandLineArgs.o Rdynload.o Renviron.o RNG.o apply.o arithmetic.o apse.o array.o attrib.o base.o bind.o builtin.o character.o coerce.o colors.o complex.o connections.o context.o cov.o cum.o dcf.o datetime.o debug.o deparse.o deriv.o dotcode.o dounzip.o dstruct.o duplicate.o engine.o envir.o errors.o eval.o format.o fourier.o gevents.o gram.o gram-ex.o graphics.o identical.o internet.o iosupport.o lapack.o list.o localecharset.o logic.o main.o mapply.o match.o memory.o model.o names.o objects.o optim.o optimize.o options.o par.o paste.o pcre.o platform.o plot.o plot3d.o plotmath.o print.o printarray.o printvector.o printutils.o qsort.o random.o regex.o registration.o relop.o rlocale.o saveload.o scan.o seq.o serialize.o size.o sort.o source.o split.o sprintf.o startup.o subassign.o subscript.o subset.o summary.o sysutils.o unique.o util.o version.o vfonts.o xxxpr.o `ls ../appl/*.o ../nmath/*.o ../unix/*.o 2>/dev/null`! -! | lb! | > las-3 -L/usr/lib/gcc/i486-linux-gnu/3.4.6 -lg2c -lm -lgcc_s -lreadline -lpcre -lbz2 -lz -ldl -lm | > make[5]: Entering directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > mkdir -p -- /tmp/buildd/r-base-2.2.1.svn37626/bin/exec | > mkdir -p -- /tmp/buildd/r-base-2.2.1.svn37626/lib | > make[5]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > make[4]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > make[4]: Entering directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > gcc -I. -I../../src/include -I../../src/include -DHAVE_CONFIG_H -fpic -O2 -c Rmain.c -o Rmain.o | > gcc -Wl,--export-dynamic -o R.bin Rmain.o -L../../lib -lR | > ../../lib/libR.so: undefined reference to `isfinite' | > collect2: ld returned 1 exit status | > make[4]: *** [R.bin] Error 1 | > make[4]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > make[3]: *** [R] Error 2 | > make[3]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626/src/main' | > make[2]: *** [R] Error 1 | > make[2]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626/src' | > make[1]: *** [R] Error 1 | > make[1]: Leaving directory `/tmp/buildd/r-base-2.2.1.svn37626' | > make: *** [make-arch-stamp] Error 2 | > pbuilder: Failed autobuilding of package | > | > I checked the svn logs, as it doesn't looks like this was addressed since the | > tarball was rolled. Am I the only one to have this happen? | > | > Dirk | > | > | | -- | 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 -- Hell, there are no rules here - we're trying to accomplish something. -- Thomas A. Edison __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Extending www.r-project.org features with a Wiki (PR#8744)
http://wiki.r-project.org is quite recent and not widely advertised yet because I want to finialize features and beta test them with a limited number of enthousiast people *before* making more publicity about it. It is dedicated to the kind of collaborative documentation that Victor Anyakin is asking for. Indeed, it is already available to everybody, i.e., those who use Google ;-) Best, Philippe Grosjean [EMAIL PROTECTED] wrote: > Try http://wiki.r-project.org (which you might have guessed or Googled: > it came up as the first item for me). > > You might want to report to the webmaster of www.r-project.org where you > looked for a link to this and failed to find one. (Both the webmaster > and the host machine have moved recently (or the machine is about to), so > various planned changes are delayed by the moves.) > > > On Mon, 3 Apr 2006, [EMAIL PROTECTED] wrote: > > >>Full_Name: Victor Anyakin >>Version: >>OS: >>Submission from: (NULL) (212.109.44.52) >> >> >>This is an enhancement request, >> >>probably, some sort of a Wiki engine could be installed on the web site, >>or some other system, which would cover some basic information about >>various aspectes of R. >> >>Currently, I am trying to find some information about GUI application >>development on R. A number of various projects use wiki's to provide such >>information to the new or experienced users. > > > Well, see the `Writing R Extensions' manual, the `Related Projects' link > on CRAN which links to a page on GUIs (and that links to another > mainifestation of the same Wiki). > > > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Return function from function with minimal environment
Hi, this relates to the question "How to set a former environment?" asked yesterday. What is the best way to to return a function with a minimal environment from a function? Here is a dummy example: foo <- function(huge) { scale <- mean(huge) function(x) { scale * x } } fcn <- foo(1:10e5) The problem with this approach is that the environment of 'fcn' does not only hold 'scale' but also the memory consuming object 'huge', i.e. env <- environment(fcn) ll(envir=env) # ll() from R.oo # member data.class dimension object.size # 1 hugenumeric 100 428 # 2 scalenumeric 1 36 save(env, file="temp.RData") file.info("temp.RData")$size # [1] 2007624 I generate quite a few of these and my 'huge' objects are of order 100Mb, and I want to keep memory usage as well as file sizes to a minimum. What I do now, is to remove variable from the local environment of 'foo' before returning, i.e. foo2 <- function(huge) { scale <- mean(huge) rm(huge) function(x) { scale * x } } fcn <- foo2(1:10e5) env <- environment(fcn) ll(envir=env) # member data.class dimension object.size # 1 scalenumeric 1 36 save(env, file="temp.RData") file.info("temp.RData")$size # [1] 156 Since my "foo" functions are complicated and contains many local variables, it becomes tedious to identify and remove all of them, so instead I try: foo3 <- function(huge) { scale <- mean(huge); env <- new.env(); assign("scale", scale, envir=env); bar <- function(x) { scale * x }; environment(bar) <- env; bar; } fcn <- foo3(1:10e5) But, env <- environment(fcn) save(env, file="temp.RData"); file.info("temp.RData")$size # [1] 2007720 When I try to set the parent environment of 'env' to emptyenv(), it does not work, e.g. fcn(2) # Error in fcn(2) : attempt to apply non-function but with the new.env(parent=baseenv()) it works fine. The "base" environment has the empty environment as a parent. So, I try to do the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but once again I get fcn(2) # Error in fcn(2) : attempt to apply non-function Apparently, I do not understand enough here. Please, enlighten me. In the meantime I stick with foo2(). Best, Henrik __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] change function's formals default values
On Mon, 3 Apr 2006, Andrew Finley wrote: > Hello, > > I'm passing a user defined function into my c code. Once this function > is in my c code, I'd like to iteratively change the values associated > with the parameters defined in the function's formal list then evaluate > the function using these newly set defaults (i.e., using lang1(fn)). > > My question is, how do I simply change the value associated with each > parameter in this function's formal list. I have messed around with the > SET_FORMAL function but can't figure out how to change these values. > Are you *sure* you want to do this? It seems that you should be able to use lexical scope to store this information in variables rather than default arguments and then you can use setVar to change the variables rather than changing the formals. -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On Tue, 4 Apr 2006, Henrik Bengtsson wrote: > Hi, > > this relates to the question "How to set a former environment?" asked > yesterday. What is the best way to to return a function with a > minimal environment from a function? Here is a dummy example: > > foo <- function(huge) { > scale <- mean(huge) > function(x) { scale * x } > } > > fcn <- foo(1:10e5) > > The problem with this approach is that the environment of 'fcn' does > not only hold 'scale' but also the memory consuming object 'huge', > i.e. > > env <- environment(fcn) > ll(envir=env) # ll() from R.oo > # member data.class dimension object.size > # 1 hugenumeric 100 428 > # 2 scalenumeric 1 36 > > save(env, file="temp.RData") > file.info("temp.RData")$size > # [1] 2007624 > > I generate quite a few of these and my 'huge' objects are of order > 100Mb, and I want to keep memory usage as well as file sizes to a > minimum. What I do now, is to remove variable from the local > environment of 'foo' before returning, i.e. > > foo2 <- function(huge) { > scale <- mean(huge) > rm(huge) > function(x) { scale * x } > } > > fcn <- foo2(1:10e5) > env <- environment(fcn) > ll(envir=env) > # member data.class dimension object.size > # 1 scalenumeric 1 36 > > save(env, file="temp.RData") > file.info("temp.RData")$size > # [1] 156 > > Since my "foo" functions are complicated and contains many local > variables, it becomes tedious to identify and remove all of them, so > instead I try: > > foo3 <- function(huge) { > scale <- mean(huge); > env <- new.env(); > assign("scale", scale, envir=env); > bar <- function(x) { scale * x }; > environment(bar) <- env; > bar; > } > > fcn <- foo3(1:10e5) > > But, > > env <- environment(fcn) > save(env, file="temp.RData"); > file.info("temp.RData")$size > # [1] 2007720 > > When I try to set the parent environment of 'env' to emptyenv(), it > does not work, e.g. > > fcn(2) > # Error in fcn(2) : attempt to apply non-function > > but with the new.env(parent=baseenv()) it works fine. The "base" > environment has the empty environment as a parent. So, I try to do > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > once again I get I don't think you want to remove baseenv() from the environment. If you do, no functions from baseenv will be visible inside fcn. These include "{" and "*", which are necessary for your function. I think the error message comes from being unable to find "{". Also, there is no memory use from having baseenv in the environment, since all the objects in baseenv are always present. -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
In R 2.3.0-to-be, I think you can do foo <- function(huge) { scale <- mean(huge) g <- function(x) { scale * x } environment(g) <- emptyenv() g } -roger Henrik Bengtsson wrote: > Hi, > > this relates to the question "How to set a former environment?" asked > yesterday. What is the best way to to return a function with a > minimal environment from a function? Here is a dummy example: > > foo <- function(huge) { > scale <- mean(huge) > function(x) { scale * x } > } > > fcn <- foo(1:10e5) > > The problem with this approach is that the environment of 'fcn' does > not only hold 'scale' but also the memory consuming object 'huge', > i.e. > > env <- environment(fcn) > ll(envir=env) # ll() from R.oo > # member data.class dimension object.size > # 1 hugenumeric 100 428 > # 2 scalenumeric 1 36 > > save(env, file="temp.RData") > file.info("temp.RData")$size > # [1] 2007624 > > I generate quite a few of these and my 'huge' objects are of order > 100Mb, and I want to keep memory usage as well as file sizes to a > minimum. What I do now, is to remove variable from the local > environment of 'foo' before returning, i.e. > > foo2 <- function(huge) { > scale <- mean(huge) > rm(huge) > function(x) { scale * x } > } > > fcn <- foo2(1:10e5) > env <- environment(fcn) > ll(envir=env) > # member data.class dimension object.size > # 1 scalenumeric 1 36 > > save(env, file="temp.RData") > file.info("temp.RData")$size > # [1] 156 > > Since my "foo" functions are complicated and contains many local > variables, it becomes tedious to identify and remove all of them, so > instead I try: > > foo3 <- function(huge) { > scale <- mean(huge); > env <- new.env(); > assign("scale", scale, envir=env); > bar <- function(x) { scale * x }; > environment(bar) <- env; > bar; > } > > fcn <- foo3(1:10e5) > > But, > > env <- environment(fcn) > save(env, file="temp.RData"); > file.info("temp.RData")$size > # [1] 2007720 > > When I try to set the parent environment of 'env' to emptyenv(), it > does not work, e.g. > > fcn(2) > # Error in fcn(2) : attempt to apply non-function > > but with the new.env(parent=baseenv()) it works fine. The "base" > environment has the empty environment as a parent. So, I try to do > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > once again I get > > fcn(2) > # Error in fcn(2) : attempt to apply non-function > > Apparently, I do not understand enough here. Please, enlighten me. In > the meantime I stick with foo2(). > > Best, > > Henrik > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] change function's formals default values
Hi Thomas, Thanks for the note. I'm not sure about a lot of things. Setting the formal defaults then calling the function seem straight forward. I just assumed it would be like setting list values, or the C equivalent of calling formals(fn)<-list(a=1, b=3). Following your suggestion, I can get the names of the parameters used in this user defined function, but am not sure how to do what you are suggesting. For instance, how do I call the function after I use setVar. Could you point me to an example? Thanks again! Andrew On Tue, 2006-04-04 at 07:21 -0700, Thomas Lumley wrote: > On Mon, 3 Apr 2006, Andrew Finley wrote: > > > Hello, > > > > I'm passing a user defined function into my c code. Once this function > > is in my c code, I'd like to iteratively change the values associated > > with the parameters defined in the function's formal list then evaluate > > the function using these newly set defaults (i.e., using lang1(fn)). > > > > My question is, how do I simply change the value associated with each > > parameter in this function's formal list. I have messed around with the > > SET_FORMAL function but can't figure out how to change these values. > > > > > > Are you *sure* you want to do this? It seems that you should be able to > use lexical scope to store this information in variables rather than > default arguments and then you can use setVar to change the variables > rather than changing the formals. > > -thomas > > Thomas Lumley Assoc. Professor, Biostatistics > [EMAIL PROTECTED] University of Washington, Seattle > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Research Fellow Department of Forest Resources University of Minnesota Office: 305 Green Hall Phone: (612) 624-1714 Fax: (612) 625-5212 web: http://blue.fr.umn.edu __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On Tue, 4 Apr 2006, Roger D. Peng wrote: > In R 2.3.0-to-be, I think you can do > > foo <- function(huge) { > scale <- mean(huge) > g <- function(x) { scale * x } > environment(g) <- emptyenv() > g > } You can, but you really don't want to and you will get the same error. A 'minimal environment' is baseenv(), since you are not going to be able to do very much without the primitives such as * (and even "{"), and (relevant here) you will never save anything from it so it will cost you nothing. In this example, 'scale' is supposed to being picked up from the environment. So (removing all the empty statements to save memory) foo <- function(huge) { scale <- mean(huge) env <- new.env(parent=baseenv()) assign("scale", scale, envir=env) bar <- function(x) { scale * x } environment(bar) <- env bar } is I think minimal baggage (and fcn saves in 153 bytes). > -roger > > Henrik Bengtsson wrote: >> Hi, >> >> this relates to the question "How to set a former environment?" asked >> yesterday. What is the best way to to return a function with a >> minimal environment from a function? Here is a dummy example: >> >> foo <- function(huge) { >> scale <- mean(huge) >> function(x) { scale * x } >> } >> >> fcn <- foo(1:10e5) >> >> The problem with this approach is that the environment of 'fcn' does >> not only hold 'scale' but also the memory consuming object 'huge', >> i.e. >> >> env <- environment(fcn) >> ll(envir=env) # ll() from R.oo >> # member data.class dimension object.size >> # 1 hugenumeric 100 428 >> # 2 scalenumeric 1 36 >> >> save(env, file="temp.RData") >> file.info("temp.RData")$size >> # [1] 2007624 >> >> I generate quite a few of these and my 'huge' objects are of order >> 100Mb, and I want to keep memory usage as well as file sizes to a >> minimum. What I do now, is to remove variable from the local >> environment of 'foo' before returning, i.e. >> >> foo2 <- function(huge) { >> scale <- mean(huge) >> rm(huge) >> function(x) { scale * x } >> } >> >> fcn <- foo2(1:10e5) >> env <- environment(fcn) >> ll(envir=env) >> # member data.class dimension object.size >> # 1 scalenumeric 1 36 >> >> save(env, file="temp.RData") >> file.info("temp.RData")$size >> # [1] 156 >> >> Since my "foo" functions are complicated and contains many local >> variables, it becomes tedious to identify and remove all of them, so >> instead I try: >> >> foo3 <- function(huge) { >> scale <- mean(huge); >> env <- new.env(); >> assign("scale", scale, envir=env); >> bar <- function(x) { scale * x }; >> environment(bar) <- env; >> bar; >> } >> >> fcn <- foo3(1:10e5) >> >> But, >> >> env <- environment(fcn) >> save(env, file="temp.RData"); >> file.info("temp.RData")$size >> # [1] 2007720 >> >> When I try to set the parent environment of 'env' to emptyenv(), it >> does not work, e.g. >> >> fcn(2) >> # Error in fcn(2) : attempt to apply non-function >> >> but with the new.env(parent=baseenv()) it works fine. The "base" >> environment has the empty environment as a parent. So, I try to do >> the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but >> once again I get >> >> fcn(2) >> # Error in fcn(2) : attempt to apply non-function >> >> Apparently, I do not understand enough here. Please, enlighten me. In >> the meantime I stick with foo2(). >> >> Best, >> >> Henrik >> >> __ >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > -- 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-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On 4/4/06, Thomas Lumley <[EMAIL PROTECTED]> wrote: > On Tue, 4 Apr 2006, Henrik Bengtsson wrote: > > > Hi, > > > > this relates to the question "How to set a former environment?" asked > > yesterday. What is the best way to to return a function with a > > minimal environment from a function? Here is a dummy example: > > > > foo <- function(huge) { > > scale <- mean(huge) > > function(x) { scale * x } > > } > > > > fcn <- foo(1:10e5) > > > > The problem with this approach is that the environment of 'fcn' does > > not only hold 'scale' but also the memory consuming object 'huge', > > i.e. > > > > env <- environment(fcn) > > ll(envir=env) # ll() from R.oo > > # member data.class dimension object.size > > # 1 hugenumeric 100 428 > > # 2 scalenumeric 1 36 > > > > save(env, file="temp.RData") > > file.info("temp.RData")$size > > # [1] 2007624 > > > > I generate quite a few of these and my 'huge' objects are of order > > 100Mb, and I want to keep memory usage as well as file sizes to a > > minimum. What I do now, is to remove variable from the local > > environment of 'foo' before returning, i.e. > > > > foo2 <- function(huge) { > > scale <- mean(huge) > > rm(huge) > > function(x) { scale * x } > > } > > > > fcn <- foo2(1:10e5) > > env <- environment(fcn) > > ll(envir=env) > > # member data.class dimension object.size > > # 1 scalenumeric 1 36 > > > > save(env, file="temp.RData") > > file.info("temp.RData")$size > > # [1] 156 > > > > Since my "foo" functions are complicated and contains many local > > variables, it becomes tedious to identify and remove all of them, so > > instead I try: > > > > foo3 <- function(huge) { > > scale <- mean(huge); > > env <- new.env(); > > assign("scale", scale, envir=env); > > bar <- function(x) { scale * x }; > > environment(bar) <- env; > > bar; > > } > > > > fcn <- foo3(1:10e5) > > > > But, > > > > env <- environment(fcn) > > save(env, file="temp.RData"); > > file.info("temp.RData")$size > > # [1] 2007720 > > > > When I try to set the parent environment of 'env' to emptyenv(), it > > does not work, e.g. > > > > fcn(2) > > # Error in fcn(2) : attempt to apply non-function > > > > but with the new.env(parent=baseenv()) it works fine. The "base" > > environment has the empty environment as a parent. So, I try to do > > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > > once again I get > > I don't think you want to remove baseenv() from the environment. If you > do, no functions from baseenv will be visible inside fcn. These include > "{" and "*", which are necessary for your function. I think the error > message comes from being unable to find "{". Thank you, this makes sense. Modifying Roger Peng's example illustrates what you say: foo <- function(huge) { scale <- mean(huge) g <- function(x) x environment(g) <- emptyenv() g } fcn <- foo(1:10e5) fcn(2) # [1] 2 But as soon as you add "something" to the g(), it is missing; foo <- function(huge) { scale <- mean(huge) g <- function(x) { x } environment(g) <- emptyenv() g } fcn <- foo(1:10e5) fcn(2) # Error in fcn(2) : attempt to apply non-function ...and I did not know that "{" and "(" are primitive functions. Interesting. I conclude that 'env <- new.env(parent=baseenv())' is better than ''env <- new.env()' in my case. I learned something new. Thanks. Henrik > Also, there is no memory use from having baseenv in the environment, since > all the objects in baseenv are always present. > > -thomas > > > Thomas Lumley Assoc. Professor, Biostatistics > [EMAIL PROTECTED]University of Washington, Seattle > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > -- Henrik Bengtsson Mobile: +46 708 909208 (+2h UTC) __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On 4/4/06, Henrik Bengtsson <[EMAIL PROTECTED]> wrote: > On 4/4/06, Thomas Lumley <[EMAIL PROTECTED]> wrote: > > On Tue, 4 Apr 2006, Henrik Bengtsson wrote: > > > > > Hi, > > > > > > this relates to the question "How to set a former environment?" asked > > > yesterday. What is the best way to to return a function with a > > > minimal environment from a function? Here is a dummy example: > > > > > > foo <- function(huge) { > > > scale <- mean(huge) > > > function(x) { scale * x } > > > } > > > > > > fcn <- foo(1:10e5) > > > > > > The problem with this approach is that the environment of 'fcn' does > > > not only hold 'scale' but also the memory consuming object 'huge', > > > i.e. > > > > > > env <- environment(fcn) > > > ll(envir=env) # ll() from R.oo > > > # member data.class dimension object.size > > > # 1 hugenumeric 100 428 > > > # 2 scalenumeric 1 36 > > > > > > save(env, file="temp.RData") > > > file.info("temp.RData")$size > > > # [1] 2007624 > > > > > > I generate quite a few of these and my 'huge' objects are of order > > > 100Mb, and I want to keep memory usage as well as file sizes to a > > > minimum. What I do now, is to remove variable from the local > > > environment of 'foo' before returning, i.e. > > > > > > foo2 <- function(huge) { > > > scale <- mean(huge) > > > rm(huge) > > > function(x) { scale * x } > > > } > > > > > > fcn <- foo2(1:10e5) > > > env <- environment(fcn) > > > ll(envir=env) > > > # member data.class dimension object.size > > > # 1 scalenumeric 1 36 > > > > > > save(env, file="temp.RData") > > > file.info("temp.RData")$size > > > # [1] 156 > > > > > > Since my "foo" functions are complicated and contains many local > > > variables, it becomes tedious to identify and remove all of them, so > > > instead I try: > > > > > > foo3 <- function(huge) { > > > scale <- mean(huge); > > > env <- new.env(); > > > assign("scale", scale, envir=env); > > > bar <- function(x) { scale * x }; > > > environment(bar) <- env; > > > bar; > > > } > > > > > > fcn <- foo3(1:10e5) > > > > > > But, > > > > > > env <- environment(fcn) > > > save(env, file="temp.RData"); > > > file.info("temp.RData")$size > > > # [1] 2007720 > > > > > > When I try to set the parent environment of 'env' to emptyenv(), it > > > does not work, e.g. > > > > > > fcn(2) > > > # Error in fcn(2) : attempt to apply non-function > > > > > > but with the new.env(parent=baseenv()) it works fine. The "base" > > > environment has the empty environment as a parent. So, I try to do > > > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > > > once again I get > > > > I don't think you want to remove baseenv() from the environment. If you > > do, no functions from baseenv will be visible inside fcn. These include > > "{" and "*", which are necessary for your function. I think the error > > message comes from being unable to find "{". > > Thank you, this makes sense. Modifying Roger Peng's example > illustrates what you say: > > foo <- function(huge) { >scale <- mean(huge) >g <- function(x) x >environment(g) <- emptyenv() >g > } > > fcn <- foo(1:10e5) > fcn(2) > # [1] 2 > > But as soon as you add "something" to the g(), it is missing; > > foo <- function(huge) { >scale <- mean(huge) >g <- function(x) { x } >environment(g) <- emptyenv() >g > } > > fcn <- foo(1:10e5) > fcn(2) > # Error in fcn(2) : attempt to apply non-function > > ...and I did not know that "{" and "(" are primitive functions. Interesting. > > I conclude that 'env <- new.env(parent=baseenv())' is better than > ''env <- new.env()' in my case. Is there any reason to use env <- new.env(parent=baseenv()) instead of just env <- baseenv() ? The extra environment being created seems to serve no purpose. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
> "Roger" == Roger D Peng <[EMAIL PROTECTED]> > on Tue, 04 Apr 2006 10:38:29 -0400 writes: Roger> In R 2.3.0-to-be, I think you can do Roger> foo <- function(huge) { Roger> scale <- mean(huge) Roger> g <- function(x) { scale * x } Roger> environment(g) <- emptyenv() Roger> g Roger> } yes, but for now, and for the given purpose, also as seen in examples, such as splinefun() or approxfun(), just rm() the things you don't want in the environment. I.e., foo <- function(huge) { scale <- mean(huge) rm(huge) function(x) { scale * x } } Martin Roger> -roger Roger> Henrik Bengtsson wrote: >> Hi, >> >> this relates to the question "How to set a former environment?" asked >> yesterday. What is the best way to to return a function with a >> minimal environment from a function? Here is a dummy example: >> >> foo <- function(huge) { >> scale <- mean(huge) >> function(x) { scale * x } >> } >> >> fcn <- foo(1:10e5) >> >> The problem with this approach is that the environment of 'fcn' does >> not only hold 'scale' but also the memory consuming object 'huge', >> i.e. >> >> env <- environment(fcn) >> ll(envir=env) # ll() from R.oo >> # member data.class dimension object.size >> # 1 hugenumeric 100 428 >> # 2 scalenumeric 1 36 >> >> save(env, file="temp.RData") >> file.info("temp.RData")$size >> # [1] 2007624 >> >> I generate quite a few of these and my 'huge' objects are of order >> 100Mb, and I want to keep memory usage as well as file sizes to a >> minimum. What I do now, is to remove variable from the local >> environment of 'foo' before returning, i.e. >> >> foo2 <- function(huge) { >> scale <- mean(huge) >> rm(huge) >> function(x) { scale * x } >> } >> >> fcn <- foo2(1:10e5) >> env <- environment(fcn) >> ll(envir=env) >> # member data.class dimension object.size >> # 1 scalenumeric 1 36 >> >> save(env, file="temp.RData") >> file.info("temp.RData")$size >> # [1] 156 >> >> Since my "foo" functions are complicated and contains many local >> variables, it becomes tedious to identify and remove all of them, so >> instead I try: >> >> foo3 <- function(huge) { >> scale <- mean(huge); >> env <- new.env(); >> assign("scale", scale, envir=env); >> bar <- function(x) { scale * x }; >> environment(bar) <- env; >> bar; >> } >> >> fcn <- foo3(1:10e5) >> >> But, >> >> env <- environment(fcn) >> save(env, file="temp.RData"); >> file.info("temp.RData")$size >> # [1] 2007720 >> >> When I try to set the parent environment of 'env' to emptyenv(), it >> does not work, e.g. >> >> fcn(2) >> # Error in fcn(2) : attempt to apply non-function >> >> but with the new.env(parent=baseenv()) it works fine. The "base" >> environment has the empty environment as a parent. So, I try to do >> the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but >> once again I get >> >> fcn(2) >> # Error in fcn(2) : attempt to apply non-function >> >> Apparently, I do not understand enough here. Please, enlighten me. In >> the meantime I stick with foo2(). >> >> Best, >> >> Henrik >> >> __ >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> Roger> -- Roger> Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/ Roger> __ Roger> R-devel@r-project.org mailing list Roger> https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] request to add argv[0]
Dear R Developers: This has come up repeatedly in the r-help mailing list, most recently in a thread started by myself. The answers have been changing over the years. Would it be possible and easy for R to offer a global read-only option that gives the name of the currently executing R script, i.e., the equivalent of argv[0] in C? (PS: An even better mechanism would be the ability to pick off multiple arguments following the .R file, different from commandArgs(), but this is not as important and probably more difficult as it would change the invokation syntax of R.) sincerely, /ivo welch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On 4/4/06, Prof Brian Ripley <[EMAIL PROTECTED]> wrote: > On Tue, 4 Apr 2006, Roger D. Peng wrote: > > > In R 2.3.0-to-be, I think you can do > > > > foo <- function(huge) { > > scale <- mean(huge) > > g <- function(x) { scale * x } > > environment(g) <- emptyenv() > > g > > } > > You can, but you really don't want to and you will get the same error. A > 'minimal environment' is baseenv(), since you are not going to be able to > do very much without the primitives such as * (and even "{"), and > (relevant here) you will never save anything from it so it will cost you > nothing. In this example, 'scale' is supposed to being picked up from the > environment. So (removing all the empty statements to save memory) > > foo <- function(huge) { >scale <- mean(huge) >env <- new.env(parent=baseenv()) >assign("scale", scale, envir=env) >bar <- function(x) { scale * x } >environment(bar) <- env >bar > } > > is I think minimal baggage (and fcn saves in 153 bytes). Thanks. For (mine and other's) record: The base environment is very specially that it is always guaranteed to exists, and that save() knows about this too, because it does not warn about "package ... may not be available when loading". In the real example I'm trying to do, my returned function calls a function in the stats package. The naive approach is then to do: foo <- function(huge) { mu <- mean(huge) parent <- pos.to.env(which("package:stats" == search())); env <- new.env(parent=parent) assign("mu", mu, envir=env) bar <- function(n) { rnorm(n, mean=mu) } environment(bar) <- env bar } fcn <- foo(1:10) print(fcn(5)) env <- environment(fcn) save(env, file="temp.RData") However, then you get "Warning message: 'package:stats' may not be available when loading". To the best of my understanding right now, it is better to use "::" as below: foo <- function(huge) { mu <- mean(huge) env <- new.env(parent=baseenv()) assign("mu", mu, envir=env) bar <- function(n) { stats::rnorm(n, mean=mu) } environment(bar) <- env bar } /Henrik > > -roger > > > > Henrik Bengtsson wrote: > >> Hi, > >> > >> this relates to the question "How to set a former environment?" asked > >> yesterday. What is the best way to to return a function with a > >> minimal environment from a function? Here is a dummy example: > >> > >> foo <- function(huge) { > >> scale <- mean(huge) > >> function(x) { scale * x } > >> } > >> > >> fcn <- foo(1:10e5) > >> > >> The problem with this approach is that the environment of 'fcn' does > >> not only hold 'scale' but also the memory consuming object 'huge', > >> i.e. > >> > >> env <- environment(fcn) > >> ll(envir=env) # ll() from R.oo > >> # member data.class dimension object.size > >> # 1 hugenumeric 100 428 > >> # 2 scalenumeric 1 36 > >> > >> save(env, file="temp.RData") > >> file.info("temp.RData")$size > >> # [1] 2007624 > >> > >> I generate quite a few of these and my 'huge' objects are of order > >> 100Mb, and I want to keep memory usage as well as file sizes to a > >> minimum. What I do now, is to remove variable from the local > >> environment of 'foo' before returning, i.e. > >> > >> foo2 <- function(huge) { > >> scale <- mean(huge) > >> rm(huge) > >> function(x) { scale * x } > >> } > >> > >> fcn <- foo2(1:10e5) > >> env <- environment(fcn) > >> ll(envir=env) > >> # member data.class dimension object.size > >> # 1 scalenumeric 1 36 > >> > >> save(env, file="temp.RData") > >> file.info("temp.RData")$size > >> # [1] 156 > >> > >> Since my "foo" functions are complicated and contains many local > >> variables, it becomes tedious to identify and remove all of them, so > >> instead I try: > >> > >> foo3 <- function(huge) { > >> scale <- mean(huge); > >> env <- new.env(); > >> assign("scale", scale, envir=env); > >> bar <- function(x) { scale * x }; > >> environment(bar) <- env; > >> bar; > >> } > >> > >> fcn <- foo3(1:10e5) > >> > >> But, > >> > >> env <- environment(fcn) > >> save(env, file="temp.RData"); > >> file.info("temp.RData")$size > >> # [1] 2007720 > >> > >> When I try to set the parent environment of 'env' to emptyenv(), it > >> does not work, e.g. > >> > >> fcn(2) > >> # Error in fcn(2) : attempt to apply non-function > >> > >> but with the new.env(parent=baseenv()) it works fine. The "base" > >> environment has the empty environment as a parent. So, I try to do > >> the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > >> once again I get > >> > >> fcn(2) > >> # Error in fcn(2) : attempt to apply non-function > >> > >> Apparently, I do not understand enough here. Please, enlighten me. In > >> the meantime I stick with foo2(). > >> > >> Best, > >> > >> Henrik > >> > >> __ > >> R-devel@r-project.org mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-devel > >> > > > > > > -- > Brian D. Ripley,
Re: [Rd] Return function from function with minimal environment
On 4/4/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > On 4/4/06, Henrik Bengtsson <[EMAIL PROTECTED]> wrote: > > On 4/4/06, Thomas Lumley <[EMAIL PROTECTED]> wrote: > > > On Tue, 4 Apr 2006, Henrik Bengtsson wrote: > > > > > > > Hi, > > > > > > > > this relates to the question "How to set a former environment?" asked > > > > yesterday. What is the best way to to return a function with a > > > > minimal environment from a function? Here is a dummy example: > > > > > > > > foo <- function(huge) { > > > > scale <- mean(huge) > > > > function(x) { scale * x } > > > > } > > > > > > > > fcn <- foo(1:10e5) > > > > > > > > The problem with this approach is that the environment of 'fcn' does > > > > not only hold 'scale' but also the memory consuming object 'huge', > > > > i.e. > > > > > > > > env <- environment(fcn) > > > > ll(envir=env) # ll() from R.oo > > > > # member data.class dimension object.size > > > > # 1 hugenumeric 100 428 > > > > # 2 scalenumeric 1 36 > > > > > > > > save(env, file="temp.RData") > > > > file.info("temp.RData")$size > > > > # [1] 2007624 > > > > > > > > I generate quite a few of these and my 'huge' objects are of order > > > > 100Mb, and I want to keep memory usage as well as file sizes to a > > > > minimum. What I do now, is to remove variable from the local > > > > environment of 'foo' before returning, i.e. > > > > > > > > foo2 <- function(huge) { > > > > scale <- mean(huge) > > > > rm(huge) > > > > function(x) { scale * x } > > > > } > > > > > > > > fcn <- foo2(1:10e5) > > > > env <- environment(fcn) > > > > ll(envir=env) > > > > # member data.class dimension object.size > > > > # 1 scalenumeric 1 36 > > > > > > > > save(env, file="temp.RData") > > > > file.info("temp.RData")$size > > > > # [1] 156 > > > > > > > > Since my "foo" functions are complicated and contains many local > > > > variables, it becomes tedious to identify and remove all of them, so > > > > instead I try: > > > > > > > > foo3 <- function(huge) { > > > > scale <- mean(huge); > > > > env <- new.env(); > > > > assign("scale", scale, envir=env); > > > > bar <- function(x) { scale * x }; > > > > environment(bar) <- env; > > > > bar; > > > > } > > > > > > > > fcn <- foo3(1:10e5) > > > > > > > > But, > > > > > > > > env <- environment(fcn) > > > > save(env, file="temp.RData"); > > > > file.info("temp.RData")$size > > > > # [1] 2007720 > > > > > > > > When I try to set the parent environment of 'env' to emptyenv(), it > > > > does not work, e.g. > > > > > > > > fcn(2) > > > > # Error in fcn(2) : attempt to apply non-function > > > > > > > > but with the new.env(parent=baseenv()) it works fine. The "base" > > > > environment has the empty environment as a parent. So, I try to do > > > > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > > > > once again I get > > > > > > I don't think you want to remove baseenv() from the environment. If you > > > do, no functions from baseenv will be visible inside fcn. These include > > > "{" and "*", which are necessary for your function. I think the error > > > message comes from being unable to find "{". > > > > Thank you, this makes sense. Modifying Roger Peng's example > > illustrates what you say: > > > > foo <- function(huge) { > >scale <- mean(huge) > >g <- function(x) x > >environment(g) <- emptyenv() > >g > > } > > > > fcn <- foo(1:10e5) > > fcn(2) > > # [1] 2 > > > > But as soon as you add "something" to the g(), it is missing; > > > > foo <- function(huge) { > >scale <- mean(huge) > >g <- function(x) { x } > >environment(g) <- emptyenv() > >g > > } > > > > fcn <- foo(1:10e5) > > fcn(2) > > # Error in fcn(2) : attempt to apply non-function > > > > ...and I did not know that "{" and "(" are primitive functions. > > Interesting. > > > > I conclude that 'env <- new.env(parent=baseenv())' is better than > > ''env <- new.env()' in my case. > > Is there any reason to use > >env <- new.env(parent=baseenv()) > > instead of just > >env <- baseenv() ? > > The extra environment being created seems to serve no purpose. I need to do this, because I do not want to assign 'scale' to the base environment: foo <- function(huge) { scale <- mean(huge) env <- new.env(parent=baseenv()) # cf. env <- baseenv() assign("scale", scale, envir=env) bar <- function(x) { scale * x } environment(bar) <- env bar } /Henrik > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > -- Henrik Bengtsson Mobile: +46 708 909208 (+2h UTC) __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] request to add argv[0]
ivo welch wrote: > Dear R Developers: This has come up repeatedly in the r-help mailing > list, most recently in a thread started by myself. The answers have > been changing over the years. Would it be possible and easy for R to > offer a global read-only option that gives the name of the currently > executing R script, i.e., the equivalent of argv[0] in C? Isn't that just `commandArgs()[1]'? I must be misunderstanding your question. > > (PS: An even better mechanism would be the ability to pick off multiple >arguments following the .R file, different from commandArgs(), >but this is not as important and probably more difficult as it would change >the invokation syntax of R.) > > sincerely, > > /ivo welch > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] extending strsplit(): supply pattern to keep, not to split by
strsplit() is a convenient way to get a list of items from a string when you have a regular expression for what is not an item. E.g., > strsplit("1.2, 34, 1.7e-2", split="[ ,] *") [[1]]: [1] "1.2""34" "1.7e-2" However, sometimes is it more convenient to give a pattern for the items you do want. E.g., suppose you want to pull all the numbers out of a string which contains a mix of numbers and words. Making a pattern for what a number is simpler than making a pattern for what may come between the number. > number.pattern <- "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" I propose adding a keep=FALSE argument to strsplit() to do this. If keep is FALSE, then the split argument matches the stuff to omit from the output; if keep is TRUE then split matches the stuff to put into the output. Then we could do the following to get a list of all the numbers in a string (done in a version of strsplit() I'm working on for S-PLUS): > strsplit("1.2, 34, 1.7e-2", split=number.pattern,keep=TRUE) [[1]]: [1] "1.2""34" "1.7e-2" > strsplit("Ibuprofin 200mg", split=number.pattern,keep=TRUE) [[1]]: [1] "200" Is this a reasonable thing to want strsplit to do? Is this a reasonable parameterization of it? Bill Dunlap Insightful Corporation bill at insightful dot com 360-428-8146 "All statements in this message represent the opinions of the author and do not necessarily reflect Insightful Corporation policy or position." __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Return function from function with minimal environment
On 4/4/06, Henrik Bengtsson <[EMAIL PROTECTED]> wrote: > On 4/4/06, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > On 4/4/06, Henrik Bengtsson <[EMAIL PROTECTED]> wrote: > > > On 4/4/06, Thomas Lumley <[EMAIL PROTECTED]> wrote: > > > > On Tue, 4 Apr 2006, Henrik Bengtsson wrote: > > > > > > > > > Hi, > > > > > > > > > > this relates to the question "How to set a former environment?" asked > > > > > yesterday. What is the best way to to return a function with a > > > > > minimal environment from a function? Here is a dummy example: > > > > > > > > > > foo <- function(huge) { > > > > > scale <- mean(huge) > > > > > function(x) { scale * x } > > > > > } > > > > > > > > > > fcn <- foo(1:10e5) > > > > > > > > > > The problem with this approach is that the environment of 'fcn' does > > > > > not only hold 'scale' but also the memory consuming object 'huge', > > > > > i.e. > > > > > > > > > > env <- environment(fcn) > > > > > ll(envir=env) # ll() from R.oo > > > > > # member data.class dimension object.size > > > > > # 1 hugenumeric 100 428 > > > > > # 2 scalenumeric 1 36 > > > > > > > > > > save(env, file="temp.RData") > > > > > file.info("temp.RData")$size > > > > > # [1] 2007624 > > > > > > > > > > I generate quite a few of these and my 'huge' objects are of order > > > > > 100Mb, and I want to keep memory usage as well as file sizes to a > > > > > minimum. What I do now, is to remove variable from the local > > > > > environment of 'foo' before returning, i.e. > > > > > > > > > > foo2 <- function(huge) { > > > > > scale <- mean(huge) > > > > > rm(huge) > > > > > function(x) { scale * x } > > > > > } > > > > > > > > > > fcn <- foo2(1:10e5) > > > > > env <- environment(fcn) > > > > > ll(envir=env) > > > > > # member data.class dimension object.size > > > > > # 1 scalenumeric 1 36 > > > > > > > > > > save(env, file="temp.RData") > > > > > file.info("temp.RData")$size > > > > > # [1] 156 > > > > > > > > > > Since my "foo" functions are complicated and contains many local > > > > > variables, it becomes tedious to identify and remove all of them, so > > > > > instead I try: > > > > > > > > > > foo3 <- function(huge) { > > > > > scale <- mean(huge); > > > > > env <- new.env(); > > > > > assign("scale", scale, envir=env); > > > > > bar <- function(x) { scale * x }; > > > > > environment(bar) <- env; > > > > > bar; > > > > > } > > > > > > > > > > fcn <- foo3(1:10e5) > > > > > > > > > > But, > > > > > > > > > > env <- environment(fcn) > > > > > save(env, file="temp.RData"); > > > > > file.info("temp.RData")$size > > > > > # [1] 2007720 > > > > > > > > > > When I try to set the parent environment of 'env' to emptyenv(), it > > > > > does not work, e.g. > > > > > > > > > > fcn(2) > > > > > # Error in fcn(2) : attempt to apply non-function > > > > > > > > > > but with the new.env(parent=baseenv()) it works fine. The "base" > > > > > environment has the empty environment as a parent. So, I try to do > > > > > the same myself, i.e. new.env(parent=new.env(parent=emptyenv())), but > > > > > once again I get > > > > > > > > I don't think you want to remove baseenv() from the environment. If you > > > > do, no functions from baseenv will be visible inside fcn. These include > > > > "{" and "*", which are necessary for your function. I think the error > > > > message comes from being unable to find "{". > > > > > > Thank you, this makes sense. Modifying Roger Peng's example > > > illustrates what you say: > > > > > > foo <- function(huge) { > > >scale <- mean(huge) > > >g <- function(x) x > > >environment(g) <- emptyenv() > > >g > > > } > > > > > > fcn <- foo(1:10e5) > > > fcn(2) > > > # [1] 2 > > > > > > But as soon as you add "something" to the g(), it is missing; > > > > > > foo <- function(huge) { > > >scale <- mean(huge) > > >g <- function(x) { x } > > >environment(g) <- emptyenv() > > >g > > > } > > > > > > fcn <- foo(1:10e5) > > > fcn(2) > > > # Error in fcn(2) : attempt to apply non-function > > > > > > ...and I did not know that "{" and "(" are primitive functions. > > > Interesting. > > > > > > I conclude that 'env <- new.env(parent=baseenv())' is better than > > > ''env <- new.env()' in my case. > > > > Is there any reason to use > > > >env <- new.env(parent=baseenv()) > > > > instead of just > > > >env <- baseenv() ? > > > > The extra environment being created seems to serve no purpose. > > I need to do this, because I do not want to assign 'scale' to the base > environment: > > foo <- function(huge) { > scale <- mean(huge) > env <- new.env(parent=baseenv()) > # cf. env <- baseenv() > assign("scale", scale, envir=env) > bar <- function(x) { scale * x } > environment(bar) <- env > bar > } > OK. I think the example changed throughout the discussion and scale was not part of the latter examples. At any rate the version with scale could be reduced to one
Re: [Rd] extending strsplit(): supply pattern to keep, not to split by
gsubfn in package gsubfn can do this. See the examples in ?gsubfn On 4/4/06, Bill Dunlap <[EMAIL PROTECTED]> wrote: > strsplit() is a convenient way to get a > list of items from a string when you > have a regular expression for what is not > an item. E.g., > > > strsplit("1.2, 34, 1.7e-2", split="[ ,] *") > [[1]]: > [1] "1.2""34" "1.7e-2" > > However, sometimes is it more convenient to > give a pattern for the items you do want. > E.g., suppose you want to pull all the numbers > out of a string which contains a mix of numbers > and words. Making a pattern for what a > number is simpler than making a pattern > for what may come between the number. > > number.pattern <- > "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" > > I propose adding a keep=FALSE argument to > strsplit() to do this. If keep is FALSE, > then the split argument matches the stuff to > omit from the output; if keep is TRUE then > split matches the stuff to put into the > output. Then we could do the following to > get a list of all the numbers in a string > (done in a version of strsplit() I'm working on > for S-PLUS): > > > strsplit("1.2, 34, 1.7e-2", split=number.pattern,keep=TRUE) > [[1]]: > [1] "1.2""34" "1.7e-2" > > > strsplit("Ibuprofin 200mg", split=number.pattern,keep=TRUE) > [[1]]: > [1] "200" > > Is this a reasonable thing to want strsplit to do? > Is this a reasonable parameterization of it? > > > Bill Dunlap > Insightful Corporation > bill at insightful dot com > 360-428-8146 > > "All statements in this message represent the opinions of the author and do > not necessarily reflect Insightful Corporation policy or position." > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] change function's formals default values
On Tue, 4 Apr 2006, Andrew Finley wrote: > Hi Thomas, > > Thanks for the note. I'm not sure about a lot of things. Setting the > formal defaults then calling the function seem straight forward. I just > assumed it would be like setting list values, or the C equivalent of > calling formals(fn)<-list(a=1, b=3). > > Following your suggestion, I can get the names of the parameters used in > this user defined function, but am not sure how to do what you are > suggesting. For instance, how do I call the function after I use setVar. > Could you point me to an example? > ##In R f<-function(a,b){ function(x){ a+b*x} } f23<-f(a=2,b=3) dyn.load("cfun.so") .Call("cfun",fun=f23,rho=environment(f23),x=c(1,2,3,4),newa=1,newb=2) ## in cfun.c #include "Rinternals.h" SEXP cfun(SEXP fun, SEXP rho, SEXP x, SEXP newa, SEXP newb){ SEXP ans, funcall; setVar(install("a"), newa, rho); setVar(install("b"), newb, rho); PROTECT(funcall=lang2(fun,x)); ans=eval(funcall,rho); UNPROTECT(1); return ans; } ## This isn't necessarily any simpler, but it seems more natural to modify variables than default arguments, and also it is probably safer since you don't have to worry about the value of NAMED() and whether to copy the arguments. This is based on the more complicated code in cox_Rcallback.c in the survival package, for penalized Cox regression https://svn.r-project.org/R-packages/trunk/survival/src/cox_Rcallback.c -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] change function's formals default values
Your example is perfectly clear. I'll take your suggestion. Thanks a lot for your time- Andrew On Tue, 2006-04-04 at 10:00 -0700, Thomas Lumley wrote: > On Tue, 4 Apr 2006, Andrew Finley wrote: > > > Hi Thomas, > > > > Thanks for the note. I'm not sure about a lot of things. Setting the > > formal defaults then calling the function seem straight forward. I just > > assumed it would be like setting list values, or the C equivalent of > > calling formals(fn)<-list(a=1, b=3). > > > > Following your suggestion, I can get the names of the parameters used in > > this user defined function, but am not sure how to do what you are > > suggesting. For instance, how do I call the function after I use setVar. > > Could you point me to an example? > > > > ##In R > f<-function(a,b){ >function(x){ a+b*x} > } > > f23<-f(a=2,b=3) > > dyn.load("cfun.so") > .Call("cfun",fun=f23,rho=environment(f23),x=c(1,2,3,4),newa=1,newb=2) > > ## in cfun.c > > #include "Rinternals.h" > > SEXP cfun(SEXP fun, SEXP rho, SEXP x, SEXP newa, SEXP newb){ >SEXP ans, funcall; > >setVar(install("a"), newa, rho); >setVar(install("b"), newb, rho); > >PROTECT(funcall=lang2(fun,x)); >ans=eval(funcall,rho); > >UNPROTECT(1); >return ans; > > } > > ## > > This isn't necessarily any simpler, but it seems more natural to modify > variables than default arguments, and also it is probably safer since you > don't have to worry about the value of NAMED() and whether to copy the > arguments. > > This is based on the more complicated code in cox_Rcallback.c in the > survival package, for penalized Cox regression > https://svn.r-project.org/R-packages/trunk/survival/src/cox_Rcallback.c > > -thomas > > Thomas Lumley Assoc. Professor, Biostatistics > [EMAIL PROTECTED] University of Washington, Seattle -- Research Fellow Department of Forest Resources University of Minnesota Office: 305 Green Hall Phone: (612) 624-1714 Fax: (612) 625-5212 web: http://blue.fr.umn.edu __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] extending strsplit(): supply pattern to keep, not to split by
On Tue, 4 Apr 2006, Gabor Grothendieck wrote: > gsubfn in package gsubfn can do this. See the examples > in ?gsubfn Thanks. gsubfn looks useful, but may be overkill for this, and it isn't vectorized. To do what strsplit(keep=T) would do I think you need to do something like: > findMatches<-function(strings, pattern){ lapply(strings, function(string){ v <- character() gsubfn(number.pattern, function(x,...)v<<-c(v,x), string) v}) } > number.pattern <- "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" > findMatches(c("12;34:56,89,,12", "1.2, .4, 1., 1e3"), number.pattern) [[1]] [1] "12" "34" "56" "89" "12" [[2]] [1] "1.2" ".4" "1." "1e3" Is this worth encapsulating in a standard R function? If so, is doing via an extra argument to strsplit() a reasonable way to do it? > strsplit(c("12;34:56,89,,12", "1.2, .4, 1., 1e3"), number.pattern, keep=T) [[1]]: [1] "12" "34" "56" "89" "12" [[2]]: [1] "1.2" ".4" "1." "1e3" > On 4/4/06, Bill Dunlap <[EMAIL PROTECTED]> wrote: > > strsplit() is a convenient way to get a > > list of items from a string when you > > have a regular expression for what is not > > an item. E.g., > > > > > strsplit("1.2, 34, 1.7e-2", split="[ ,] *") > > [[1]]: > > [1] "1.2""34" "1.7e-2" > > > > However, sometimes is it more convenient to > > give a pattern for the items you do want. > > E.g., suppose you want to pull all the numbers > > out of a string which contains a mix of numbers > > and words. Making a pattern for what a > > number is simpler than making a pattern > > for what may come between the number. > > > number.pattern <- > > "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" > > > > I propose adding a keep=FALSE argument to > > strsplit() to do this. If keep is FALSE, > > then the split argument matches the stuff to > > omit from the output; if keep is TRUE then > > split matches the stuff to put into the > > output. Then we could do the following to > > get a list of all the numbers in a string > > (done in a version of strsplit() I'm working on > > for S-PLUS): > > > > > strsplit("1.2, 34, 1.7e-2", split=number.pattern,keep=TRUE) > > [[1]]: > > [1] "1.2""34" "1.7e-2" > > > > > strsplit("Ibuprofin 200mg", split=number.pattern,keep=TRUE) > > [[1]]: > > [1] "200" > > > > Is this a reasonable thing to want strsplit to do? > > Is this a reasonable parameterization of it? Bill Dunlap Insightful Corporation bill at insightful dot com 360-428-8146 "All statements in this message represent the opinions of the author and do not necessarily reflect Insightful Corporation policy or position." __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] extending strsplit(): supply pattern to keep, not to split by
On 4/4/06, Bill Dunlap <[EMAIL PROTECTED]> wrote: > On Tue, 4 Apr 2006, Gabor Grothendieck wrote: > > > gsubfn in package gsubfn can do this. See the examples > > in ?gsubfn > > Thanks. gsubfn looks useful, but may be overkill > for this, and it isn't vectorized. To do what gsubfn is vectorized. Its just that you are not using the output of gsubfn in this case. > strsplit(keep=T) would do I think you need to do something like: > > > findMatches<-function(strings, pattern){ >lapply(strings, function(string){ > v <- character() > gsubfn(number.pattern, function(x,...)v<<-c(v,x), string) > v}) > } > > number.pattern <- > "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" > > findMatches(c("12;34:56,89,,12", "1.2, .4, 1., 1e3"), number.pattern) > [[1]] > [1] "12" "34" "56" "89" "12" > > [[2]] > [1] "1.2" ".4" "1." "1e3" > > Is this worth encapsulating in a standard R function? I will likely add a wrapper to the gsubfn package for this. > If so, is doing via an extra argument to strsplit() > a reasonable way to do it? My current thought was to create a strapply function to do that. > > > strsplit(c("12;34:56,89,,12", "1.2, .4, 1., 1e3"), number.pattern, keep=T) > [[1]]: > [1] "12" "34" "56" "89" "12" > > [[2]]: > [1] "1.2" ".4" "1." "1e3" > > > > On 4/4/06, Bill Dunlap <[EMAIL PROTECTED]> wrote: > > > strsplit() is a convenient way to get a > > > list of items from a string when you > > > have a regular expression for what is not > > > an item. E.g., > > > > > > > strsplit("1.2, 34, 1.7e-2", split="[ ,] *") > > > [[1]]: > > > [1] "1.2""34" "1.7e-2" > > > > > > However, sometimes is it more convenient to > > > give a pattern for the items you do want. > > > E.g., suppose you want to pull all the numbers > > > out of a string which contains a mix of numbers > > > and words. Making a pattern for what a > > > number is simpler than making a pattern > > > for what may come between the number. > > > > number.pattern <- > > > "[-+]?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))([eE][+-]?[0-9]+)?" > > > > > > I propose adding a keep=FALSE argument to > > > strsplit() to do this. If keep is FALSE, > > > then the split argument matches the stuff to > > > omit from the output; if keep is TRUE then > > > split matches the stuff to put into the > > > output. Then we could do the following to > > > get a list of all the numbers in a string > > > (done in a version of strsplit() I'm working on > > > for S-PLUS): > > > > > > > strsplit("1.2, 34, 1.7e-2", split=number.pattern,keep=TRUE) > > > [[1]]: > > > [1] "1.2""34" "1.7e-2" > > > > > > > strsplit("Ibuprofin 200mg", split=number.pattern,keep=TRUE) > > > [[1]]: > > > [1] "200" > > > > > > Is this a reasonable thing to want strsplit to do? > > > Is this a reasonable parameterization of it? > > > Bill Dunlap > Insightful Corporation > bill at insightful dot com > 360-428-8146 > > "All statements in this message represent the opinions of the author and do > not necessarily reflect Insightful Corporation policy or position." > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Build failure with this morning's R-alpha
On 3 April 2006 at 16:11, Dirk Eddelbuettel wrote: | Trying to build a Debian snapshot of R-alpha based on last night's tarball, | it fails with what looks like a missing -lm linking directive: | | [...] | gcc -I. -I../../src/include -I../../src/include -DHAVE_CONFIG_H -fpic -O2 -c Rmain.c -o Rmain.o | gcc -Wl,--export-dynamic -o R.bin Rmain.o -L../../lib -lR | ../../lib/libR.so: undefined reference to `isfinite' Entirely my fault -- I had been resetting CFLAGS and friends in debian/rules, which lead to the all-important -std=gnu99 being dropped from the compiler flags. And gcc up to version 3.4.* seems to forgive this, whereas 4.* versions want it. Thanks, Dirk -- Hell, there are no rules here - we're trying to accomplish something. -- Thomas A. Edison __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
Full_Name: c fillekes Version: Version 2.2.1 (2005-12-20 r36812) OS: Gentoo Linux kernel 2.6.12 Submission from: (NULL) (129.116.71.233) "Not Available" is of course not a numeric: R > is.numeric (NA) [1] FALSE But for some reason, all arithmetic operations on NA's are in fact numeric, even if it's with other NA's. > is.numeric (NA+NA) [1] TRUE > is.numeric (NA^2) [1] TRUE > is.numeric (NA-NA) [1] TRUE > is.numeric (NA*NA) [1] TRUE > is.numeric (NA/NA) [1] TRUE > This is not the expected thing. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
2006/4/4, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > > Full_Name: c fillekes > Version: Version 2.2.1 (2005-12-20 r36812) > OS: Gentoo Linux kernel 2.6.12 > Submission from: (NULL) (129.116.71.233) > > > > > "Not Available" is of course not a numeric: > R > > > is.numeric (NA) > [1] FALSE In the help you can read: " 'NA' is a logical constant of length 1 which contains a missing value indicator. 'NA' can be freely coerced to any other vector type except raw. " And in fact: > storage.mode(NA) [1] "logical" > storage.mode(NA+0) [1] "double" > storage.mode(as.numeric(NA)) [1] "double" > storage.mode(as.character(NA)) [1] "character" So in NA+NA the logical is automatically coerced to a numerical value, and is.numericreturns TRUE, as expected. But for some reason, all arithmetic operations on NA's are > in fact numeric, even if it's with other NA's. > > > > is.numeric (NA+NA) > [1] TRUE > > is.numeric (NA^2) > [1] TRUE > > is.numeric (NA-NA) > [1] TRUE > > is.numeric (NA*NA) > [1] TRUE > > is.numeric (NA/NA) > [1] TRUE > > > > This is not the expected thing. I think this is the expected thing. __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > Antonio, Fabio Di Narzo. [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
On 4/4/2006 3:38 PM, [EMAIL PROTECTED] wrote: > Full_Name: c fillekes > Version: Version 2.2.1 (2005-12-20 r36812) > OS: Gentoo Linux kernel 2.6.12 > Submission from: (NULL) (129.116.71.233) > > > > > "Not Available" is of course not a numeric: > R > >> is.numeric (NA) > [1] FALSE > > But for some reason, all arithmetic operations on NA's are > in fact numeric, even if it's with other NA's. > > >> is.numeric (NA+NA) > [1] TRUE >> is.numeric (NA^2) > [1] TRUE >> is.numeric (NA-NA) > [1] TRUE >> is.numeric (NA*NA) > [1] TRUE >> is.numeric (NA/NA) > [1] TRUE >> > > This is not the expected thing. But it is the documented thing, so it is not a bug: "'is.numeric' returns 'TRUE' if its argument is of mode '"numeric"' (type '"double"' or type '"integer"') and not a factor, and 'FALSE' otherwise." The value is irrelevant. You probably want to use "is.numeric(x) && all(is.finite(x))" to get the result you're expecting. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
On Tue, 4 Apr 2006, Antonio, Fabio Di Narzo wrote: > So in > NA+NA > the logical is automatically coerced to a numerical value, and > is.numericreturns TRUE, as expected. > > But for some reason, all arithmetic operations on NA's are >> in fact numeric, even if it's with other NA's. >> All arithmetic operatiosn on *logicals* are coerced to numeric, so if you have two logical NAs and add them you get a numeric NA. This isn't anything special about NAs, since you would get the same conversion for TRUE+TRUE, and you would not get the same conversion for two character NAs -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
So this is perfectly consistent then: > A <- NA > B <- NA+NA > B [1] NA > A [1] NA > is.numeric (A) [1] FALSE > is.numeric (B) [1] TRUE > And the test based on value rather than type would be: > !is.na(A) [1] FALSE > !is.na(B) [1] FALSE On 4/4/06, Antonio, Fabio Di Narzo <[EMAIL PROTECTED]> wrote: > > 2006/4/4, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > > > Full_Name: c fillekes > > Version: Version 2.2.1 (2005-12-20 r36812) > > OS: Gentoo Linux kernel 2.6.12 > > Submission from: (NULL) (129.116.71.233) > > > > > > > > > > "Not Available" is of course not a numeric: > > R > > > > > is.numeric (NA) > > [1] FALSE > > > In the help you can read: > " > 'NA' is a logical constant of length 1 which contains a missing > value indicator. 'NA' can be freely coerced to any other vector > type except raw. > " > And in fact: > > > storage.mode(NA) > [1] "logical" > > storage.mode(NA+0) > [1] "double" > > storage.mode(as.numeric(NA)) > [1] "double" > > storage.mode(as.character(NA)) > [1] "character" > > So in > NA+NA > the logical is automatically coerced to a numerical value, and > is.numericreturns TRUE, as expected. > > > But for some reason, all arithmetic operations on NA's are > > in fact numeric, even if it's with other NA's. > > > > > > > is.numeric (NA+NA) > > [1] TRUE > > > is.numeric (NA^2) > > [1] TRUE > > > is.numeric (NA-NA) > > [1] TRUE > > > is.numeric (NA*NA) > > [1] TRUE > > > is.numeric (NA/NA) > > [1] TRUE > > > > > > > This is not the expected thing. > > > I think this is the expected thing. > > __ > > R-devel@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > > > > > Antonio, Fabio Di Narzo. > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] is.numeric (NA + NA) is TRUE, should be FALSE (PR#8745)
On 4/4/2006 6:38 PM, cheryl fillekes wrote: > So this is perfectly consistent then: > >> A <- NA >> B <- NA+NA >> B > [1] NA >> A > [1] NA A and B both print as NA, but they are stored differently. A is logical, B is numeric. You'll also see this in the following tests: > identical(A, A) [1] TRUE > identical(A, B) [1] FALSE >> is.numeric (A) > [1] FALSE >> is.numeric (B) > [1] TRUE > > And the test based on value rather than type would be: > >> !is.na(A) > [1] FALSE >> !is.na(B) > [1] FALSE Yes, that works. Duncan Murdoch > > > > On 4/4/06, Antonio, Fabio Di Narzo <[EMAIL PROTECTED]> wrote: >> 2006/4/4, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: >> >>> Full_Name: c fillekes >>> Version: Version 2.2.1 (2005-12-20 r36812) >>> OS: Gentoo Linux kernel 2.6.12 >>> Submission from: (NULL) (129.116.71.233) >>> >>> >>> >>> >>> "Not Available" is of course not a numeric: >>> R >>> is.numeric (NA) >>> [1] FALSE >> >> In the help you can read: >> " >> 'NA' is a logical constant of length 1 which contains a missing >> value indicator. 'NA' can be freely coerced to any other vector >> type except raw. >> " >> And in fact: >> >>> storage.mode(NA) >> [1] "logical" >>> storage.mode(NA+0) >> [1] "double" >>> storage.mode(as.numeric(NA)) >> [1] "double" >>> storage.mode(as.character(NA)) >> [1] "character" >> >> So in >> NA+NA >> the logical is automatically coerced to a numerical value, and >> is.numericreturns TRUE, as expected. >> >> >> But for some reason, all arithmetic operations on NA's are >>> in fact numeric, even if it's with other NA's. >>> >>> is.numeric (NA+NA) >>> [1] TRUE is.numeric (NA^2) >>> [1] TRUE is.numeric (NA-NA) >>> [1] TRUE is.numeric (NA*NA) >>> [1] TRUE is.numeric (NA/NA) >>> [1] TRUE >>> This is not the expected thing. >> >> I think this is the expected thing. >> >> __ >>> R-devel@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-devel >>> >> >> Antonio, Fabio Di Narzo. >> > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel