Tools for developing R code

As you probably already know, SQL Server 2016 brings support for the R language. Of course, you need to have a development tool for the R code. There is a free version of the integrated development environment (IDE) tool RStudio IDE that has been available for quite a long time. This is probably the most popular R tool. In addition, Microsoft developed R Tools for Visual Studio (RTVS), a plug-in for Visual Studio 2015, which enables you to develop R code in an IDE and is well-known among developers who use Microsoft products and languages.

In this section, you will learn about:

  • RStudio IDE
  • R Tools for Visual Studio

RStudio IDE

The first tool you get for writing and executing R code is the R Console. The console uses the greater than (>) sign as a prompt for 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 figure shows the console, with the demo() command executed, which opened 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.

RStudio IDE

Figure 3.18: R Console

RStudio is a company that is dedicated to helping the R community with its 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. The open source desktop edition, which is described in this section, is very suitable for developing in R. Already, this edition has built in the majority of features needed 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 FilesRStudiobinrstudio.exe file, if you used the defaults during installation.

RStudio screen is, by default, split into four panes when you open an R script file, as Figure 3.19 shows. The open script file is showing the R script used for the demos in Chapter 13, Supporting R in SQL Server , of this book.

RStudio IDE

Figure 3.19: 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 a list of possible arguments, or, if you have already started to write the name of an object, all objects that start with the letters you have already written.

The top-left pane is the Source pane that is, by default, the settings used for the script. 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 execute it by pressing the Ctrl + Enter keys.

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 can see 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 previous commands. The history goes beyond the commands in the current session, 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 the R documentation and 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.

Figure 3.20 shows all four panes in action. You can see the usage of the Tab key in the Source pane to autocomplete 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 details about the iris dataset that is loaded in memory. The Plots pane shows plots for all of the variables in the demo iris dataset.

RStudio IDE

Figure 3.20: RStudio IDE in action

Note that you can enlarge 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. 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 AltShiftK keys at the same time. The Keyboard Shortcut Quick Reference cheat sheet appears, as Figure 3.21 shows. You can get rid of this cheat sheet by pressing the Esc key.

Note that, although exhaustive, even this cheat sheet is not complete. In the top-right corner of the cheat sheet you can see 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.

You have access to many of the keyboard shortcut 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 also find links to various cheat sheets, for example, to the complete RStudio IDE cheat sheet, a PDF document you can download from the RStudio site, besides the help options you would usually expect.

RStudio IDE

Figure 3.21: RStudio IDE keyboard shortcuts cheat sheet

R Tools for Visual Studio

Microsoft developed R Tools for Visual Studio (RTVS) for those developers who are used to developing code in the popular Microsoft's IDE, Visual Studio. You can download the tools from https://www.visualstudio.com/vs/rtvs/.

Once you install RTVS, you open the Visual Studio as you would open it for any other project. Of course, since SQL Server Data Tools (SSDT) is not a separate product, it is just another shortcut to the Visual Studio IDE. You can also open SSDT and get the R Tools menu besides other common Visual Studio menus.

With RTVS, you get most of the useful panes of the RStudio IDE. You get the Source pane, the Console pane, which is called R Interactive in RTVS, and the Plots pane. Figure 3.22 shows the RTVS window with the Source, R Interactive, and the Plots pane open, showing the same plots for the variables from the iris demo dataset, as shown in Figure 3.20 earlier in this section.

R Tools for Visual Studio

Figure 3.22: R Tools for Visual Studio

If you are familiar with the Visual Studio IDE, then you might want to test RTVS.

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

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