362

I am wondering if there is a function to clear the console in R and, in particular, RStudio I am looking for a function that I can type into the console, and not a keyboard shortcut.

Someone has already provided such a function in this StackExchange post from 2010. Unfortunately, this depends on the RCom package and will not run on Mac OS X.

nbro
  • 15,395
  • 32
  • 113
  • 196
Berk U.
  • 7,018
  • 6
  • 44
  • 69
  • It's not an exact duplicate but it's pretty highly related and the answer to this question was given in the following - http://stackoverflow.com/questions/8421005/r-about-removing-all-of-the-typed-commands-from-the-command-window/8422206#8422206 – Dason Jan 11 '13 at 15:56
  • Only type `clc` with this script [clc.R](https://github.com/HubertRonald/clcR) that I development. How does it work? `clc<-0`; `class(clc) <- 'cleanup'`; `print.cleanup <- function(cleanupObject) cat("\f")`. The last line corresponds to RStudio but in terminal change it by `print.cleanup <- function(cleanupObject) cat(c("\033[2J","\033[H"))`. The [clc.R](https://github.com/HubertRonald/clcR) contains more details. – HubertRonald Feb 19 '19 at 15:49

14 Answers14

611
cat("\014")  

is the code to send CTRL+L to the console, and therefore will clear the screen.

Far better than just sending a whole lot of returns.

nbro
  • 15,395
  • 32
  • 113
  • 196
E Luxo So
  • 6,158
  • 1
  • 13
  • 2
  • 19
    This just prints a single blank line to my interactive terminal (on both Ubuntu and Mac OSX) – Scott Ritchie May 27 '13 at 13:00
  • 2
    Works for me. This might also help with the *extremely slow console*-bug in Rstudio. – Ruben May 30 '13 at 10:45
  • 1
    Works for me in RStudio on Windows Xp – Juancentro Jul 26 '13 at 19:31
  • 13
    Works in RStudio on Mac, but not in R.app! In R.app the command Cmd+Alt+L clears the screen, not Ctrl+L... Is there a similar 'code' to send that key combination to the R.app? (I see that \014 is the ASCII code for Form Feed, so I guess it is not sending the key combination but just the Form Feed command, which makes it unlikely that a 'code' for Cmd+Alt+L would exist.) – Johan Sep 20 '13 at 10:19
  • 5
    does not work for me. windows 7, R console. (r.exe). – Knows Not Much Feb 13 '15 at 22:14
  • Thanks a lot. Worked for me. Windows 8. – Pragyaditya Das Feb 07 '16 at 06:20
  • Doesn't work for me on OSX. See my posted answer for one that does work on osx and linux. – jvd10 Jun 29 '16 at 18:03
  • 12
    This works only in RStudio on Windows, not in the "usual" R console nor in a DOS console. For the record, it's also the Form Feed character, and you can just type `cat("\f")`. –  Jul 27 '16 at 08:36
  • 1
    How can you clear the top margin too? - - You can still browse the history with your mouse, for instance. Can you clear it too? - - OS: Debian 8.5 – Léo Léopold Hertz 준영 Nov 11 '16 at 18:30
  • Works on gnome terminal on debian 8. – byxor Mar 27 '17 at 12:16
  • @E Luxo So, where can I find a list of these codes? In particular, I’m looking for a way to send backspace using an r snippet – Josh Apr 05 '19 at 15:24
107

If you are using the default R console, the key combination Option + Command + L will clear the console.

nbro
  • 15,395
  • 32
  • 113
  • 196
Rindra
  • 1,187
  • 1
  • 7
  • 2
36

You may define the following function

clc <- function() cat(rep("\n", 50))

which you can then call as clc().

nbro
  • 15,395
  • 32
  • 113
  • 196
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
34

shell("cls") if on Windows,

shell("clear") if on Linux or Mac.

(shell() passes a command (or any string) to the host terminal.)

Ryan Blanchard
  • 509
  • 5
  • 11
  • 1
    This worked. It is not as elegant as the other answers, but far easier to remember. – Martini Bianco Apr 19 '18 at 11:46
  • 3
    This is the only command that worked for me in the Rterm console on Windows 10... but what a pain to have to type all that... – Michael Szczepaniak Jun 03 '18 at 21:46
  • Yep, that's why I learned it - I also use the R terminal on Windows 10 (much easier than loading up RStudio) - and it's handy to be able to pass commands to the shell. I believe there is a way in R to sort of "map" a sequence of characters to a particular command, although I can't remember exactly how to do it - I think it's a bit like an alias in bash, or maybe it's more like a function - but you could do that to reduce the amount of typing. (For instance, I think you could set it up where something like `clr()` or `clr` runs the command `shell("cls")` or `shell("clear")`.) – Ryan Blanchard Jun 04 '18 at 07:38
  • 1
    Likewise, this was the only one that worked for both RStudio, and my Windows 7 shell (cmd.exe) windows. To .Rprofile I've added: `clear_fun <- function() shell("cls"); makeActiveBinding("cls", clear_fun, baseenv());` –  Jun 22 '18 at 15:40
  • 1
    This works for the terminal in Visual Studio Code on Windows. – Bilbottom Jan 26 '20 at 16:15
  • 2
    `shell("cls")` is the only one that works for me if i use it in code in a [snippet](https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets?version=1.2.5033&mode=desktop) – Levi Baguley Mar 15 '20 at 00:13
31

cat("\f") may be easier to remember than cat("\014").

It works fine for me on Windows 10.

nbro
  • 15,395
  • 32
  • 113
  • 196
bogec
  • 430
  • 5
  • 7
27

In Ubuntu-Gnome, simply pressing CTRL+L should clear the screen.

This also seems to also work well in Windows 10 and 7 and Mac OS X Sierra.

nbro
  • 15,395
  • 32
  • 113
  • 196
kmario23
  • 57,311
  • 13
  • 161
  • 150
16

Here's a function:

clear <- function() cat(c("\033[2J","\033[0;0H"))

then you can simply call it, as you call any other R function, clear().

If you prefer to simply type clear (instead of having to type clear(), i.e. with the parentheses), then you can do

clear_fun <- function() cat(c("\033[2J","\033[0;0H"));
makeActiveBinding("clear", clear_fun, baseenv())
nbro
  • 15,395
  • 32
  • 113
  • 196
jvd10
  • 1,826
  • 17
  • 17
  • 3
    Much like the `\014` approach, this approach gives me funny characters (from the R console on a Windows 10 machine). – demongolem Dec 29 '16 at 17:49
  • Unlike \014 (or \f) this worked for me on Windows 10 in Rterm. – Martini Bianco Apr 19 '18 at 11:42
  • @MartiniBianco: I think that the answers [here](https://stackoverflow.com/questions/1348563/clearing-output-of-a-terminal-program-linux-c-c) should help. Basically "\033" is "ESC". The "[2J" and "[0;0H" clear the screen and move the cursor to upper left, respectively. – jvd10 Apr 25 '18 at 12:57
15

I developed an R package that will do this, borrowing from the suggestions above. The package is called called mise, as in "mise en place." You can install and run it using

install.packages("mise")
library(mise)
mise()

Note that mise() also deletes all variables and functions and closes all figures by default. To just clear the console, use mise(vars = FALSE, figs = FALSE).

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
papplegate
  • 151
  • 1
  • 3
10

In linux use system("clear") to clear the screen.

Porcupine
  • 5,885
  • 2
  • 19
  • 28
7

If you are using the default R console CTRL + L

RStudio - CTRL + L

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
Rakesh
  • 2,732
  • 29
  • 28
5

cat("\014") . This will work. no worries

4

You can combine the following two commands

cat("\014"); 
cat(rep("\n", 50))
nbro
  • 15,395
  • 32
  • 113
  • 196
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
1

Another option for RStudio is rstudioapi::sendToConsole("\014"). This will work even if output is diverted.

sink("out.txt")

cat("\014") # Console not cleared

rstudioapi::sendToConsole("\014") # Console cleared

sink()
Paul
  • 8,734
  • 1
  • 26
  • 36
0

I know this question is very old, but I found myself visiting it many times looking for a totally different answer:

n = 20
for (i in 0:n) {
  cat(100*i/n, "% \r")
  flush.console()
  Sys.sleep(0.01) #do something slow
}

flush.console() will kind of "clear the console in r and studio", maybe not in OP's terms but still.

This code will act like a progress bar in the console. For each iteration, the percentage is incremented and then erased on the next iteration.

Note that this won't work without \r or with an \n, for some reason.

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92