R For Dummies. Vries Andrie de

Чтение книги онлайн.

Читать онлайн книгу R For Dummies - Vries Andrie de страница 9

R For Dummies - Vries Andrie de

Скачать книгу

any difference regarding the results of your code. You can use the echo option if you want to source a long script and keep track of which part of the script R is currently carrying out.

      Finding help on functions

      We discuss R’s built-in help system in Chapter 11, but for now, to get help on any function, type ? in the console. To get help with the paste() function, for example, type the following:

      > ?paste

      This code opens a Help window. In RStudio, this Help window is in the bottom-right corner of your screen by default. In other editors, the Help window sometimes appears as a local web page in your default web browser.

      You also can type help, but remember to use parentheses around your search term:

      > help(paste)

      Navigating the Environment

      So far in this chapter, you’ve created several variables. These form part of what R calls the global environment. The global environment refers to all the variables and functions (collectively called objects) that you create during the session, as well as any packages that are loaded.

      Often, you want to remind yourself of all the variables you’ve created in the environment. To do this, use the ls() function to list the objects in the environment. In the console, type the following:

      > ls()

      [1] "h"  "hw"  "x"  "y"  "yourname" "z"

      R tells you the names of all the variables that you created.

      

One very nice feature of RStudio lets you examine the contents of the environment at any time without typing any R commands. By default, the top-right window in RStudio has two tabs: Environment and History. Click the Environment tab to see the variables in your global environment, as well as their values. For example, in Figure 2-5 you see that the global environment contains one object called h that contains the value "hello".

Manipulating the content of the environment

      If you decide that you don’t need some variables anymore, you can remove them. Suppose that the object z is simply the sum of two other variables and no longer needed. To remove it permanently, use the rm() function and then use the ls() function to display the contents of the environment, as follows:

      > rm(z)

      > ls()

      [1] "h"  "hw"  "x"  "y"  "yourname"

      Notice that the object z is no longer there.

Saving your work

      You have several options for saving your work:

      ✔ You can save individual variables with the save() function.

      ✔ You can save the entire environment with the save.image() function.

      ✔ You can save your R script file, using the appropriate save menu command in your code editor.

      Suppose you want to save the value of yourname. To do that, follow these steps:

      1. Find out which working directory R will use to save your file by typing the following:

      > getwd()

      [1] "c:/users/andrie"

      The default working directory should be your user folder. The exact name and path of this folder depend on your operating system. (In Chapter 12, you get more familiar with the working directory.)

      

If you use the Windows operating system, the path is displayed with slashes instead of backslashes. In R, similar to many other programming languages, the backslash character has a special meaning. The backslash indicates an escape sequence, indicating that the character following the backslash means something special. For example, \t indicates a tab, rather than the letter t. (You can read more about escape sequences in Chapter 12.) Rest assured that, although the working directory is displayed differently from what you’re used to, R is smart enough to translate it when you save or load files. Conversely, when you type a file path, you have to use slashes, not backslashes.

      2. Type the following code in your console, using a filename like yourname.rda, and then press Enter.

      > save(yourname, file = "yourname.rda")

      R silently saves the file in the working directory. If the operation is successful, you don’t get any confirmation message.

      3. To make sure that the operation was successful, use your file browser to navigate to the working directory, and see whether the new file is there.

      You have a file browser in the lower-right panel of RStudio.

Retrieving your work

      To retrieve saved data, you use the load() function. Say you want to retrieve the value of yourname that you saved previously.

      First, remove the variable yourname, so you can see the effect of the load process:

       > rm(yourname)

      If you’re using RStudio, you may notice that yourname is no longer displayed in the Environment pane.

      Next, use load to retrieve your variable. Type load followed by the filename you used to save the value earlier:

      > load("yourname.rda")

      Notice that yourname reappears in the Environment pane of RStudio.

Chapter 3

      The Fundamentals of R

In This Chapter

      ▶ Using functions and arguments

      ▶ Making code clear and legible

      ▶ Extending R with user packages

      Before you start discovering the different ways you can use R on your data, you need to know a few more fundamental things about R.

      In Chapter 2, we show you how to use the command line and work with the global environment, so if you read that chapter, you can write a simple script and use the print(), paste(), and readline() functions – at least in the most basic way. But functions in R are more complex than that, so in this chapter we tell you how to get the most out of your functions.

      As you add more arguments to your functions and more functions to your scripts, those scripts can become pretty complex. To keep

Скачать книгу