R For Dummies. Vries Andrie de. Читать онлайн. Newlib. NEWLIB.NET

Автор: Vries Andrie de
Издательство: Автор
Серия:
Жанр произведения: Зарубежная образовательная литература
Год издания: 0
isbn: 9781119055839
Скачать книгу
code/functions.

      In practice, it means that you simply write your code and send it to R, and the code runs, which makes the development cycle easy. This ease of development comes at the cost of speed of code execution, however. The downside of an interpreted language is that the code usually runs slower than the equivalent compiled code.

      

If you have experience in other languages, be aware that R is not C or Java. Although you can use R as a procedural language such as C or an object-oriented language such as Java, R is mostly based on the functional programming paradigm. As we discuss later in this book, especially in Part III, this characteristic requires a bit of a different mindset. Forget what you know about other languages, and prepare for something completely different.

Chapter 2

      Exploring R

In This Chapter

      ▶ Looking at your R editing options

      ▶ Starting R

      ▶ Writing your first R script

      ▶ Finding your way around the R environment

      In order to start working in R, you need two things. First, you need a tool to easily write and edit code (an editor). You also need an interface, so you can send that code to R. Which tools you use depend to some extent on your operating system. The basic R install gives you these options:

      ✔ Windows: A basic user interface called RGui.

      ✔ Mac OS X: A basic user interface called R.app.

      ✔ Linux: There is no specific interface on Linux, but you can use any code editor (like Vim or Emacs) to edit your R code. R itself opens by default in a terminal window.

      At a practical level, this difference between operating systems and interfaces doesn’t matter very much. R is a programming language, and you can be sure that R interprets your code identically across operating systems.

      Still, we want to show you how to use a standard R interface, so in this chapter we briefly illustrate how to use R with the Windows RGui. Our advice also works on the Mac R.app.

      Fortunately, there is an alternative, third-party interface called RStudio that provides a consistent user interface regardless of operating system. RStudio increasingly is the standard editing tool for R, so we also illustrate how to use RStudio.

      In this chapter, after opening an R console, you flex your R muscles and write some scripts. You do some calculations, create some numeric and text objects, take a look at the built-in help, and save your work.

      Working with a Code Editor

      R is many things: a programming language, a statistical processing environment, a way to solve problems, and a collection of helpful tools to make your life easier. The one thing that R is not is an application, which means that you have the freedom of selecting your own editing tools to interact with R.

      In this section we discuss the Windows R interface, RGui (short for R graphical user interface). This interface also includes a very basic editor for your code. Since this standard editor is so, well, basic, we also introduce you to RStudio. RStudio offers a richer editing environment than RGui and many handy shortcuts for common tasks in R.

      Alternatives to the standard R editors

      Among the many freedoms that R offers you is the freedom to choose your own code editor and development environment, so you don’t have to use the standard R editors or RStudio.

      These are powerful full-featured editors and development environments:

      ✔ Eclipse StatET (www.walware.de/goto/statet): Eclipse, another powerful integrated development environment, has an R add-in called StatET. If you’ve done software development on large projects, you may find Eclipse useful. Eclipse requires you to install Java on your computer.

      ✔ Emacs Speaks Statistics (http://ess.r-project.org): Emacs, a powerful text and code editor, is widely used in the Linux world and also is available for Windows. It has a statistics add-in called Emacs Speaks Statistics (ESS), which is famous for having keyboard shortcuts for just about everything you could possibly do and for its very loyal fan base. If you’re a programmer coming from the Linux world, this editor may be a good choice for you.

      ✔ Tinn-R (http://nbcgib.uesc.br/lec/software/editores/tinn-r/en): This editor, developed specifically for working with R, is available only for Windows. It has some nice features for setting up collections of R scripts in projects. Tinn-R is easier to install and use than either Eclipse or Emacs, but it isn’t as fully featured.

      A couple of interfaces are designed as tools for special purposes:

      ✔ Rcommander (http://www.rcommander.com/): Rcommander provides a simple GUI for data analysis in R and contains a variety of plugins for different tasks.

      ✔ Rattle (http://rattle.togaware.com/): Rattle is a GUI designed for typical data mining tasks.

Exploring RGui

      As part of the process of downloading and installing R, you get the standard graphical user interface (GUI), called RGui. RGui gives you some tools to manage your R environment – most important, a console window. The console is where you type instructions and generally get R to do useful things for you.

Seeing the naked R console

The standard installation process creates useful menu shortcuts (although this may not be true if you use Linux, because there is no standard GUI interface for Linux). In the menu system, look for a folder called R, and then find an icon called R followed by a version number (for example, R 3.2.0, as shown in Figure 2-1).

       Figure 2-1: Shortcut icons for RGui (R x64) and RStudio.

When you open RGui for the first time, you see the R Console screen (shown in Figure 2-2), which lists some basic information such as your version of R and the licensing conditions.

       Figure 2-2: A brand-new session in RGui.

      Below all this information is the R prompt, denoted by a > symbol. The prompt indicates where you type your commands to R; you see a blinking cursor to the right of the prompt.

      We explore the R console in more depth in “Navigating the Environment,” later in this chapter.

Issuing a simple command

      Use the console to issue a very simple command to R. Type the following to calculate the sum of some numbers, directly after the prompt:

      > 24 + 7 + 11

      R responds immediately to your command, calculates and displays the total in the console:

      > 24 + 7 + 11

      [1] 42

      The answer is 42. R gives you one other piece of information: The [1] preceding 42 indicates that the value 42 is the first element in your answer. It is, in fact, the only element in your answer! One of the clever things about R