Function to clear the console in R and RStudio

R

R Problem Overview


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.

R Solutions


Solution 1 - R

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.

Solution 2 - R

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

Solution 3 - R

You may define the following function

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

which you can then call as clc().

Solution 4 - R

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.

Solution 5 - R

shell("cls") if on Windows,

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

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

Solution 6 - R

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

It works fine for me on Windows 10.

Solution 7 - R

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

Solution 8 - R

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

Solution 9 - R

If you are using the default R console CTRL + L

RStudio - CTRL + L

Solution 10 - R

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

Solution 11 - R

You can combine the following two commands

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

Solution 12 - R

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

Solution 13 - R

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerk U.View Question on Stackoverflow
Solution 1 - RE Luxo SoView Answer on Stackoverflow
Solution 2 - RRindraView Answer on Stackoverflow
Solution 3 - RCarl WitthoftView Answer on Stackoverflow
Solution 4 - Rkmario23View Answer on Stackoverflow
Solution 5 - RRyan BlanchardView Answer on Stackoverflow
Solution 6 - RmyfirstAnswerView Answer on Stackoverflow
Solution 7 - Rjvd10View Answer on Stackoverflow
Solution 8 - RpapplegateView Answer on Stackoverflow
Solution 9 - RRakeshView Answer on Stackoverflow
Solution 10 - RPorcupineView Answer on Stackoverflow
Solution 11 - RLéo Léopold Hertz 준영View Answer on Stackoverflow
Solution 12 - RSATYAJIT MAITRAView Answer on Stackoverflow
Solution 13 - RPaulView Answer on Stackoverflow