[R] when setting environment: target of assignment expands to non-language object

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 28 02:31:28 CEST 2010


The environment of your function, as written, is the global
environment which means that free variables in it are looked up there
but you want it so that those free variables are looked up in the nlme
namespace first so set its environment as shown:

library(nlme)
nlme <- asNamespace("nlme")
unlockBinding("coef<-.corSpatial", nlme)
`coef<-.corSpatial` <- ... your function ...
environment(`coef<-.corSpatial`) <- nlme  ###
assignInNamespace("coef<-.corSpatial", `coef<-.corSpatial`, nlme)
lockBinding("coef<-.corSpatial", nlme)
mymodel <- lme(fixed = e ~ a,random= ~ 1 |
g,data=df,correlation=corExp(c(1,.5),form= ~ x + y | g,
nugget=TRUE),control=list(msVerbose=TRUE,opt="nlminb"))



On Tue, Apr 27, 2010 at 8:19 PM, Michael Steven Rooney
<michael.s.rooney at gmail.com> wrote:
> Thanks for your help. I am making great progress, but there are still some
> things that are puzzling me.
>
> In the code below, I add a cat() statement to `coef<-.corExp`. Sure enough,
> when I run lme(), it prints. (awesome!) However, the function does not seem
> to be able to find the C script "spatial_factList". I find that if I replace
> "spatial_factList" with "nlme:::spatial_factList" it works, but I am
> confused by why it cannot be found in the first case.
>
> Mike
>
>> set.seed(444)
>> N <- 100
>> x <- round(sin(rep(1:23/2,length.out=N)),digits=2)+1:N*2/N
>> y <- round(cos(rep(1:23/2,length.out=N)),digits=2)+1:N*2/N
>> g <- rep(1:5,each=N/5)
>> a <- round(runif(N,0,10))
>> t <- 1:N
>> r <- runif(N,0,5)
>> e <- 5*sin(4*x) +
> +      5*cos(4*y) +
> +      5*sin(t) +
> +      2*g +
> +      a +
> +      r
>> e <- round(e)
>>
>> df <- data.frame(x,y,g,a,t,r,e)
>> df <- df[-c(22,23,67),]
>>
>> library(nlme)
>> unlockBinding("coef<-.corSpatial",asNamespace("nlme"))
>> assignInNamespace("coef<-.corSpatial",
> + `coef<-.corSpatial` <- function (object, ..., value)
> + {
> +     cat("coef<-.corExp\n")
> +     if (length(value) != length(object)) {
> +         stop("Cannot change the length of the parameter after
> initialization")
> +     }
> +     object[] <- value
> +     corD <- attr(object, "Dim")
> +     aux <- .C(spatial_factList, as.double(as.vector(object)),
> +         as.integer(attr(object, "nugget")),
> as.double(unlist(getCovariate(object))),
> +         as.integer(unlist(corD)), as.double(attr(object, "minD")),
> +         factor = double(corD[["sumLenSq"]]), logDet =
> double(1))[c("factor",
> +         "logDet")]
> +     attr(object, "factor") <- aux[["factor"]]
> +     attr(object, "logDet") <- -aux[["logDet"]]
> +     object
> + },
> + asNamespace("nlme"))
>> lockBinding("coef<-.corSpatial",asNamespace("nlme"))
>> mymodel <- lme(fixed = e ~ a,random= ~ 1 |
>> g,data=df,correlation=corExp(c(1,.5),form= ~ x + y | g,
>> nugget=TRUE),control=list(msVerbose=TRUE,opt="nlminb"))
> coef<-.corExp
> Error in `coef<-.corSpatial`(`*tmp*`, value = c(0, 0)) :
>   object 'spatial_factList' not found
>
> On Tue, Apr 27, 2010 at 4:59 PM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>>
>> You did change it. Try nlme:::corExp
>>
>> Also note that asNamespace("nlme") is a nicer way to refer to it.
>>
>> On Tue, Apr 27, 2010 at 4:32 PM, Michael Steven Rooney
>> <michael.s.rooney at gmail.com> wrote:
>> > Sorry, I am still not understanding this. I tried using the unlock
>> > binding
>> > function, but the function remains unchanged after I attempt an
>> > assignment:
>> >
>> >> library(nlme)
>> >> bindingIsLocked("corExp",environment(corExp))
>> > [1] TRUE
>> >> unlockBinding("corExp",environment(corExp))
>> >> bindingIsLocked("corExp",environment(corExp))
>> > [1] FALSE
>> >> assignInNamespace("corExp",
>> > + function (value = numeric(0), form = ~1, nugget = FALSE, metric =
>> > c("euclidean",
>> > +     "maximum", "manhattan"), fixed = FALSE)
>> > + {
>> > +     cat("corExp\n")  ## SLIGHTLY CHANGE FUNCTION BY ADDING THIS LINE
>> > +     attr(value, "formula") <- form
>> > +     attr(value, "nugget") <- nugget
>> > +     attr(value, "metric") <- match.arg(metric)
>> > +     attr(value, "fixed") <- fixed
>> > +     class(value) <- c("corExp", "corSpatial", "corStruct")
>> > +     value
>> > + },
>> > + environment(corExp))
>> >> corExp
>> > function (value = numeric(0), form = ~1, nugget = FALSE, metric =
>> > c("euclidean",
>> >     "maximum", "manhattan"), fixed = FALSE)
>> > {
>> >     attr(value, "formula") <- form
>> >     attr(value, "nugget") <- nugget
>> >     attr(value, "metric") <- match.arg(metric)
>> >     attr(value, "fixed") <- fixed
>> >     class(value) <- c("corExp", "corSpatial", "corStruct")
>> >     value
>> > }
>> > <environment: namespace:nlme>
>> >
>> > On Mon, Apr 26, 2010 at 11:25 PM, Gabor Grothendieck
>> > <ggrothendieck at gmail.com> wrote:
>> >>
>> >> See ?lockBinding
>> >>
>> >> On Mon, Apr 26, 2010 at 11:20 PM, Michael Steven Rooney
>> >> <michael.s.rooney at gmail.com> wrote:
>> >> > I tried editing the corExp function (added a line) within the nlme
>> >> > environment with the following code, but it looks like my declaration
>> >> > did
>> >> > not have any effect. I am guessing it is locked some how. Is there an
>> >> > easy
>> >> > way to do this or am I treading into complicated waters? (I just
>> >> > picked
>> >> > up R
>> >> > a year ago, and I don't have any experience in development. Willing
>> >> > to
>> >> > learn.)
>> >> >
>> >> >> assignInNamespace("corExp",
>> >> > + function (value = numeric(0), form = ~1, nugget = FALSE, metric =
>> >> > c("euclidean",
>> >> > +     "maximum", "manhattan"), fixed = FALSE)
>> >> > + {
>> >> > +     cat("corExp\n")   # I ADDED THIS LINE HERE
>> >> > +     attr(value, "formula") <- form
>> >> > +     attr(value, "nugget") <- nugget
>> >> > +     attr(value, "metric") <- match.arg(metric)
>> >> > +     attr(value, "fixed") <- fixed
>> >> > +     class(value) <- c("corExp", "corSpatial", "corStruct")
>> >> > +     value
>> >> > + },
>> >> > + environment(corExp))
>> >> >> corExp
>> >> > function (value = numeric(0), form = ~1, nugget = FALSE, metric =
>> >> > c("euclidean",
>> >> >     "maximum", "manhattan"), fixed = FALSE)
>> >> > {
>> >> >     attr(value, "formula") <- form
>> >> >     attr(value, "nugget") <- nugget
>> >> >     attr(value, "metric") <- match.arg(metric)
>> >> >     attr(value, "fixed") <- fixed
>> >> >     class(value) <- c("corExp", "corSpatial", "corStruct")
>> >> >     value
>> >> > }
>> >> > <environment: namespace:nlme>
>> >> >
>> >> >
>> >> > On Mon, Apr 26, 2010 at 10:46 PM, Michael Steven Rooney
>> >> > <michael.s.rooney at gmail.com> wrote:
>> >> >>
>> >> >> Thanks.
>> >> >>
>> >> >> How do I make my function visible to others? Will assignInNamespace
>> >> >> do
>> >> >> that?
>> >> >>
>> >> >> On Mon, Apr 26, 2010 at 10:23 PM, Gabor Grothendieck
>> >> >> <ggrothendieck at gmail.com> wrote:
>> >> >>>
>> >> >>> See ?assignInNamespace
>> >> >>>
>> >> >>> On Mon, Apr 26, 2010 at 9:49 PM, Michael Steven Rooney
>> >> >>> <michael.s.rooney at gmail.com> wrote:
>> >> >>> > Hi,
>> >> >>> >
>> >> >>> > I am trying to place my own functions in the nlme environment:
>> >> >>> >
>> >> >>> > The following statement works:
>> >> >>> >
>> >> >>> > environment(coef.corSPT) <-
>> >> >>> > environment(getS3method("coef","corSpatial"))
>> >> >>> >
>> >> >>> > but this one returns an error:
>> >> >>> >
>> >> >>> > environment(get("coef<-.corSPT")) <-
>> >> >>> > environment(getS3method("coef<-","corSpatial"))
>> >> >>> > Error in environment(get("coef<-.corSPT")) <-
>> >> >>> > environment(getS3method("coef<-",  :
>> >> >>> >  target of assignment expands to non-language object
>> >> >>> >
>> >> >>> > What should I do?
>> >> >>> >
>> >> >>> > Thanks.
>> >> >>> >
>> >> >>> > Mike
>> >> >>> >
>> >> >>> >        [[alternative HTML version deleted]]
>> >> >>> >
>> >> >>> > ______________________________________________
>> >> >>> > R-help at 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.
>> >> >>> >
>> >> >>
>> >> >
>> >> >
>> >
>> >
>
>



More information about the R-help mailing list