From ross at biostat.ucsf.edu  Thu Mar 20 07:45:10 2014
From: ross at biostat.ucsf.edu (Ross Boylan)
Date: Wed, 19 Mar 2014 23:45:10 -0700
Subject: [Rd] modifying data in a package [a solution]
In-Reply-To: <1395282143.1752.111.camel@localhost>
References: <1395282143.1752.111.camel@localhost>
Message-ID: <1395297910.1752.120.camel@localhost>

On Wed, 2014-03-19 at 19:22 -0700, Ross Boylan wrote:
> I've tweaked Rmpi and want to have some variables that hold data in the
> package.  One of the R files starts
> mpi.isend.obj <- vector("list", 500) #mpi.request.maxsize())                                                                          
> mpi.isend.inuse <- rep(FALSE, 500) #mpi.request.maxsize())    
> 
> and then functions update those variables with <<-.  When run:
>   Error in mpi.isend.obj[[i]] <<- .force.type(x, type) :                                                                                
>   cannot change value of locked binding for 'mpi.isend.obj'
> 
> I'm writing to ask the proper way to accomplish this objective (getting
> a variable I can update in package namespace--or at least somewhere
> useful and hidden from the outside).
> 
I've discovered one way to do it:
In one of the regular R files
mpi.global <- new.env()

Then at the end of .onLoad in zzz.R:
assign("mpi.isend.obj", vector("list", mpi.request.maxsize()),
mpi.global)
and similary for the logical vector mpi.isend.inuse

Access with functions like this:
## Next 2 functions have 3 modes                                                                                                      
##  foo()  returns foo from mpi.global                                                                                                
##  foo(request) returns foo[request] from mpi.global                                                                                 
##  foo(request, value) set foo[request] to value                                                                                     
mpi.isend.inuse <- function(request, value) {
    if (missing(request))
        return(get("mpi.isend.inuse", mpi.global))
    i <- request+1L
    parent.env(mpi.global) <- environment()
    if (missing(value))
        return(evalq(mpi.isend.inuse[i], mpi.global))
    return(evalq(mpi.isend.inuse[i] <- value, mpi.global))
}

# request, if present, must be a single value                                                                                         
mpi.isend.obj <- function(request, value){
    if (missing(request))
        return(get("mpi.isend.obj", mpi.global))
    i <- request+1L
    parent.env(mpi.global) <- environment()
    if (missing(value))
        return(evalq(mpi.isend.obj[[i]], mpi.global))
    return(evalq(mpi.isend.inuse[[i]] <- value, mpi.global))
}

This is pretty awkward; I'd love to know a better way.  Some of the
names probably should change too: mpi.isend.obj() sounds too much as if
it actually sends something, like mpi.isend.Robj().

Ross



[Rd] modifying data in a package [a solution]
Ross Boylan