[R] Prevent Printing Function's Environment

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Aug 14 10:34:40 CEST 2018


Hello,

I am not sure I understand the question.
You say that

One way to prevent this [to print the attributes] is to set the 
function's environment to the global environment.

But this is not true, just see the example below, where I set the 
function's environment to .GlobalEnv


myfunction2 = function (){
   f = function () NULL
   attributes (f) = list (class="myfunction2", myattribute2=1)
   environment(f) <- .GlobalEnv
   f
}

myfunction2.f = myfunction2 ()

myfunction2.f
#function ()
#  NULL
#attr(,"class")
#[1] "myfunction2"
#attr(,"myattribute2")
#[1] 1

environment(myfunction2.f)
#<environment: R_GlobalEnv>


When you run the function's name, the function's definition is printed, 
all of it. If you want to print its return value you have to call it 
with the parenthesis:

myfunction2.f()    # My function
#NULL

myfunction.f()     # Your function
#NULL


If you want to print the body of the function, use, well, body().

body(myfunction.f)
#NULL


Hope this helps,

Rui Barradas
Às 04:16 de 14/08/2018, Abs Spurdle escreveu:
> Hi All
> 
> When you print a function constructed within a function, R prints it's
> environment.
> For example:
> 
>> myfunction = function ()
> + {   f = function () NULL
> +     attributes (f) = list (class="myfunction", myattribute=1)
> +     f
> + }
> 
>> myfunction.f = myfunction ()
> 
>> myfunction.f
> function ()
> NULL
> <environment: 0x03fcbc30>
> attr(,"class")
> [1] "myfunction"
> attr(,"myattribute")
> [1] 1
> 
> One way to prevent this is to set the function's environment to the global
> environment.
> But I was wondering if there's a way to stop R from printing the
> environment without changing the environment?
> 
> 
> kind regards
> Abs
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>




More information about the R-help mailing list