r - Assigning namespace variables inside of a function -
i'm struggling assign namespace variable inside of function. consider example using cran package "qcc": qcc() generates plot, display options of plot controlled qcc.options().
when working in global, fine:
library(qcc) qcc.options(bg.margin="red") # sets margin background colour, i.e. # qcc:::.qcc.options$bg.margin "red" qcc(rnorm(100), type = "xbar.one") # generates plot red margin but when working in local environment of function, qcc , qcc.options seem use namespace differently:
foo <- function(x){ qcc.options(bg.margin=x) qcc(rnorm(100), type = "xbar.one") } foo("green") # generates default plot grey margins
this because of qcc.options stores .qcc.options variable. working in global, qcc:::.qcc.options, when you're inside function, storing in local variable called .qcc.options, when try use plot.qcc (called qcc) retrieves options global (non-exported) qcc:::.qcc.options rather local .qcc.options.
here's function shows what's happening options:
bar <- function(x){ pre <- qcc:::.qcc.options pre.marg <- qcc.options("bg.margin") qcc.options(bg.margin=x) post1 <- qcc:::.qcc.options post2 <- .qcc.options post.marg <- qcc.options("bg.margin") qcc(rnorm(100), type = "xbar.one") list(pre,post1,post2,pre.marg,post.marg) } bar('green') if @ results, you'll see qcc.options creates local variable , changes value of bg.margin "green" isn't object that's subsequently referenced plot.qcc.
seems should request code modifications package maintainer because not best setup.
edit: workaround use assigninnamespace use local variable overwrite global one. (obviously, changes parameter globally , affect subsequent plots unless parameter updated.)
foo <- function(x){ qcc.options(bg.margin=x) assigninnamespace('.qcc.options',.qcc.options,ns='qcc') qcc(rnorm(100), type = "xbar.one") } foo('green') 
Comments
Post a Comment