[Rd] How do I modify an exported function in a locked environment?

From: Steven McKinney <smckinney_at_bccrc.ca>
Date: Thu 20 Jul 2006 - 19:49:14 GMT

Running R.app on Mac OS X 10.4

> version

               _                         
platform       powerpc-apple-darwin8.6.0 
arch           powerpc                   
os             darwin8.6.0               
system         powerpc, darwin8.6.0      
status                                   
major          2                         
minor          3.1                       
year           2006                      
month          06                        
day            01                        
svn rev        38247                     
language       R                         
version.string Version 2.3.1 (2006-06-01)
>

I am trying to learn how to modify functions in a locked environment.

For an example, suppose I'm using the package "zoo".

zoo contains function "rollmean.default"

> rollmean.default

function (x, k, na.pad = FALSE, align = c("center", "left", "right"),

    ...)
{

    x <- unclass(x)
    n <- length(x)
    y <- x[k:n] - x[c(1, 1:(n - k))]

    y[1] <- sum(x[1:k])
    rval <- cumsum(y)/k
    if (na.pad) {
        rval <- switch(match.arg(align), left = {
            c(rval, rep(NA, k - 1))
        }, center = {
            c(rep(NA, floor((k - 1)/2)), rval, rep(NA, ceiling((k - 
                1)/2)))
        }, right = {
            c(rep(NA, k - 1), rval)
        })

    }
    return(rval)
}
<environment: namespace:zoo>

Suppose for whatever reason I want output to be in percent, so I'd like to modify the result to be rval <- 100 * cumsum(y)/k

I cannot just copy the function and change it, as the namespace mechanism ensures the rollmean.default in 'zoo' continues to be used.

If I use
fixInNamespace("rollmean.default", ns = "zoo") I can edit the rval <- cumsum(y)/k line to read rval <- 100 * cumsum(y)/k
save the file and exit the R.app GUI editor.

But this does not update the exported copy of the function (the documentation for fixInNamespace says this is the case) - how do I accomplish this last step?

If I list the function after editing, I see the original copy. But if I reinvoke the editor via fixInNamespace(), I do see my modification.
Where is my copy residing? How do I push it out to replace the exported copy?

Is this the proper way to modify a package function? Are there other ways? I've searched webpages, R news, help files and have been unable to find out how to get this process fully completed.

Any guidance appreciated.

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre

email: smckinney@bccrc.ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri Jul 21 05:52:02 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 20 Jul 2006 - 22:28:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.