Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning R for Geospatial Analysis

You're reading from   Learning R for Geospatial Analysis Leverage the power of R to elegantly manage crucial geospatial analysis tasks

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher Packt
ISBN-13 9781783984367
Length 364 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Michael Dorman Michael Dorman
Author Profile Icon Michael Dorman
Michael Dorman
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. The R Environment FREE CHAPTER 2. Working with Vectors and Time Series 3. Working with Tables 4. Working with Rasters 5. Working with Points, Lines, and Polygons 6. Modifying Rasters and Analyzing Raster Time Series 7. Combining Vector and Raster Datasets 8. Spatial Interpolation of Point Data 9. Advanced Visualization of Spatial Data A. External Datasets Used in Examples
B. 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:

Downloading R

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:

Using R as a calculator

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 Using R as a calculator and Using R as a calculator 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.

You have been reading a chapter from
Learning R for Geospatial Analysis
Published in: Dec 2014
Publisher: Packt
ISBN-13: 9781783984367
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image