Exit R terminal by typing `exit`?

176 views Asked by At

Almost every tool I use can be exited from terminal by typing exit. But R, being "unique" requires I type q().

Can I set-up something in .Rprofile to achieve this?

2

There are 2 answers

0
Mikael Jagan On

R has active bindings for this exact purpose:

makeActiveBinding("exit", function() q(), .GlobalEnv)

Now getting exit from the global environment triggers a call to q. Hence any of the following statements will end the R process:

exit
get("exit")
exit + 1
typeof(exit)
(function() exit)()

But this one will not:

(function(exit) exit)(0)

rm(exit) deletes the binding.

You'll find more details, including other ways to interact with bindings (locking, accessing, assigning to, etc.), in ?makeActiveBinding.

0
jessexknight On

Yes, you can add to your .Rprofile:

exit <- list()
class(exit) <- 'exiter'
print.exiter <- function(exiterObject){ q() }

which creates an object exit, assigns it the class exiter, and then overrides the print function to actually call q() by only typing exit (which would normally print the object).

Thanks to this blog for the hack.