Using the R Console
There are a few ways of interacting with R in a workspace: using the built-in console, running script files or the Built-in RStudio App. Using the R console, you can type in commands directly, but they will be forgotten once you close the session. Alternatively, you can enter R commands in a script and save it in the workspace Files or create a file in the RStudio Built-in App.
The R console can be accessed by clicking on the Tools menu and selecting New R Console.
Before starting the session, you can select the version of R from the sidebar on the right. Once you have started the session, changing the R version in the dropdown will not affect the current session. If you want to use a different R version, you will have to open a brand-new session.
If you had previously closed an R session, you will find it listed in the same dropdown menu. Clicking on an existing session will open the R console with the same environment, meaning it will contain all the functions and variables you had previously defined.
Workspaces currently support R versions 4.1.1, 4.0.2, 3.6.3, 3.5.3
Using the console
Starting a new R session will open the console. You can type in your command and the output will be displayed in the R console. Any visual outputs will appear in the Plots tab on the right-hand side panel. You can click on the plot to enlarge it or save it in the workspace Blobs.
The scripts and files stored in the workspace will appear in the Files tab on the right-hand side panel. If you click on the arrow of an R file, the script will be executed in the console, whereas if you click on the name, the script will open in a text editor in a separate tab. You can modify, save and run the script (or portions of the script) from the new tab.
To terminate an R session, execute
quit() command in the console. R consoles automatically time out after 30 minutes of inactivity.
Creating and running R scripts
Another way to interact with R is by saving your R code in a script and then running it in the console.
Creating a new R script
You can create a new R file by navigating to the Files menu and clicking the 'New File' command. This will open a new window, where you need to enter the file name and select the type of file you want to create (R file in this case). Once the new file is created, a text editor tab will open, where you can enter your R code.
Running an R script
An R script can be run by pressing the ‘Run file’ button at the top of the text editor. You also have the option to run individual statements by highlighting them and pressing ‘Run selected’.
Executing a script opens the R console, where you can see outputs if there are any. If you haven't previously started an R session, you will be prompted to select the R version that you want your script to run in and start the session.
You can also run your script directly from the console by finding your file in the Files tab on the right-hand side panel and clicking on the arrow next to the file. Please note that you must save your script before running it in the console.
Stopping an R script
If you run an R script or command and notice that it is taking a long time to complete, you can stop it by clicking the ‘Stop’ button at the bottom of the console to terminate its execution.