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.ffunction () 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]]
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 at 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.
>
>>>>> Abs Spurdle >>>>> on Tue, 14 Aug 2018 15:16:08 +1200 writes:> 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? Probably, not the way you want, but if you need it e.g., for didactical reasons, here's a way that may be didactically relevant in it self: > ls.str function (pos = -1, name, envir, all.names = FALSE, pattern, mode = "any") { if (missing(envir)) envir <- as.environment(pos) nms <- ls(name, envir = envir, all.names = all.names, pattern = pattern) r <- unlist(lapply(nms, function(n) exists(n, envir = envir, mode = mode, inherits = FALSE))) structure(nms[r], envir = envir, mode = mode, class = "ls_str") } <bytecode: 0xb222858> <environment: namespace:utils> so, I want to suppress the last two lines that are printed. That's a piece of cake if you know capture.output() : > P2 <- function(.) writeLines(head(capture.output(.), -2)) > P2(ls.str) function (pos = -1, name, envir, all.names = FALSE, pattern, mode = "any") { if (missing(envir)) envir <- as.environment(pos) nms <- ls(name, envir = envir, all.names = all.names, pattern = pattern) r <- unlist(lapply(nms, function(n) exists(n, envir = envir, mode = mode, inherits = FALSE))) structure(nms[r], envir = envir, mode = mode, class = "ls_str") } >