RStudio IDE

The first tool you get to write and execute the R code in is the R Console. The console presents the greater than (>) sign as the prompt to the user. In the console, you write commands line by line, and execute them by pressing the Enter key. You have some limited editing capabilities in the console. For example, you can use the up and down arrow keys to retrieve the previous or the next command in the buffer. The following screenshot shows the console, with the demo() command executed, which opens an Explorer window with a list of demo packages.

Because R is a functional package, you close the R Console with the q() function call. Anyway, you probably want to use a nicer, graphical environment. Therefore, it is time to introduce the RStudio IDE. The following screenshot shows the R console:

R Console

RStudio is a company that is dedicated to helping the R community with its free and payable products (https://www.rstudio.com/). Their most popular product is the RStudio IDE, or as most R developers used to say, just RStudio. RStudio is available in open source and commercial editions, in both cases for desktop computers or for servers https://www.rstudio.com/products/rstudio/. The open source desktop edition, which is described in this section, is very suitable for starting developing in R. Already this edition has the majority of features built-in for smooth and efficient coding. This edition is described in this section.

You can download the RStudio IDE from the RStudio company site. The IDE supports Windows, macOS, and Linux. Once you install it, you can open it through a desktop shortcut, which points to the C:Program FilesRStudioin studio.exe file, if you used the defaults during the installation.

The RStudio screen is usually split into four panes if you open an R script file, as shown in the following figure. The open script file is showing the R script used for the demos in Chapter 14, Supporting R in SQL Server R, of this book:

RStudio IDE

The bottom left pane is the Console pane. It works similarly to the R Console Command Prompt utility shipped with the R engine. You can write statements and execute them one by one, by pressing the Enter key. However, in RStudio, you have many additional keyboard shortcuts available. One of the most important keyboard shortcuts is the Tab key, which provides you with the code complete option. You can press the Tab key nearly anywhere in the code. For example, if you press it when you are writing function arguments, it gives you the list of the possible arguments, or if you already started to write a name of an object, all objects that start with the letters you have already written.

The top-left pane is the Source pane, the pane that is by default settings used for the R code script; therefore, this is your basic R code editor. Writing R code line by line in a console is simple, but not very efficient for developing a script with thousands of lines. The Source pane does not execute your R code line by line. You highlight portions of your code and you execute it by pressing the Ctrl and Enter keys simultaneously.

The top-right pane is the Environment pane. It shows you the objects in your current environment, the objects currently loaded in memory. However, this pane has more than one function. You may notice additional tabs at the top of the pane. By default, you see the History tab, the tab that leads you to the History pane, where you can see the history of the commands. The history goes beyond the commands in the current session and in the current console or script.

The bottom-right pane is also a multi-purpose pane. It includes the Help pane, Plots pane, Files pane, Packages pane, and Viewer pane by default. You can use the Files tab to check the files you saved in your RStudio account. With the help of the Packages tab you can get a list of all R packages you have access to in the current session. The Help tab brings you, of course, to R documentation and a help system. You can use the Viewer tab to get to the Viewer pane where you can see local web content that you can create with some graphical packages. The Plots pane shows you the plots you created by executing R code either in the Console or in the Script pane.

The following screenshot shows you all the four panes in action. You can see the usage of the Tab key in the Source pane to auto-complete the name of the dataset used in the plot() function. The dataset used is the iris dataset, a very well-known demo dataset in R. You can see the command echoed in the Console pane. The Environment pane shows the details about the iris dataset that is loaded in memory. The Plots pane shows the plots for all of the variables in the demo iris dataset:

RStudio IDE in action

Note that you can zoom the plot and save it in different graphical formats from the Plots pane.

There are literally dozens of keyboard shortcuts. It is impossible to memorize all of them. You memorize them by using them. Nevertheless, you don't need to know all of the shortcuts before you start writing R code. You can always get a quick reference of the keyboard shortcuts by pressing the Alt, Shift, and K keys at the same time. The keyboard Shortcut Quick Reference cheat sheet appears, as shown in the following screenshot. You can get rid of this cheat sheet by pressing the Esc key.

Please note that, although exhaustive, even this cheat sheet is not complete. In the top-right corner of the cheat sheet you can notice a link to even more shortcuts. Finally, it is worth mentioning that you can modify the pre-defined shortcuts and replace them with your own ones.

You have access to many of the keyboard shortcuts actions through the menus at the top of the RStudio IDE window. For example, in the Tools menu, you can find the link to the keyboard shortcuts cheat sheet. In the Help menu, you can find, besides the help options you would expect, the links to various cheat sheets, for example to the complete RStudio IDE cheat sheet, a PDF document you can download from https://www.rstudio.com/wp-content/uploads/2016/01/rstudio-IDE-cheatsheet.pdf at the RStudio site. The following screenshot illustrates the keyboard shortcuts cheat sheet:

RStudio IDE keyboard shortcuts cheat sheet

Using the Panes menu, you can change the default layout of the panes. There are numerous options in the Code menu. For example, you can extract a function from the code. Figure Extracting a function shows an example where the Source pane is enlarged at the right side of the RStudio IDE window to show the path to the Extract Function option in the Code menu. The highlighted code is calculating the third and the fourth population moments for a continuous variable, the skewness and the kurtosis, as known from the descriptive statistics. When you extract a function from the code, you need to provide the name for the function, and RStudio extracts the necessary parameters from the code.

If you extract the function with the name skewkurt and test it using the iris dataset Sepal.Length variable, you get an infinite number for both skewness and kurtosis. Apparently, there is a bug in the code (you can see the bug from the inline comments in the code in the following screenshot). Fortunately, RStudio provides a debugger. You can just click at the left border of the Source pane at the line where you want the execution to stop, meaning to set a breakpoint at that line. Then you highlight the code and start debugging by clicking the Source button at the top right of the Source pane, or by using the keyboard shortcut Ctrl, Shift, and S:

Extracting a function

When you execute the code in the debugging mode, the execution stops at the breakpoint. You can use the buttons in the Console pane to execute the code line by line, step into a function, execute the remainder of a function, and more. You can use the Traceback pane to see the full call stack to the code you are currently executing. In the Environment pane, you can watch the current values of the objects you are using.

The following screenshot shows a debugging session in RStudio IDE. In the Source pane at the right of the window, you can see a breakpoint in line 19, near the cnt <- 0 code. You can see also the line that is just about to execute, in this case line 22, with the code kurt <- sum((p-avg)^4/stdev^4)/cnt-3. In the Console pane at the bottom-left of the screen, you can see the Next button highlighted. This button executes the next line of the code. In the middle-left of the screen you can see the Trackback pane, and at the top left the Environment pane. You can see that the value of the variable cnt is 0, and because this variable is used in the denominator when calculating the skewness, the skew variable has an infinite value:

Debugging in RStudio IDE

After you find the error, you can stop debugging either from the Debug menu or with the Shift and F8 keyboard shortcut. Correct the code and run it to test it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.95