This chapter covers the R prompt and the R Studio windows. It starts with descriptions of the three parts of R: objects, operators, and assignments. It continues with a discussion of working with the R prompt, followed by an example of doing a calculation at the R prompt. Afterward, it describes the four R Studio windows.

In Windows and macOS, R runs in GUIs: RGUI in Windows and R.app GUI in macOS. Both RGUI and R.app GUI open an R Console and run from the R prompt in the R Console. GUIs are available in Linux, but this book only covers running R from the terminal window R prompt. R in R Studio, for the three operating systems, is covered.

The Three Parts of R: Objects, Operators, and Assignments

There are basically three parts of R: objects, operators, and assignments.

  • Objects contain information and can be, among other things, data, functions, or the results of functions. Objects always have a name. Users create some objects, which are automatically saved on creation. Other objects are constants, functions, and datasets contained in the packages of R.

  • Operators manipulate objects, numbers, strings, and/or logical variables. For example, entering a = 2*b at the R prompt would multiply b by two and assign the result to a. The objects a and b are numeric objects, and * is the multiplication operator. The equal sign makes an assignment of two times b to a. Operators are a type of function.

  • Assignments assign an expression to an object. Expressions can consist of objects, numbers, logical variables, strings, lists, other expressions, and/or functions, which are operated on by operators.

Expressions can be evaluated from the R prompt, instead of being evaluated and assigned to an object. (The other places where assignments and operations occur are within functions and within flow control.)

The R Prompt

All of R flows from the R prompt. R is essentially the running of functions and the doing of calculations. Functions and calculations can be run at the R prompt with or without an assignment to an object. Functions and calculations can also be run as part of another function, but everything starts at the R prompt.

Using R from the R prompt may seem daunting at first. R opens with some writing, and then a lonely little greater-than sign (>), which is the R prompt. The opening writing gives the R version number and some other information about the program, including the fact that the program runs with no warranty.

R remembers every line that is entered into the program, up to a set number of lines. A very handy side of R is that the up and down arrows on the keyboard will step through the lines. You only need to enter an expression once. Corrections to expressions are easy to do without typing the entire expression again.

To close R, enter q() at the R prompt or, for Windows and macOS, close the window. R will close with the option to save the workspace. In Linux, if the terminal window is closed without using q(), the current workspace will be lost.

The workspace consists of any objects present in R at the time the program is closed and the current history. Closing R without saving the workspace will result in reverting to the workspace present at the time the R session started.

An Example of a Calculation

The simplest use of R is as a calculator. The following calculation was done from the R prompt. There is no assignment in the calculation, so the result is returned on the screen.

> (1 + 3 + 7)/5 [1] 2.2 >

The first line gives the expression to be evaluated and the second line gives the result. The [1] in the second line is a label that tells the user that the result is the first value returned from the expression. Many expressions return more than one value. At the third line, the R prompt comes back and R is ready for another task.

The Four R Studio Windows

On opening the program for the first time, R Studio presents you three sub-windows. On the left side of the main R Studio window is a smaller single window. On the right are two vertically aligned smaller windows. Across the top, above the three windows, are two menus that provide several options for working with R Studio. Both menus extend the full width of the main window.

In Windows and Linux, the upper menu is the main R Studio window, while in macOS, the top menu is on the main macOS menu bar. In macOS, if R Studio is expanded to the full screen, you must hover over the top of the page to see the upper menu. One nice thing about R Studio is that, when you type, it provides autocompletion for object names. But, R Studio can be slow to load.

The upper menu has the buttons “File”, “Edit”, “Code”, “View”, “Plots”, “Session”, “Build”, “Debug”, “Profile”, “Tools”, and “Help”. Each button opens a dropdown menu. The dropdown menus are self-explanatory.

The lower menu contains icons for (from left to right): opening new things (an R Script, an R Notebook, an R Markdown document, a Shiny Web App, a text file, an C++ file, an R Sweave document, an R HTML document, an R presentation, or an R documentation file), opening a new project, opening a file on the computer, saving the contents of the Source window (see the section on the fourth window), saving the contents under all of the Source window tabs, printing the contents of the active sub-window, searching for and opening files in the working directory (it searches for the letters from left to right and does not appear to work on macOS), adjusting the look and positions of the sub-windows, and adding add-ins.

The First Sub-window

The sub-window on the left opens to the standard R console, under a tab labeled “console.” Commands are entered at the R prompt in the same way as in R. To the right of the console tab is a tab labeled “terminal,” which gives access to the terminal of the computer.

The Second Sub-window

The upper window on the right contains the tabs “Environment”, “History”, and “Connections”. Under the “Environment” tab, R Studio lists the objects in the workspace, classified as “Data”, “Values”, and “Functions”. “Data” contains the data frame and matrix objects (to be defined later). “Values” contains objects that are not data frames, matrices, or user-defined functions. “Functions” contains user-defined functions. Various properties of the objects are given in the window, such as the type of the object and the number of dimensions of the object.

Under the “History” tab are the lines of code that have been entered at the console. Only a set number of lines are retained. The code can be highlighted and moved to the console. A search function is available to search the history.

Under the “Connections” tab is a list of connections. Initially, there are none. Connections are links to files, URLs, pipes, sockets, or other types of data outside R. Connections provide for the interactive reading of outside data and are used in data mining. Clicking on the “new connections” button under the “Connections” tab gives a list of the possible connections that R Studio sees as available. (Connections can also be opened from the R prompt.)

The Third Sub-window

The lower right window has the tabs “Files”, “Plots”, “Packages”, “Help”, and “Viewer”. Under the Files tab is a list of the files and folders in the working directory of the computer. Options exist to add a folder, delete a file, rename a file, copy or move a file, and to got to or set a working a directory.

Under the Plots tab are any plots that have been created. You can use the left and right arrow icons to move through the plots. Plots can be exported to image or pdf files.

The Packages tab gives a list of installed packages. Clicking on the Install link opens a search box to find packages to install. Entering characters into the search box brings up all packages beginning with the characters, making it easy to find the package to be installed. Clicking on the Update link gives a list of those installed packages with updates and offers a choice to update them. Checking the box to the left of a package in the list of packages opens the library in the console window, and unchecking the box detaches the library.

The Help tab provides a link to the help pages. Entering characters into the search box on the right side of the window menu bar brings up available functions that begin with those characters. The search is case sensitive. Only those functions whose libraries have been loaded are available. There is a search box for searching within the help page on the line below.

The Viewer tab is for viewing content on the local web. The R Studio website has helpful information on using Viewer.

The Fourth Sub-window

The fourth window is the “Source” window. The Source window contains source code or data sets and, when open, is on the upper left side of the main window. The Source window opens when a data object or function is clicked in the Environment window. The data object or function appears in the Source window for inspection but is not editable. The Source window also opens if you open an external file for editing and(or) running, or a blank page for new code.

If a source file (usually with an R extension) is in the working directory, the file can be loaded into the Source window. A file with an R extension is called a script and, when loaded, is editable and runable. A text file with no R extension is not runnable but is editable and can be saved. Also, new code can be entered into a blank window opened from the lower main menu.

Several pages can be opened in the Source window, but only one is displayed at a given time. Each open page in the window has a tab above the window containing the name of the source file or a label “Untitledn”, where “n” is a number indicating which untitled object is under the tab. The source files are displayed by clicking on the tab and closed by clicking on the small “x” n the right side of the tab.

The menu above the sub-window is part of the tab for the object in the window. If the object is a script, the menu offers the following options (from left to right): arrows to go back to the previous tab or forward to the next one, an icon to open the window in a stand-alone window, an icon to save the current object as an R script, a checkbox to source (run) the script when saving, an icon to use specialized editing tools, an icon to run highlighted text, an icon to rerun text, and an icon to source (run) the entire contents of the window.

If the object is a text file, the tab menu contains (from left to right) the following: arrows to move among the tabs, an icon to open the text in a new window, an icon to save the document to the working directory, a spellchecker icon, and a find and replace icon.

If the object is a dataset, the menu contains (from left to right): arrows to move through the tabs, an icon to open the window in a new standalone window, a filter option to filter the columns by range values, and, on the far right, a search box to search the data.

If the object is a function, the menu contains (from left to right) the following: arrows to move through the tabs, an icon to open the window in a standalone window, an icon to print the function, a search function, an icon for code tools or an icon to open help functions for highlighted text, and, on the right side of the menu, run and re-run icons for running all or a selected part of the function.