[ESS] cannot add bindings to a locked environment

Boylan, Ross Ross.Boylan at ucsf.edu
Sat Jul 23 07:09:14 CEST 2016


The reason I was trying to set the environment was to get around the following problem.  I was going to  post this to the R list, but after debugging it I suspect it may be ess-related, and so I'm posting it here.

Sequence
1. start R 3.1 via ESS 16.04 in Emacs 24.4
2.in R, do library(LazarSim)
3. Attempt C-c C-t C-s to set environment, but fail with the previously reported type error.
3. Visit a file and read it in to R via C-c C-l.  The file is in the LazarSim source directory, which is below
the working directory for R.  The file contains code and classes that are not in the installed library loaded in step 1.
4. Attempt to use those classes:
> sb <- StandBonf(alpha=.1)
 > shortName(sb)
 Error in assign(".SigLength", nsig, envir = env) (from Test.R#13)   
  cannot add bindings to a locked environment

Debugging that error showed the env in the error message was <environment: namespace:LazarSim>

I expected that the file contents of step 3 would end up in the global namespace, and suspect that ESS must have fiddled things so that's not the case.  Of course, ideally I would like to insert them into the package namespace, and I thought that was what ESS's developer model was supposed to do.

Here's the file for step 3, minus comments
setClass("AbstractTest", representation=representation(alpha="numeric"))

setGeneric("test", function(self, r, ...) standardGeneric("test"))
                                                 
setGeneric("shortName", function(self) standardGeneric("shortName"))
setMethod("shortName", c("AbstractTest"),
          function(self) c(class(self)))

StandBonf <- setClass("StandBonf", contains="AbstractTest")
setMethod("test", c("StandBonf"),
          function(self, r, ...) covariates(bonf(r$all, alpha=self at alpha)))


Here's a slightly cleaned up transcript of the debugging:
 > traceback()
 4: assign(".SigLength", nsig, envir = env)
 3: .setupMethodsTables(fdef, initialize = TRUE)
 2: (function (fdef, env = environment(fdef), check = TRUE, inherited = FALSE)
    {
        name <- if (inherited)
            ".AllMTable"
        else ".MTable"
        if (check && !exists(name, envir = env, inherits = FALSE)) {
            .setupMethodsTables(fdef, initialize = TRUE)
            if (!exists(name, envir = env, inherits = FALSE))
                stop("invalid methods table request")
        }
        get(name, envir = env)
    })(function (self)
    standardGeneric("shortName")) at Test.R#13                                                                      
 1: shortName(sb)
 > debug(methods:::.setupMethodsTables)
 > shortName(sb)
 debugging in: .setupMethodsTables(fdef, initialize = TRUE)
 debug: {
     env <- environment(generic)
     if (initialize || !exists(".SigLength", envir = env, inherits = FALSE)) {
         nsig <- 1
         for (gp in generic at group) {
             gpDef <- getGeneric(gp)
             if (is(gpDef, "genericFunction")) {
                 .getMethodsTable(gpDef)
                 nsig <- max(nsig, get(".SigLength", envir = environment(gpDef)))
             }
         }
         assign(".SigLength", nsig, envir = env)
     }
     argSyms <- lapply(generic at signature, as.name)
     assign(".SigArgs", argSyms, envir = env)
     if (initialize) {
         mlist <- generic at default
         mtable <- .mlistAddToTable(generic, mlist)
         assign(".MTable", mtable, envir = env)
     }
     else mtable <- getMethodsForDispatch(generic)
     .resetInheritedMethods(env, mtable)
     if (is(generic, "groupGenericFunction")) {
         for (gp in generic at groupMembers) {
             gpDef <- getGeneric(gp)
             if (is(gpDef, "genericFunction"))
                 .getMethodsTable(gpDef)
         }
     }
     NULL
 }
 Browse[2]> s
 debug: env <- environment(generic)
 Browse[2]> initialize
 [1] TRUE
 Browse[2]> s
 debugging in: environment(generic)
 debug: .Internal(environment(fun))
 Browse[3]> n
 debug: if (initialize || !exists(".SigLength", envir = env, inherits = FALSE)) {
     nsig <- 1
     for (gp in generic at group) {
         gpDef <- getGeneric(gp)
         if (is(gpDef, "genericFunction")) {
             .getMethodsTable(gpDef)
             nsig <- max(nsig, get(".SigLength", envir = environment(gpDef)))
         }
     }
     assign(".SigLength", nsig, envir = env)
 }
 Browse[2]> env
 <environment: namespace:LazarSim>
 Browse[2]> n
 debug: nsig <- 1
 Browse[2]> n
 debug: for (gp in generic at group) {
     gpDef <- getGeneric(gp)
     if (is(gpDef, "genericFunction")) {
         .getMethodsTable(gpDef)
         nsig <- max(nsig, get(".SigLength", envir = environment(gpDef)))
     }
 }
 Browse[2]> n
 debug: assign(".SigLength", nsig, envir = env)
 Browse[2]> nsig
 [1] 1
!Browse[2]> 
 Error in assign(".SigLength", nsig, envir = env) (from Test.R#13) :                                                
   cannot add bindings to a locked environment





More information about the ESS-help mailing list