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

Ross Boylan ross at biostat.ucsf.edu
Thu Mar 20 07:45:10 CET 2014


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



More information about the R-devel mailing list