[Rd] Sharing variables in seperate workspace

Philip philip at sctkjeld.dk
Tue Apr 21 00:45:02 CEST 2009


Hi all.


I'm for the first time trying to make a library in R 2.8.1 on Ubuntu
Linux. The library is very simple, R functions just need to share a
variable (that is defined in the R code) and maybe functions and export
functions for the user to .GlobalEnv. This variable should only be
directly accessible from the global workspace (without change of
environment) via a hereto dedicated function. It only consists of R
code. I'm not sure if I should rather ask R-help. If so, just tell me.

The package source is available at
http://www.delff.dk/~philip/bdplot/
the check output at
http://www.delff.dk/~philip/bdplot.Rcheck/

The package code was checked and stopped because of lack of examples.
Until that, everything is OK.

> ### ** Examples
>
> ~~ simple examples of the most important functions ~~
Error: unexpected symbol in "~~ simple examples"
Execution halted

I would like to get the code issue solved before moving the
documentation from the R scripts and have proceeded the to building and
installation. These two last steps gave no warnings or errors.

My problem is that I cannot change the mentioned variable (from now on
called .FOO) that belongs to the namespace of the package. I think it is
because .FOO variable in some locked state, maybe by default created as
read-only. It is called .FOO because I use exportPattern("^[[:alpha:]]+")
as NAMESPACE file. I don't import anything.

I have a function (bar) that, like par does with .Pars from the graphics
namespace, modifies the contents of a list. This function has in the
bottom (when the function name is called without () from the R command
line) a line

<environment: namespace:"mypackage">

This is like par() has the graphics namespace mentioned, and therefore,
as I would expect. I can via this function read the contents of the
variable just by print(.FOO). This cannot be done from .GlobalEnv which
is as intended. The surprise is that I cannot write to it. If I do

.FOO$x <<- "value"
(names and values are arguments to the function, like with par().), I
get the error:

cannot change value of locked binding for '.FOO'

The double arrow, I use because <- does not affect the contents of .FOO
after the function has run. I don't know how I can use assign in this
case. I don't know a name for my package's environment. I have tried
with my package's name in both assign() and unlockBinding(), but my
package's name is not recognized as an environment (neither beginning
with a dot.). I also tryed unlockBinding() in the NAMESPACE file.

I don't know if this is normal, but I get
<environment: R_GlobalEnv>
from print(parent.frame()) and things like
<environment: 0x8df2ad0>
from print(environment())
This is surprising to me. I would expect
<environment: bdplot>
from environment().

How can I get to write to .FOO via bar()?


Thank you very much
Philip.



More information about the R-devel mailing list