Re: [R] Hiding a function

From: Christophe Genolini <cgenolin_at_u-paris10.fr>
Date: Sat, 23 Feb 2008 16:58:00 +0100

After some private email with some high level programmer (Gabor), I have the solution and I can answer to my own question :-)

The problem was 1 to define a function B( ) available only in a function A( ) (for code safeness) and 2 to define B( ) elsewhere than in A( ) (for code readability)

The solution is the following

# Definition of an environment that will be use in A() (a list will work as well) envA <- new.env()

# Definition of B(), in environment envA : envA$b <- function(x) x2

# Definition of A :
A <- function(x) with(envA, {
  B(3)
})

Thanks

Christophe

> On 23/02/2008 5:15 AM, Christophe Genolini wrote:

>> Hi the list
>>
>> Is it possible to 'hide' a function from the user ? I cut a big
>> fonction in sub
>> function and I would like to hide the sub function, just like if I
>> declare them
>> in the big function :
>>
>> --------------
>> a <- function(x){
>> b <- function(y){y^2}
>> d <- function(y){y^3}
>> b(x)+d(x)+2
>> }
>> a(2)
>> # [1] 14
>> b(2)
>> # Error :
>> ----------------
>>
>> I would like the same, but with external declaration (for readability) :
>>
>> ----------------
>> b <- function(y){y^2}
>> d <- function(y){y^3}
>> a <- function(x){
>> b(x)+d(x)+2
>> }
>> a(2)
>> # [1] 14
>> b(2)
>> # Error
>> ----------------
>>
>> Is it possible ?
>
> Yes, as long as you're using a package with a NAMESPACE, just don't 
> export b and d.  There are other ways too, but they don't improve 
> readability.
>
> Duncan Murdoch
>

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 23 Feb 2008 - 16:04:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 23 Feb 2008 - 19:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive