How to enter and run R code

While you can enter and run instructions at the R prompt, one line at a time, pretty soon this isn’t a viable option. For example, when you create functions it is often best to write the functions over multiple lines (functions instructions are enclosed between {}). Thus R comes with the ability to handle scripts. Scripts can contain hundreds of lines of commands. You then highlight the lines of code you want to run and submit them at once. The RGUI allows you to create script documents:

File → New document

and you submit code to run by placing the cursor on the code line and entering keyboard combination Ctrl+R; on macOS, replace Ctrl with Command key and return (enter) for the R key). Figure 1 shows a screenshot of R with a new script file (Fig. 1).

Work with the script window in R Commander

But for us, it’s easier to take advantage of the Rcmdr script window. Once you graduate from using R Commander you’ll most likely migrate to use of RStudio, which has a more powerful collection of tools to work with R, including a great script editor. While you are learning R, R Commander is a

Thus, if you have not already done so, start R Commander (again, type at the R prompt)

library(Rcmdr)

Figure 1. Screenshot of R with a new script file.

R Commander windows

We outlined to R Commander environment in Part 02. Getting started with R and Rcmdr

For review, R Commander provides access to many statistical commands via drop down menus or via the script window. Rcmdr is organized into four parts:

Figure 2. Screenshot of R Commander graphical user interface.

Use menus to select Data to import and manage datasets, Statistics to Summarize (descriptive statistics), make inferences, or model data, Graphs provide access to several graph types, plus additional R commands. The KMggplot2 plugin is shown installed and available in Figure 2.

The Script window can be used to enter commands directly (highlight one or more lines of code, press Submit button to run the code). R Commander will also print out the function calls made via Menus; this is a good way to learn how to code — R Commander generates the code based on menu choices, you then can go back and make changes to the code, adding color options, font size, etc., to refine your work.

Enter your code, line by line. You run a single line of code by placing your cursor somewhere on the line, then click the Submit button. Alternatively, use keyboard shortcuts: on WinPC10/11 Ctrl+R; on macOS Cmd+Enter. If your code wraps across several lines, select all of the continuous code, then click on the Submit button to run the code.

Results from R commands are printed to the Output window. Graphs will popup as new windows. You may have to move around to find graph windows that may get hidden because we have several windows open on our desktops.

Error messages show up in red in Messages window. Error generates error reports when we have mistakes in our commands. Messages can start piling up; we can reset or clear the messages by clicking in the Message window, then selecting Rcmdr: Edit → Clear window.