[R-pkg-devel] Is there a better way ...?

Deepayan Sarkar deep@y@n@@@rk@r @end|ng |rom gm@||@com
Thu Oct 21 06:40:45 CEST 2021


On Thu, Oct 21, 2021 at 9:59 AM Rolf Turner <r.turner using auckland.ac.nz> wrote:
>
>
> I have a plot method (say plot.foo()) that I want to be able to call so
> that if argument "add" is set equal to TRUE, then further structure will
> be added to the same plot.  This is to be used *only* in the context in
> which the plot being added to was created using plot.foo().
>
> [Please don't ask me why I don't do everything in a single call to
> plot.foo().  There *are* reasons.]
>
> In order to make sure that the "further structure" has the appropriate
> form, the call to plot.foo(...,add=TRUE,...) needs to access information
> about what was done in the previous call to plot.foo().
>
> Initially I tried to effect this by creating, in a call of the form
> plot.foo(...,add=FALSE,...), an object, say ".fooInfo", in the global
> environment, and then having the call plot.foo(...,add=TRUE,...)
> access the necessary information from ".fooInfo".

Why not have a private (unexported) environment in your own package
for the same purpose? (That's the strategy used by lattice.) E.g., in
your zzz.R

.FooEnv <- new.env()

and then have your plot.foo write into / read from that. Long term, it
may be better to write accessor functions (which can still be
unexported if only used by your package code) if you want an
implementation-agnostic interface.

Best,
-Deepayan

> It all worked OK, but when I built my package for Windoze, using
> win-builder, I got a note of the form:
>
> > NOTE
> > Found the following assignment to the global environment:
> > File 'pckgename/R/plot.foo.R':
> >   assign(".fooInfo", fooInfo, pos = 1)
>
> I thought uh-oh; CRAN will kick up a stink if/when I submit my package.
>
> I think I've figured out a work-around using tempfile().  There
> are difficulties in that tempfile() creates unique names by tacking
> on an alpha-numeric string such as "38348397e595c" to the file name
> that I specify, so the call to plot.foo(...,add=TRUE,...) cannot know
> the *exact* file name.  I think I can get around that by grepping on
> "fooInfo" in list.files(tempdir()).  I also need to make sure that
> I unlink() any old instances of files in tempdir() with the string
> "fooInfo" in their names before creating a new instance.  Elsewise
> ambiguities will ensue.
>
> As I say --- I think this can all be made to work.  But ....
> Am I missing some "obvious" strategy?  I.e. is there a
> better/simpler/less convoluted way of handling this problem?
>
> Grateful for any pearls of wisdom.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list