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
Share:
11,075
Brani
Author by

Brani

Updated on July 19, 2022

Comments

  • Brani
    Brani almost 2 years

    Possible Duplicate:
    Function to Clear the Console in R

    Is there a way to invoke the Clear Console (Ctrl+L) menu command programmatically?

    • wkmor1
      wkmor1 almost 14 years
      The Mac OSX R.app equivalent is <Cmd><Opt><L>.
  • siddharthsn
    siddharthsn over 10 years
    This doesn't actually clear the console. Just prints 100 new lines.
  • Contango
    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
    Antony almost 8 years
    As 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)"); }