Set values in namespaces
On Mon, 5 Jan 2004, Prof Brian Ripley wrote:
On Mon, 5 Jan 2004, Thomas Stabla wrote:
I want to use global variables in a package which is using a namespace. But I don`t know how to change the values of the global variables. I know how to get the value of the variables, e.g.
base::pi
[1] 3.141593 but following code doesn`t work
base::pi <- 3.14
Error: Object "base" not found
Base is a special case, and
assign("pi", 3.14, envir=NULL)
will do this (although please don't).
All other namespaces are sealed, so you will get something like
library(MASS)
assign("lda", pi, pos=2)
Error in assign("lda", pi, pos = 2) : can't change value of a locked binding
Now, that attempts to change the export, and not the value in the
namespace, so there is a question of which you want and why you want to
change it. (Changing the value in the namespace does not change the
export, which is a copy, but as _both_ the namespace and exports
environments are sealed, this would not matter much.)
In which manuals do I have to look to learn more about namespaces, exports and sealed environments? I`m not yet very familiar with this concepts. I want to be able to change the values, so that the user of the package can control in some way how some of the functions in the package will work. "useless" example: mypackage::username = "Thomas" mypackage::printusername = function() print(username)
There are ways around this and if you peruse the R sources you will find them. For example, grid sets up an environment in its namespace for its global variables, and in R-devel there is assignInNamespace().
Well, I looked at grid sources, and found the piece of code you mentioned, but wasn`t able to understand it fully. As a work-around, I now use the function assignInNamespace() from R-devel, which also works in R 1.8.1 if it`s pasted in the running R process. Thanks, Thomas Stabla