How to Clear Console in R and RStudio
Everyone loves a clean working environment and it helps to increase productivity. This works the most especially for programmers and those that make regular use of the R or Rstudio consoles on their computers. However, a lot of lines and commands in the console can sometimes hinder productivity which is why we have assembled this guide to help you clear the console in R and RStudio.
Clear Console in R and RStudio
The process of clearing the R console is different for different operating systems and versions which is why we have compiled a solution for all of them. Follow the solution which best suits you to clear the console in R.
For Windows Users
If you are using R on Windows, your work is cut out for you. Clearing the console is very easy and can be done in just a few steps. There are two methods to clear the console in Windows and both of them are listed below.
1. Through Button Combination
- In R, press the “Ctrl” + “L” keys simultaneously.
- The screen will now be refreshed and the console should be cleared.
2. Through Function
You can also install a function to help clear the console for you. In order to do that:
- Use the following function to clear the console in R.
cls <- function() { require(rcom) wsh <- comCreateObject("Wscript.Shell") comInvoke(wsh, "SendKeys", "\014") invisible(wsh) } cls()
- If you are using “R Gui” you can use the following function as well.
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()
- Your console will be cleared once you call these functions.
For Mac Users
Mac users can use both scripts and the key combinations to clear the console. We have listed both of them to assist them in this task.
1. Through Key Combination
It is very easy to get around this problem by using a key combination to clear your console. In order to do that:
- In R, press the “Option” + “Command” + “L” keys to simultaneously.
- This will clear the console and allow you to work more fluently.
2. Through Script
A Git hub user developed a script to allow you to easily get around this problem. You can download the script from here and follow the instructions that come with it to easily fix this issue on your console.