Re: [R] Wrong environment when evaluating and expression?

2011-07-06 Thread Joshua Wiley
Thanks Bill!  That is very useful.  Is the str.language function in
any package (findFn(str.language) came up empty)?  It certainly
helped me, not only to understand this particular problem, but in
trying to wrap my head around language objects (which I only very
poorly grasp) in general.

Josh

On Tue, Jul 5, 2011 at 11:48 AM, William Dunlap wdun...@tibco.com wrote:
 -Original Message-
 From: r-help-boun...@r-project.org
 [mailto:r-help-boun...@r-project.org] On Behalf Of Joshua Wiley
 Sent: Monday, July 04, 2011 1:12 AM
 To: r-help@r-project.org
 Subject: [R] Wrong environment when evaluating and expression?

 Hi All,

 I have constructed two expressions (e1  e2).  I can see that they are
 not identical, but I cannot figure out how they differ.

 ###
 dat - mtcars
 e1 - expression(with(data = dat, lm(mpg ~ hp)))
 e2 - as.expression(substitute(with(data = dat, lm(f)),
 list(f = mpg ~ hp)))

 str(e1)
 str(e2)
 all.equal(e1, e2)
 identical(e1, e2) # false

 With the appended str.language function you can see the difference
 between e1 and e2.  It displays
  `name` class(length)
 of each component of a recursive object, along with a short text summary
 of
 it after a colon.

 str.language(e1)
 `e1` expression(1): expression(with(data = da...
  `` call(3): with(data = dat, lm(mpg ~...
    `` name(1): with
    `data` name(1): dat
    `` call(2): lm(mpg ~ hp)
      `` name(1): lm
      `` call(3): mpg ~ hp
        `` name(1): ~
        `` name(1): mpg
        `` name(1): hp
 str.language(e2)
 `e2` expression(1): expression(with(data = da...
  `` call(3): with(data = dat, lm(mpg ~...
    `` name(1): with
    `data` name(1): dat
    `` call(2): lm(mpg ~ hp)
      `` name(1): lm
      `` formula(3): mpg ~ hp
        `` name(1): ~
        `` name(1): mpg
        `` name(1): hp
        `Attributes of ` list(2): structure(list(class = f...
          `class` character(1): formula
          `.Environment` environment(5): R_GlobalEnv dat e1 e2 s...

 It is a bug in all.equal() that it ignores attributes of formulae.
 E.g.,

   all.equal(y~x, terms(y~x))
  [1] TRUE
   identical(y~x, terms(y~x))
  [1] FALSE

 Here is str.language

 str.language -
 function (object, ..., level = 0, name = deparse(substitute(object)),
    attributes = TRUE)
 {
    abbr - function(string, maxlen = 25) {
        if (length(string)  1 || nchar(string)  maxlen)
            paste(substring(string[1], 1, maxlen), ..., sep = )
        else string
    }
    myDeparse - function(object) {
        if (!is.environment(object)) {
            deparse(object)
        }
        else {
            ename - environmentName(object)
            if (ename == )
                ename - unnamed env
            paste(sep = , , ename,  , paste(collapse =  ,
                objects(object)))
        }
    }
    cat(rep(  , level), sep = )
    if (is.null(name))
        name - 
    cat(sprintf(`%s` %s(%d): %s\n, abbr(name), class(object),
        length(object), abbr(myDeparse(object
    a - attributes(object)
    if (is.recursive(object)  !is.environment(object)) {
        object - as.list(object)
        names - names(object)
        for (i in seq_along(object)) {
            str.language(object[[i]], ..., level = level + 1,
                name = names[i], attributes = attributes)
        }
    }
    if (attributes) {
        a$names - NULL
        if (length(a)  0) {
            str.language(a, level = level + 1, name = paste(Attributes
 of,
                abbr(name)), attributes = attributes)
        }
    }
 }

 Bill Dunlap
 Spotfire, TIBCO Software
 wdunlap tibco.com


 eval(e1)
 eval(e2)
 

 The context is trying to use a list of formulae to generate several
 models from a multiply imputed dataset.  The package I am using (mice)
 has methods for with() and that is how I can (easily) get the pooled
 results.  Passing the formula directly does not work, so I was trying
 to generate the entire call and evaluate it as if I had typed it at
 the console, but I am missing something (probably rather silly).

 Thanks,

 Josh


 --
 Joshua Wiley
 Ph.D. Student, Health Psychology
 University of California, Los Angeles
 http://www.joshuawiley.com/

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Wrong environment when evaluating and expression?

2011-07-06 Thread William Dunlap
No, it is not in any package.  Feel free to use
it as you wish - it has no licensing restrictions.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 -Original Message-
 From: Joshua Wiley [mailto:jwiley.ps...@gmail.com] 
 Sent: Wednesday, July 06, 2011 1:23 PM
 To: William Dunlap
 Cc: r-help@r-project.org
 Subject: Re: [R] Wrong environment when evaluating and expression?
 
 Thanks Bill!  That is very useful.  Is the str.language function in
 any package (findFn(str.language) came up empty)?  It certainly
 helped me, not only to understand this particular problem, but in
 trying to wrap my head around language objects (which I only very
 poorly grasp) in general.
 
 Josh
 
 On Tue, Jul 5, 2011 at 11:48 AM, William Dunlap 
 wdun...@tibco.com wrote:
  -Original Message-
  From: r-help-boun...@r-project.org
  [mailto:r-help-boun...@r-project.org] On Behalf Of Joshua Wiley
  Sent: Monday, July 04, 2011 1:12 AM
  To: r-help@r-project.org
  Subject: [R] Wrong environment when evaluating and expression?
 
  Hi All,
 
  I have constructed two expressions (e1  e2).  I can see 
 that they are
  not identical, but I cannot figure out how they differ.
 
  ###
  dat - mtcars
  e1 - expression(with(data = dat, lm(mpg ~ hp)))
  e2 - as.expression(substitute(with(data = dat, lm(f)),
  list(f = mpg ~ hp)))
 
  str(e1)
  str(e2)
  all.equal(e1, e2)
  identical(e1, e2) # false
 
  With the appended str.language function you can see the difference
  between e1 and e2.  It displays
   `name` class(length)
  of each component of a recursive object, along with a short 
 text summary
  of
  it after a colon.
 
  str.language(e1)
  `e1` expression(1): expression(with(data = da...
   `` call(3): with(data = dat, lm(mpg ~...
     `` name(1): with
     `data` name(1): dat
     `` call(2): lm(mpg ~ hp)
       `` name(1): lm
       `` call(3): mpg ~ hp
         `` name(1): ~
         `` name(1): mpg
         `` name(1): hp
  str.language(e2)
  `e2` expression(1): expression(with(data = da...
   `` call(3): with(data = dat, lm(mpg ~...
     `` name(1): with
     `data` name(1): dat
     `` call(2): lm(mpg ~ hp)
       `` name(1): lm
       `` formula(3): mpg ~ hp
         `` name(1): ~
         `` name(1): mpg
         `` name(1): hp
         `Attributes of ` list(2): structure(list(class = f...
           `class` character(1): formula
           `.Environment` environment(5): R_GlobalEnv dat e1 e2 s...
 
  It is a bug in all.equal() that it ignores attributes of formulae.
  E.g.,
 
    all.equal(y~x, terms(y~x))
   [1] TRUE
    identical(y~x, terms(y~x))
   [1] FALSE
 
  Here is str.language
 
  str.language -
  function (object, ..., level = 0, name = 
 deparse(substitute(object)),
     attributes = TRUE)
  {
     abbr - function(string, maxlen = 25) {
         if (length(string)  1 || nchar(string)  maxlen)
             paste(substring(string[1], 1, maxlen), ..., sep = )
         else string
     }
     myDeparse - function(object) {
         if (!is.environment(object)) {
             deparse(object)
         }
         else {
             ename - environmentName(object)
             if (ename == )
                 ename - unnamed env
             paste(sep = , , ename,  , paste(collapse =  ,
                 objects(object)))
         }
     }
     cat(rep(  , level), sep = )
     if (is.null(name))
         name - 
     cat(sprintf(`%s` %s(%d): %s\n, abbr(name), class(object),
         length(object), abbr(myDeparse(object
     a - attributes(object)
     if (is.recursive(object)  !is.environment(object)) {
         object - as.list(object)
         names - names(object)
         for (i in seq_along(object)) {
             str.language(object[[i]], ..., level = level + 1,
                 name = names[i], attributes = attributes)
         }
     }
     if (attributes) {
         a$names - NULL
         if (length(a)  0) {
             str.language(a, level = level + 1, name = 
 paste(Attributes
  of,
                 abbr(name)), attributes = attributes)
         }
     }
  }
 
  Bill Dunlap
  Spotfire, TIBCO Software
  wdunlap tibco.com
 
 
  eval(e1)
  eval(e2)
  
 
  The context is trying to use a list of formulae to generate several
  models from a multiply imputed dataset.  The package I am 
 using (mice)
  has methods for with() and that is how I can (easily) get 
 the pooled
  results.  Passing the formula directly does not work, so I 
 was trying
  to generate the entire call and evaluate it as if I had typed it at
  the console, but I am missing something (probably rather silly).
 
  Thanks,
 
  Josh
 
 
  --
  Joshua Wiley
  Ph.D. Student, Health Psychology
  University of California, Los Angeles
  http://www.joshuawiley.com/
 
  __
  R-help@r-project.org mailing list
  https://stat.ethz.ch/mailman/listinfo/r-help
  PLEASE do read the posting guide
  http://www.R-project.org/posting-guide.html
  and provide commented

Re: [R] Wrong environment when evaluating and expression?

2011-07-05 Thread William Dunlap
 -Original Message-
 From: r-help-boun...@r-project.org 
 [mailto:r-help-boun...@r-project.org] On Behalf Of Joshua Wiley
 Sent: Monday, July 04, 2011 1:12 AM
 To: r-help@r-project.org
 Subject: [R] Wrong environment when evaluating and expression?
 
 Hi All,
 
 I have constructed two expressions (e1  e2).  I can see that they are
 not identical, but I cannot figure out how they differ.
 
 ###
 dat - mtcars
 e1 - expression(with(data = dat, lm(mpg ~ hp)))
 e2 - as.expression(substitute(with(data = dat, lm(f)), 
 list(f = mpg ~ hp)))
 
 str(e1)
 str(e2)
 all.equal(e1, e2)
 identical(e1, e2) # false

With the appended str.language function you can see the difference
between e1 and e2.  It displays
  `name` class(length)
of each component of a recursive object, along with a short text summary
of
it after a colon.

 str.language(e1)
`e1` expression(1): expression(with(data = da...
  `` call(3): with(data = dat, lm(mpg ~...
`` name(1): with
`data` name(1): dat
`` call(2): lm(mpg ~ hp)
  `` name(1): lm
  `` call(3): mpg ~ hp
`` name(1): ~
`` name(1): mpg
`` name(1): hp
 str.language(e2)
`e2` expression(1): expression(with(data = da...
  `` call(3): with(data = dat, lm(mpg ~...
`` name(1): with
`data` name(1): dat
`` call(2): lm(mpg ~ hp)
  `` name(1): lm
  `` formula(3): mpg ~ hp
`` name(1): ~
`` name(1): mpg
`` name(1): hp
`Attributes of ` list(2): structure(list(class = f...
  `class` character(1): formula
  `.Environment` environment(5): R_GlobalEnv dat e1 e2 s...

It is a bug in all.equal() that it ignores attributes of formulae.
E.g.,

   all.equal(y~x, terms(y~x))
  [1] TRUE
   identical(y~x, terms(y~x))
  [1] FALSE

Here is str.language

str.language -
function (object, ..., level = 0, name = deparse(substitute(object)),
attributes = TRUE)
{
abbr - function(string, maxlen = 25) {
if (length(string)  1 || nchar(string)  maxlen)
paste(substring(string[1], 1, maxlen), ..., sep = )
else string
}
myDeparse - function(object) {
if (!is.environment(object)) {
deparse(object)
}
else {
ename - environmentName(object)
if (ename == )
ename - unnamed env
paste(sep = , , ename,  , paste(collapse =  ,
objects(object)))
}
}
cat(rep(  , level), sep = )
if (is.null(name))
name - 
cat(sprintf(`%s` %s(%d): %s\n, abbr(name), class(object),
length(object), abbr(myDeparse(object
a - attributes(object)
if (is.recursive(object)  !is.environment(object)) {
object - as.list(object)
names - names(object)
for (i in seq_along(object)) {
str.language(object[[i]], ..., level = level + 1,
name = names[i], attributes = attributes)
}
}
if (attributes) {
a$names - NULL
if (length(a)  0) {
str.language(a, level = level + 1, name = paste(Attributes
of,
abbr(name)), attributes = attributes)
}
}
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

 
 eval(e1)
 eval(e2)
 
 
 The context is trying to use a list of formulae to generate several
 models from a multiply imputed dataset.  The package I am using (mice)
 has methods for with() and that is how I can (easily) get the pooled
 results.  Passing the formula directly does not work, so I was trying
 to generate the entire call and evaluate it as if I had typed it at
 the console, but I am missing something (probably rather silly).
 
 Thanks,
 
 Josh
 
 
 -- 
 Joshua Wiley
 Ph.D. Student, Health Psychology
 University of California, Los Angeles
 http://www.joshuawiley.com/
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Wrong environment when evaluating and expression?

2011-07-04 Thread Joshua Wiley
Hi All,

I have constructed two expressions (e1  e2).  I can see that they are
not identical, but I cannot figure out how they differ.

###
dat - mtcars
e1 - expression(with(data = dat, lm(mpg ~ hp)))
e2 - as.expression(substitute(with(data = dat, lm(f)), list(f = mpg ~ hp)))

str(e1)
str(e2)
all.equal(e1, e2)
identical(e1, e2) # false

eval(e1)
eval(e2)


The context is trying to use a list of formulae to generate several
models from a multiply imputed dataset.  The package I am using (mice)
has methods for with() and that is how I can (easily) get the pooled
results.  Passing the formula directly does not work, so I was trying
to generate the entire call and evaluate it as if I had typed it at
the console, but I am missing something (probably rather silly).

Thanks,

Josh


-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Wrong environment when evaluating and expression?

2011-07-04 Thread Gabor Grothendieck
On Mon, Jul 4, 2011 at 4:11 AM, Joshua Wiley jwiley.ps...@gmail.com wrote:
 Hi All,

 I have constructed two expressions (e1  e2).  I can see that they are
 not identical, but I cannot figure out how they differ.

 ###
 dat - mtcars
 e1 - expression(with(data = dat, lm(mpg ~ hp)))
 e2 - as.expression(substitute(with(data = dat, lm(f)), list(f = mpg ~ hp)))

 str(e1)
 str(e2)
 all.equal(e1, e2)
 identical(e1, e2) # false

 eval(e1)
 eval(e2)
 

 The context is trying to use a list of formulae to generate several
 models from a multiply imputed dataset.  The package I am using (mice)
 has methods for with() and that is how I can (easily) get the pooled
 results.  Passing the formula directly does not work, so I was trying
 to generate the entire call and evaluate it as if I had typed it at
 the console, but I am missing something (probably rather silly).


In e1, mpg ~ hp is a call object but in e2 its a formula with an environment:

 e1[[1]][[3]][[2]]
mpg ~ hp
 e2[[1]][[3]][[2]]
mpg ~ hp

 class(e1[[1]][[3]][[2]])
[1] call
 class(e2[[1]][[3]][[2]])
[1] formula

 environment(e2[[1]][[3]][[2]])
environment: R_GlobalEnv

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Wrong environment when evaluating and expression?

2011-07-04 Thread Joshua Wiley
Thanks Gabor, that makes sense now.

In case anyone else runs into something similar, I ended up just
passing a character string of the formula so it could be coerced to a
formula in the correct environment.

Thanks again,

Josh

On Mon, Jul 4, 2011 at 4:26 AM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 On Mon, Jul 4, 2011 at 4:11 AM, Joshua Wiley jwiley.ps...@gmail.com wrote:
 Hi All,

 I have constructed two expressions (e1  e2).  I can see that they are
 not identical, but I cannot figure out how they differ.

 ###
 dat - mtcars
 e1 - expression(with(data = dat, lm(mpg ~ hp)))
 e2 - as.expression(substitute(with(data = dat, lm(f)), list(f = mpg ~ hp)))

 str(e1)
 str(e2)
 all.equal(e1, e2)
 identical(e1, e2) # false

 eval(e1)
 eval(e2)
 

 The context is trying to use a list of formulae to generate several
 models from a multiply imputed dataset.  The package I am using (mice)
 has methods for with() and that is how I can (easily) get the pooled
 results.  Passing the formula directly does not work, so I was trying
 to generate the entire call and evaluate it as if I had typed it at
 the console, but I am missing something (probably rather silly).


 In e1, mpg ~ hp is a call object but in e2 its a formula with an environment:

 e1[[1]][[3]][[2]]
 mpg ~ hp
 e2[[1]][[3]][[2]]
 mpg ~ hp

 class(e1[[1]][[3]][[2]])
 [1] call
 class(e2[[1]][[3]][[2]])
 [1] formula

 environment(e2[[1]][[3]][[2]])
 environment: R_GlobalEnv

 --
 Statistics  Software Consulting
 GKX Group, GKX Associates Inc.
 tel: 1-877-GKX-GROUP
 email: ggrothendieck at gmail.com




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.