Search code examples
renvironmentpretty-print

How do I suppress the environment line when printing an R function?


How can I suppress the environment/namespace line when I print a function from a package? I've tried reassigning the function to the global environment. I have tried entering into the foobar environment. I've tried gimmicks and games, even with R to make this work. It seems like there should be a simple way to do it.

> x2
function(x) {
    return(x * x)
}
<environment: namespace:foobar>
>

Solution

  • Well, if you want to change how functions are printed, create your own print.function. I think this should work for you

    print.function <- function(x,...) {
        environment(x)<-globalenv(); 
        base::print.function(x,...)
    }
    

    For example

    x2<-function(x) {
        return(x * x)
    }
    environment(x2)<-asNamespace("stats")
    
    base::print.function(x2)
    # function(x) {
    #     return(x * x)
    # }
    # <environment: namespace:stats>
    
    x2
    # function(x) {
    #     return(x * x)
    # }