keepcalculator

06 Numbers and Powers

How to Clear Console in R and RStudio

Card No. 06·49672 · Updated · Standard formula, shown below

Clearing the console in R and RStudio removes previous output and error messages, giving you a clean workspace to focus on your current task. It does not delete any variables, data, or scripts—only the text displayed in the console pane. This guide covers the quickest methods for both RStudio and base R, plus tips for keeping your console tidy.

What You’ll Need

  • R or RStudio installed on your computer (any recent version).
  • An open R session or RStudio project.

Step-by-Step

In RStudio (Recommended)

  1. Use the keyboard shortcut: Press Ctrl + L on Windows/Linux or Cmd + L on macOS. This instantly clears the console.
  2. Use the menu: Click on Session in the top menu bar, then select Clear Console. (In older versions, the option is under Edit > Clear Console.)
  3. Use the console toolbar: Look for a small broom icon (🧹) at the top-right of the console pane. Click it to clear the console.
  4. Type a command: In the console, type cat("\014") and press Enter. This sends the form-feed character, which clears the console in RStudio.

In Base R (Terminal or Command Line)

  1. Use the keyboard shortcut: In the R terminal, press Ctrl + L on Windows/Linux or Cmd + L on macOS. This works in most terminal emulators.
  2. Use the shell command: Type shell("cls") on Windows or shell("clear") on macOS/Linux. This runs the system’s clear command. Note: This may not work in all environments.
  3. Use the system function: Type system("clear") on macOS/Linux or system("cls") on Windows. This is similar to the shell method and may be more reliable.

Common Mistakes

  • Confusing clearing with removing objects: Clearing the console does not remove variables from memory. To remove all objects, use rm(list = ls()).
  • Using rm() to clear the console: rm() deletes objects, not console text. It will not clear the console.
  • Expecting cat("\014") to work in base R: In a plain R terminal, cat("\014") may just print a strange character instead of clearing. It is designed for RStudio.
  • Using Ctrl + L in the source editor: In RStudio, Ctrl + L clears the console only when the console pane has focus. If you’re typing in the source editor, it will not clear the console.

Tips

  • Use the broom icon: The broom icon in RStudio is the most visual and foolproof method—no need to remember shortcuts.
  • Create a custom shortcut: In RStudio, you can go to Tools > Modify Keyboard Shortcuts and assign a new key combination to Clear Console if you prefer something else.
  • Use Ctrl + Shift + L to clear and scroll to top: In some RStudio versions, this clears the console and moves the cursor to the top. Check your version’s shortcuts.
  • Clear the console frequently: A clean console makes it easier to spot errors and results. Make it a habit after each major task.
  • Use Ctrl + L in RStudio even when the console is not focused: If you click on the console first, the shortcut works. If you’re in the source editor, click the console once, then press the shortcut.

Troubleshooting

Why doesn’t Ctrl + L work in RStudio?

Make sure the console pane is active (click on it) before pressing the shortcut. If you’re in the source editor, the shortcut may be intercepted by the editor. Also, check if you have any custom shortcuts that override the default.

Why does shell("clear") not work?

On Windows, the command is cls, not clear. On macOS/Linux, use clear. If you’re in RStudio, the shell command may not affect the RStudio console—use cat("\014") instead.

Why does cat("\014") print a symbol instead of clearing?

This command is specific to RStudio. In a plain R terminal, it may not work. Use Ctrl + L or system("clear") instead.

Why is the console still showing old output after clearing?

If you have a long output, the console may scroll back up. Press Ctrl + L again to ensure it’s fully cleared. Also, check if you have any code that automatically prints output after clearing (e.g., in a script that runs on startup).

Frequently Asked Questions

Does clearing the console remove my variables?

No. Clearing the console only removes the text displayed in the console pane. All variables, data frames, and functions in your environment remain intact.

What is the difference between clearing the console and clearing the environment?

Clearing the console removes output text. Clearing the environment (using rm(list = ls())) removes all objects from memory. They are independent actions.

Can I clear the console automatically at the start of my script?

Yes, you can add cat("\014") at the beginning of your script in RStudio. However, this only works when the script is run in RStudio, not in a plain R terminal.

Why does my console not clear when I press Ctrl + L in the terminal?

In a plain R terminal, Ctrl + L may not be mapped to clear. Try using system("clear") or shell("clear") depending on your OS.

Is there a way to clear the console and the environment at the same time?

You can run cat("\014"); rm(list = ls()) in RStudio to clear the console and remove all objects. Be careful—this will delete all your data.

Does clearing the console improve performance?

No, clearing the console does not affect R’s performance. It only improves readability. If you have a very large output, clearing it may free up some memory, but it’s negligible.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →