Data Science

What is the specific function or keyboard shortcut to clear the R and RStudio console?

KI Asked by Kimberly Thompson · 14-05-2024
0 upvotes 12,142 views 0 comments
The question

I am currently working on a long data analysis script in RStudio and my console is getting extremely cluttered with previous outputs and error messages. Is there a built-in R function I can call within my script to refresh the view, or is there a specific keyboard shortcut for Windows and Mac that clears everything instantly without restarting the entire session?

3 answers

0
PA
Answered on 18-08-2024

In RStudio, the most efficient way to clear the console is the keyboard shortcut Ctrl + L for both Windows and Mac. However, if you want to clear the console programmatically from within a script, R does not have a single "clear" function like other languages. You can simulate this by using cat("\014"), which sends a form-feed character to the console, effectively pushing the previous text out of view. In my experience, this is particularly useful at the beginning of a markdown-style script to ensure that the output you see during execution is fresh and not confused with old logs from a previous run.

0
ST
Answered on 02-09-2024

Does using cat("\014") also clear the environment variables and the command history, or does it only hide the visible text in the console window?

JA 04-09-2024

Steven, that is a very important distinction to make. The cat("\014") function and the Ctrl + L shortcut only clear the visual display of the console. They do not touch your Global Environment or your history. If you want to wipe your variables as well, you would need to use rm(list = ls()). For a truly fresh start, I usually recommend using the "Restart R" option found in the Session menu of RStudio, which clears the memory and the console simultaneously, ensuring no "ghost variables" interfere with your new analysis.

0
BA
Answered on 12-10-2024

If you want a custom command, you can define a function like cls <- function() cat("\014"). Then, simply typing cls() in the console will clear it anytime.

KI 15-10-2024

I agree with Barbara. Creating a custom cls() function is a great way to make R feel more like a standard terminal. It’s one of the first things I add to my .Rprofile so that it’s available every time I open RStudio for a new project. It definitely helps maintain a clean mental workspace during complex debugging.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session