Skip to content

Repost: Expressions returned by GlobalEnv functions and package functions

2 messages · Saptarshi Guha, Oliver Ruebenacker

#
Hello,
(Sorry for the repost, i am resending in plain text)

I have a function 'ewrap' (see below for definition).
It takes 3 expressions and returns another expression e.g.

map <- ewrap({
    len <- length(r$addon)
    rhcollect(len,1)
  })

becomes:

expression({
    NULL
    result <- mapply(function(.index, k, r) {
        {
            len <- length(r$addon)
            rhcollect(len, 1)
        }
    }, 1:length(map.values), map.keys, map.values)
    NULL
})
attr(,"class")
[1] "expression" "rhmr-map"

ewrap is defined in the GlobalEnv. In my package (Rhipe), a function
rhwrap has the exact same definition

rhwap <-  function(co1=NULL,before=NULL,after=NULL){
  co <- substitute(co1); before=substitute(before)
  j <- as.expression(bquote({
    .(BE)
    result <- mapply(function(.index,k,r){
      .(CO)
    },1:length(map.values),map.keys,map.values)
    .(AF)
  },list(CO=co,BE=before,AF=after)))
  class(j) <- c(class(j),"rhmr-map")
  j
}

but the following two are different,

map <- ewrap({
    len <- length(r$addon)
    rhcollect(len,1)
  })

and

map2 <- rhwrap({
    len <- length(r$addon)
    rhcollect(len,1)
  })

(because serialize(map,NULL) != serialize(map2,NULL))

I guess this is because both functions(ewrap and rhwrap) return an
environment in which they are defined and in the case
of rhwrap this is the Rhipe package namespace/environment (i'm not
sure what jargon i should use here).

So my questions are:

1. how do i inspect the extra information that rhwrap is adding to its
return value
2. How do i remove this, so that it behaves like ewrap


Thanks in advance
Saptarshi
#
Hello Saptarshi,

  If two functions behave differently even though their definitions
are identical, then it is because they refer to variables that were in
scope when the functions were defined (i.e. lexical scoping). To
prevent such differences, either define both functions in the same
scope or have them read only arguments and local variables (i.e. pass
all needed input variables as arguments).

     Take care
     Oliver

On Thu, May 31, 2012 at 5:25 PM, Saptarshi Guha
<saptarshi.guha at gmail.com> wrote: