Hello, 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.14Error: Object "base" not found Thanks for your help, Thomas Stabla --> version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 8.1 year 2003 month 11 day 21 language R
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 foundBase 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.) 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(). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Digressing somewhat from the namespace discussion, and focussing on> 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) >you might instead just put this info into a list perhaps called mypackageOptions, which would allow the user to change things such as mypackageOptions$username <- "Simon", or you could even write a friendlier wrapper (like options itself). I realise this is not as beautiful as you are trying to achieve, but it's there if you need a quick fix. Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
Maybe Matching Threads
- R CMD check errors
- NAMESPACE directives ?
- tcltk - tkcreate question
- after package reorganization: version 1.6 of package "distr" available; new packages "distrEx", "distrSim", "distrTEst", "RandVar"
- after package reorganization: version 1.6 of package "distr" available; new packages "distrEx", "distrSim", "distrTEst", "RandVar"