Clear R Console programmatically
11,075
Solution 1
I use a function for doing that, and actually have put it in {R directory}\etc\Rprofile.site so that it will always be available for use.
cls <- function() {
require(rcom)
wsh <- comCreateObject("Wscript.Shell")
comInvoke(wsh, "SendKeys", "\014")
invisible(wsh)
}
cls()
To clear the console give
cls()
P.S. The function doesn't work the first time it's called and that's why I invoke the function immediately after declaring it in Rprofile.site. As I recall, you may be asked to install some program, in order for this to work.
Solution 2
Create this function:
cls <- function() cat(rep("\n",100))
Then call it:
cls()
Works with:
- Windows
- Linux
- Mac
Author by
Brani
Updated on July 19, 2022Comments
-
Brani almost 2 years
Possible Duplicate:
Function to Clear the Console in RIs there a way to invoke the Clear Console (Ctrl+L) menu command programmatically?
-
wkmor1 almost 14 yearsThe Mac OSX R.app equivalent is <Cmd><Opt><L>.
-
-
siddharthsn over 10 yearsThis doesn't actually clear the console. Just prints 100 new lines.
-
Contango over 10 years@Siddharth. You're right. When I use this cls(), the intent is to get rid of the previous commands, this pushes them away off the top of the screen.
-
Antony almost 8 yearsAs RCom has been discontinued, I thought it might be worth porting this to PowerShell (available on most Windows systems these days)
cls <- function() { system("powershell -ExecutionPolicy Bypass -command (New-Object -ComObject Wscript.Shell).SendKeys([string][char]12)"); }