If you’re a programmer, then chances are you’ve heard about R and RStudio. In fact, we have spoken about the R programming language before, so it makes sense for us to revisit it. Now, the idea behind this article is to explain how to clear the console in both R and RStudio. Not everyone who uses this language knows how to complete this task, which is why we have decided to bring this article to the forefront.
Why you may need to close the RStudio console?
Suppose you’re the type of programmer who likes a clean environment when writing code; clearing the console would make a lot of sense. You see, a cluttered console can hinder productivity, and no one wants that, especially if you have deadlines to meet.
How to clear the R and RStudio console
Here’s the thing, clearing the console in both R and RStudio is very easy and straightforward to get done, as it should be. Now, bear in mind that the process of clearing the console is not the same for all operating systems, which is expected.
Clear the console for Windows 11/10 users
The first thing we are going to do here is to look at how to clear the console for Windows 11/10. Now, there are two ways to get this done, so let us take a look at that.
- Via button combinations
- Using a Function
1] Via button combinations
First, press the CTRL key along with the L key, and right away, the screen will refresh and then show the empty console, suggesting it has been cleared.
Alternatively, click on Edit > Clear Console, and that should get the job done just as well.
2] Using a Function
Another option you could take is to use a Function to help clear your console. To do this, please use the following in R to get it done:
cls <- function() { require(rcom) wsh <- comCreateObject("Wscript.Shell") comInvoke(wsh, "SendKeys", "\014") invisible(wsh) } cls() If you’re using RStudio, then we suggest using the following function: cls <- function() { if (.Platform$GUI[1] != "Rgui") return(invisible(FALSE)) if (!require(rcom, quietly = TRUE)) # Not shown any way! stop("Package rcom is required for 'cls()'") wsh <- comCreateObject("Wscript.Shell") if (is.null(wsh)) { return(invisible(FALSE)) } else { comInvoke(wsh, "SendKeys", "\014") return(invisible(TRUE)) } } cls()
How to clear the R console on a Mac
For those using an Apple Mac device, you should continue reading because things are a bit different compared to Windows.
- Via key combinations: From within R, please press Options + Command + L, and that should be it for clearing the console.
- Use a script: A while back, a user on GitHub created a script designed to clear the console in R. Go on ahead and download the script from the official page, then follow the instructions.
Read: What is the R programming language and why you should use RStudio?