Book Image

Learning R for Geospatial Analysis

By : Michael Dorman
Book Image

Learning R for Geospatial Analysis

By: Michael Dorman

Overview of this book

Table of Contents (18 chapters)
Learning R for Geospatial Analysis
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
External Datasets Used in Examples
Cited References
Index

Installing R and using the command line


In this section, we'll cover the installation of R before getting started with the R command line.

Downloading R

The R software can be downloaded from the R Project website at http://www.r-project.org/. The following screenshot shows the main page of this website:

Perform the following steps to download R from the R Project website:

  1. Under the Getting Started section, select the download R link.

  2. Select one of the download sources (it does not matter which one).

  3. Choose the appropriate version for your operating system, such as Linux, Mac OS, or Windows.

  4. If you are using Windows, which is the option we will cover from now on, select install R for the first time.

  5. Finally, click on the download link. This may vary according to the name of the current R version, such as Download R 3.1.0 for Windows.

Installing R

After downloading the file, follow the installation instructions. Note that if you are using a 64-bit version of Windows, you will be asked to select whether to install a 32-bit version, 64-bit version, or both. It is recommended that you use the 64-bit version in this case since it allows a single process to take advantage of more than 4 GB of RAM (this is helpful, for example, when loading a large raster file into memory).

Using R as a calculator

Start R by navigating to Start | All Programs | R and choose the appropriate shortcut from there (such as R x64 3.1.0 when running the 3.1.0 64-bit version of R).

The main screen of the R Graphical User Interface (RGui) looks like the following screenshot:

The window you see when starting the program, R Console, is the command line. The > symbol followed by a flashing cursor indicates that the system is waiting for instructions from the user. When the user types an expression into the command line and presses Enter, that expression is interpreted from the R language into the language that the computer processor understands, and the respective operation that expression entails is performed. As you may have noted, very few point-and-click menu options are found within the R environment as almost all operations are only accessible through code.

First, we will try simple calculations. For example, type the expression 5+5. The result 10 will appear on the next line followed by the > symbol, indicating that all instructions have been executed and the system is waiting for new ones:

> 5+5
[1] 10

What has just happened is that the expression 5+5 was interpreted and the respective instruction (add 5 and 5) was sent to the processor. The processor found the result (which is 10), which was then returned and printed in the command-line window. As we will see later, the result was saved neither in the RAM nor in the long-term computer memory, such as the hard disk. The meaning of the [1] part is that the result is a vector, with the first member being the number 10. Vectors will be covered in the next chapter.

Note that an R expression can be several lines long. For example, if we type 5* and press Enter, the symbol + appears on the next line, indicating that R is waiting for the remaining part of the expression (5 multiplied by ...):

> 5*
+ 2
[1] 10

If you change your mind and do not wish to complete the expression, you can press Esc to cancel the + mode and return to the command line. Pressing Esc can also be used to terminate the current process that is being executed. (We didn't get a chance to try that out yet since simple operations such as 5+5 are executed very quickly.)

Note

While using the command line, you can scroll through the history of previously executed expressions with the and keys. For example, this can be useful to modify a previously executed expression and run it once more.

You can clear all text from the command-line window by pressing Ctrl + L.

Throughout this book, code sections display both the expressions that the user enters (following the > symbol) and the resulting output. Reading both the inputs and the outputs will make it easier to follow the code examples. If you wish to execute the code examples in R and to investigate what happens when modifying them (which is highly recommended), only the input expressions should be entered into the R interpreter (these are the expressions followed by the >, or + if the expression spans several lines, symbols). Therefore, copying and pasting the entire content of code sections directly from the book into the interpreter will result in errors, since R will try to execute the output lines. The input, in fact, will not be correctly interpreted either since input expressions include > and + symbols that are not part of the code. To make things easier, all code sections from this book are provided on the book's website as plain R code files.