Displaying 20 results from an estimated 5000 matches similar to: "how to control the environment of a formula"
2013 Mar 20
1
behaviour of formula objects and environment inside functions
Dear List
I am looking for the recommended way to create a formula inside a
function with an empty environment. I tried several versions (see
below), and one of them seemed to work, but I dont understand why there
is a difference between .GlobalEnv and the environment inside a
function. I would be greatful for any reference or explanation or
advice.
Thanks
Thomas
#+BEGIN_SRC R :results output
2020 Aug 10
1
lm() takes weights from formula environment
Thank you for your suggestion. I do know how to work around the issue. I usually build a fresh environment as a child of base-environment and then insurt the weights there. I was just trying to provide an example of the issue.
emptyenv() can not be used, as it is needed for the eval (errors out even if weights are not used with "could not find function list").
For some applications
2019 Mar 23
4
topenv of emptyenv
I was surprised just now to find out that `topenv(emptyenv())` equals
? `.GlobalEnv`, not `emptyenv()`. From my understanding of the
description of `topenv`, it should walk up the chain of enclosing
environments (as if by calling `e = parent.env(e)` repeatedly; in
fact, that is almost exactly its implementation in envir.c) until it
hits a top level. However, `emptyenv()` has no enclosing
2015 Oct 13
1
A where() functions that does what exists() does but return the environment when object lives?
On Tue, Oct 13, 2015 at 4:43 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
> Seems easy enough to write yourself:
>
> where <- function(x, env = parent.frame()) {
> if (identical(env, emptyenv()))
> return(NULL)
> if (exists(x, envir = env, inherits = FALSE))
> return(env)
> where(x, parent.env(env))
> }
>
> sample2 <-
2010 Sep 01
2
testing for emptyenv
Dear R-users,
Is there a way to test whether a particular environment e is equal to emtyenv(), or for that sake whether two environments e1 and e2 are equal?
I tried a couple of ways to compare environments, but neither seem to work:
> e1 <- new.env()
> e2 <- new.env()
> e1 == e2
Error in e1 == e2 :
comparison (1) is possible only for atomic and list types
> all.equal(e1,
2006 Apr 04
2
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
2020 Aug 10
3
lm() takes weights from formula environment
I wish I had started with "I am disappointed that lm() doesn't continue its search for weights into the calling environment" or "the fact that lm() looks only in the formula environment and data frame for weights doesn't seem consistent with how other values are treated."
But I did not. So I do apologize for both that and for negative tone on my part.
Simplified
2020 Aug 10
0
lm() takes weights from formula environment
On 10/08/2020 1:42 p.m., John Mount wrote:
> I wish I had started with "I am disappointed that lm() doesn't continue its search for weights into the calling environment" or "the fact that lm() looks only in the formula environment and data frame for weights doesn't seem consistent with how other values are treated."
Normally searching is done automatically by
2006 Apr 25
7
R 2.3.0: Use of NULL as an environment is deprecated
Dear R-Devel subscriber,
first, let me express my thank to the R-Core team for the new release! I
appreciate their efforts and time spent to enhance R.
In accordance with the 'NEWS' file (see excerpt of it below),
[...
o Changed the environment tree to be rooted in an empty
environment, available as emptyenv(). baseenv() has been
modified to return an environment with emptyenv() as
2006 Apr 25
7
R 2.3.0: Use of NULL as an environment is deprecated
Dear R-Devel subscriber,
first, let me express my thank to the R-Core team for the new release! I
appreciate their efforts and time spent to enhance R.
In accordance with the 'NEWS' file (see excerpt of it below),
[...
o Changed the environment tree to be rooted in an empty
environment, available as emptyenv(). baseenv() has been
modified to return an environment with emptyenv() as
2005 Nov 04
1
Changes to environments in R-devel
I've just committed some changes to R-devel which affect environments.
Specifically:
- using NULL as an environment is now deprecated: use baseenv()
instead. (baseenv() is already available in R 2.2.0, where it returns
NULL. For most purposes it retains the same meaning in R-devel.) If you
do use NULL, it will be converted to baseenv(), and a warning printed.
For example:
> f
2008 Nov 17
4
functional (?) programming in r
the following is a trivialized version of some functional code i tried
to use in r:
(funcs = lapply(1:5, function(i) function() i))
# a list of no-parameter functions, each with its own closure environment,
# each supposed to return the corresponding index when applied to no
arguments
sapply(funcs, function(func) func())
# supposed to return c(1,2,3,4,5)
there is absolutely nothing unusual in
2005 Feb 08
1
Environment with no parent?
Is it possible to create an environment that has no parent (or an
empty parent)? I would have thought
e <- new.env(parent=NULL)
would work, but it acts as though the parent is the base namespace:
> get("close", envir = e)
function (con, ...)
UseMethod("close")
<environment: namespace:base>
I can use inherits = FALSE in this case:
> get("close",
2011 Mar 17
1
assigning to list element within target environment
I would like to assign an value to an element of a list contained in an
environment. The list will contain vectors and matrices. Here's a simple
example:
# create toy environment
testEnv = new.env(parent = emptyenv())
# create list that will be in the environment, then assign() it
x = list(a=1,b=2)
assign("xList",x,testEnv)
# create new element, to be inserted into xList
c = 5:7
2016 Aug 05
2
Extra copies of objects in environments when using $ operator?
My understanding is that R will not make copies of lists if there is
only one reference to the object. However, I've encountered a case
where R does make copies, even though (I think) there should be only
one reference to the object. I hope that someone could shed some light
on why this is happening.
I'll start with a simple example. Below, x is a list with one element,
and changing that
2008 May 10
2
Hashes as S4 Classes, or: How to separate environments
For learning purposes mainly I attempted to implement hashes/maps/dictionaries
(Python lingua) as S4 classes, see the coding below. I came across some rough S4
edges, but in the end it worked (for one dictionary).
When testing ones sees that the dictionaries D1 and D2 share their environments
D1 at hash and D2 at hash, though I thought a new and empty environment would be
generated each time
2011 Jun 09
2
Coercing Output from mget() into Proper Data Frame
Hello R-philes:
I have the following function that gets the output of mget() and
converts it to a data frame to return. What I am finding is that the
dimensions are wrong. Basically, I get:
bridesmaid wed u see m gt lt like love X.0 dress pagetrack one go X3 get
1 56 35 27 30 24 20 20 23 28 17 25 16 16 28 15 26
Instead, I want something like:
[1] bridesmaid
2009 Jun 02
2
formal argument "envir" matched by multiple actual arguments
Hi list,
This looks similar to the problem reported here
https://stat.ethz.ch/pipermail/r-devel/2006-April/037199.html
by Henrik Bengtsson a long time ago. It is very sporadic and
non-reproducible.
Henrik, do you remember if your code was using reg.finalizer()?
I tend to suspect it but I'm not sure.
I've been hunting this bug for months but today, and we the help of other
Bioconductor
2014 Oct 17
2
Most efficient way to check the length of a variable mentioned in a formula.
Dear R gurus,
I need to know the length of a variable (let's call that X) that is
mentioned in a formula. So obviously I look for the environment from which
the formula is called and then I have two options:
- using eval(parse(text='length(X)'),
envir=environment(formula) )
- using length(get('X'),
envir=environment(formula) )
a bit of
2019 Mar 28
0
topenv of emptyenv
>>>>> Konrad Rudolph
>>>>> on Sat, 23 Mar 2019 14:26:40 +0000 writes:
>>>>> Konrad Rudolph
>>>>> on Sat, 23 Mar 2019 14:26:40 +0000 writes:
> I was surprised just now to find out that `topenv(emptyenv())` equals
> ? `.GlobalEnv`, not `emptyenv()`. From my understanding of the
> description of `topenv`, it