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.

R Console

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

  1. In R, press the “Ctrl” + “L” keys simultaneously.
  2. 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:

  1. 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()
  2. 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()
  3. 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:

  1. In R, press the “Option” + “Command” + “L” keys to simultaneously.
  2. 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.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.