From ccberry at ucsd.edu  Fri Dec 11 03:24:52 2015
From: ccberry at ucsd.edu (Charles C. Berry)
Date: Thu, 10 Dec 2015 18:24:52 -0800
Subject: [Rd] How do I reliably and efficiently hash a function?
In-Reply-To: <CAM2gKPbSZjLUpVW87iaKczF-2eDHiT6GReu28SBJ98mSt8Djcg@mail.gmail.com>
References: <CAM2gKPbSZjLUpVW87iaKczF-2eDHiT6GReu28SBJ98mSt8Djcg@mail.gmail.com>
Message-ID: <alpine.OSX.2.20.1512101810510.2980@charles-berrys-macbook.local>

On Thu, 10 Dec 2015, Konrad Rudolph wrote:

> I?ve got the following scenario: I need to store information about an
> R function, and retrieve it at a later point. In other programming
> languages I?d implement this using a dictionary with the functions as
> keys. In R, I?d usually use `attr(f, 'some-name')`. However, for my
> purposes I do not want to use `attr` because the information that I
> want to store is an implementation detail that should be hidden from
> the user of the function (and, just as importantly, it shouldn?t
> clutter the display when the function is printed on the console).
>
> `comment` would be almost perfect since it?s hidden from the output
> when printing a function ? unfortunately, the information I?m storing
> is not a character string (it?s in fact an environment), so I cannot
> use `comment`.
>
> How can this be achieved?
>

See

https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Scope

For example, these commands:

foo <- function() {info <- "abc";function(x) x+1}
func <- foo()
find("func")
func(1)
ls(envir=environment(func))
get("info",environment(func))
func

Yield these printed results:

: [1] ".GlobalEnv"
: [1] 2
: [1] "info"
: [1] "abc"
: function (x)
: x + 1
: <environment: 0x7fbd5c86bc60>

The environment of the function gets printed, but 'info' and other
objects that might exist in that environment do not get printed unless
you explicitly call for them.

HTH,

Chuck

p.s. 'environment(func)$info' also works.


[Rd] How do I reliably and efficiently hash a function?
Charles C. Berry