官术网_书友最值得收藏!

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 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.

主站蜘蛛池模板: 布尔津县| 威宁| 岳西县| 东乡县| 怀集县| 清水县| 赞皇县| 阿勒泰市| 达日县| 抚顺县| 岐山县| 高雄县| 边坝县| 林西县| 阿坝| 漯河市| 西峡县| 芜湖县| 宁陕县| 呼图壁县| 丰台区| 四平市| 于田县| 庆元县| 巴中市| 闽清县| 碌曲县| 正宁县| 常宁市| 彭水| 奎屯市| 阳泉市| 安吉县| 通渭县| 申扎县| 依兰县| 孝感市| 山西省| 江西省| 腾冲县| 拜城县|