373

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.

3
  • It's not an exact duplicate but it's pretty highly related and the answer to this question was given in the following - stackoverflow.com/questions/8421005/… Commented Jan 11, 2013 at 15:56
  • Only type clc with this script clc.R 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 contains more details. Commented Feb 19, 2019 at 15:49
  • I found this command system("clear") similar to clear in bash which clean up everything even scrolling up. Commented Nov 30, 2023 at 7:36

14 Answers 14

630
Answer recommended by R Language Collective
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.

Sign up to request clarification or add additional context in comments.

11 Comments

This just prints a single blank line to my interactive terminal (on both Ubuntu and Mac OSX)
Works for me. This might also help with the extremely slow console-bug in Rstudio.
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.)
does not work for me. windows 7, R console. (r.exe).
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").
|
108

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

3 Comments

CTRL-L just in case you thought that was an "i", like me :)
Ctrl + L for Rstudio on OSX Yosemite as well.
This does not answer the question. The OP wanted a function and clearly said not a keyboard shortcut.
39

shell("cls") if on Windows,

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

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

6 Comments

This worked. It is not as elegant as the other answers, but far easier to remember.
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...
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());
This works for the terminal in Visual Studio Code on Windows.
shell("cls") is the only one that works for me if i use it in code in a snippet
|
37

You may define the following function

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

which you can then call as clc().

1 Comment

This does not clear the console rather it inserts 50 empty lines which push the output up.
32

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

It works fine for me on Windows 10.

Comments

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.

Comments

18

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())

3 Comments

Much like the \014 approach, this approach gives me funny characters (from the R console on a Windows 10 machine).
Unlike \014 (or \f) this worked for me on Windows 10 in Rterm.
@MartiniBianco: I think that the answers here should help. Basically "\033" is "ESC". The "[2J" and "[0;0H" clear the screen and move the cursor to upper left, respectively.
17

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).

Comments

10

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

Comments

8

If you are using the default R console CTRL + L

RStudio - CTRL + L

2 Comments

Works on macOS too
The question asks for a function and specifically says does not want a keyboard shortcut!
5

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

Comments

4

You can combine the following two commands

cat("\014"); 
cat(rep("\n", 50))

Comments

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()

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.