Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R I overrode a function, how do I get it back?

Tags:

r

I did something silly in an R session. I wrote

print = FALSE

Now I can't print things!

print [1] FALSE

How do I get it back?

like image 955
Colonel Panic Avatar asked Nov 30 '22 14:11

Colonel Panic


2 Answers

rm will not remove base objects, so you can just 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>
like image 192
Ari B. Friedman Avatar answered Dec 15 '22 11:12

Ari B. Friedman


The irony here is that you did NOT overwrite it. You created a data object named "print" and when you typed print at the console, the eval-print loop took over and returned it. Had you tested for the behavior of print correctly by typing print("something") or print(42) you would have seen that the interpreter was still able to find the print.default function in the base NAMESPACE. Defining data objects with the same names as existing functions is bad practice, not because they overwrite in the R interpreter, but because they overwrite in minds of users. The interpreter determines your intent (well, it determines what it will do anyway) by seeing whether there is a open-parenthesis which signifies a function call. If the letters p-r-i-n-t are followed by "(" then the interpreter looks at the class of the argument and dispatches the appropriate print method.

like image 43
IRTFM Avatar answered Dec 15 '22 12:12

IRTFM