RStudio is an opensource Integrated Development Environment (IDE) that provides a user-friendly environment for the user to interact with the R language more rapidly. It is very similar to the RGuibut it has more dropdown menus, windows, and multiple tabs which makes it very easy and interesting to use. Working on the Rstudio console is very productive because of the variety of features offered by it but a lot of people ask how they can clear console in R.
You can see three different windows as soon as you launch RStudio. The fourth window is not shown as default but you can open it by clicking File-> New File-> R Script. These windows are names as:
- Workspace and History.
- Files, plots, packages, and help.
- The R script(s) and data view.
As soon as you launch RStudio, you see some information on the console. This information is given in the beginning to instruct the user if he needs help. Also, after running many commands, these commands will remain in the console. Sometimes, the user may not like previous commands appearing in the console window so, in this article, we are going to learn two different methods to clear the workspace in R.
Methods to Clear Environment in R
Method 1: Clear R Console using Shortcuts
The shortest and the quickest way to clear the global environment in R is by using shortcut keys from the keyboards. Simply hit Ctrl+L on the keyboard and you will see that everything written in the console will be erased and the console will be cleared. This works with all Windows, Linux, and Mac.
Method 2: Define a Function to Clear Environment in R
There is another method to clear the screen. You can remove all objects in R by writing a function in the code.
Both of these functions are used to send Ctrl+L to the console which helps to clear the console.
You can also use the following command to clear variables in R.
rm(list = ls())
Method 3: Customize a function to Erase R Console
If you want to define your own function to clear the console, you can easily do that. Just define it as follows and call it by typing clc().
clc <- function() cat(rep("n", 50))