In R, I redefined a function, how can I return it?

I did something stupid in session R. I wrote

print = FALSE

Now I can not print things!

print [1] FALSE

How do I get it back?

+4
source share
3 answers

The irony is that you did NOT rewrite it. You created a data object called "print", and when you typed print on the console, the eval-print loop took and returned it. If you correctly tested print behavior by typing print("something") or print(42) , you would see that the interpreter could still find the print.default function in the NAMESPACE database. Defining data objects with the same names as existing functions is bad practice, not because they are overwritten in the R interpreter, but because they are overwritten in the minds of users. The interpreter determines your intention (well, it determines what it will do anyway), knowing if there is an open bracket that denotes a function call. If the letters print are followed by "(", then the interpreter looks at the class of the argument and sends the corresponding print method.

+8
source

rm will not delete the underlying objects, so you can simply run:

 rm(print) 

Interestingly, you can print things:

 > print <- FALSE > print [1] FALSE > print("hi") [1] "hi" > rm(print) > print("hi") [1] "hi" > print function (x, ...) UseMethod("print") <bytecode: 0x2a3a148> <environment: namespace:base> 
+10
source

Take it from the namespace

 print = base::print 
+3
source

All Articles