R For Dummies. Vries Andrie de

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

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

R For Dummies - Vries Andrie de

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

style="font-size:15px;">      > h <– "Hello"

      > h

      [1] "Hello"

      

You must enter text or character values to R inside quotation marks – either single or double. R accepts both. So both h <– "Hello" and h <– 'Hello' are examples of valid R syntax. For consistency, we use double quotes throughout this book.

      In “Using vectors,” earlier in this chapter, you use the c() function to combine numeric values into vectors. This technique also works for text:

      > hw <– c("Hello", "world!")

      > hw

      [1] "Hello" "world!"

      You use the paste() function to concatenate multiple text elements. By default, paste() puts a space between the different elements, like this:

      > paste("Hello", "world!")

      [1] "Hello world!"

Talking back to the user

      You can write R scripts that have some interaction with a user. To ask the user questions, you can use the readline() function. In the following code snippet, you read a value from the keyboard and assign it to the variable yourname:

      > h <– "Hello"

      > yourname <– readline("What is your name? ")

      What is your name? Andrie

      > paste(h, yourname)

      [1] "Hello Andrie"

      This code seems to be a bit cumbersome, however. Clearly, it would be much better to send these three lines of code simultaneously to R and get them evaluated in one go. In the next section, we show you how.

      Sourcing a Script

      Until now, you’ve worked directly in the R console and issued individual commands in an interactive style of coding. In other words, you issue a command, R responds, you issue the next command, R responds, and so on.

      In this section, you kick it up a notch and tell R to perform several commands one after the other without waiting for additional instructions. Because the R function to run an entire script is source(), R users refer to this process as sourcing a script.

      To prepare your script to be sourced, you first write the entire script in an editor window. In RStudio, for example, the editor window is in the top-left corner of the screen (refer to Figure 2-4). Whenever you press Enter in the editor window, the cursor moves to the next line, as in any text editor.

      To create a new script in RStudio, begin by opening the editor window (choose File ⇒ New File ⇒ R script to open the editor window). Type the following lines of code in the editor window. Notice that the last line contains a small addition to the code you saw earlier: the print() function.

      h <– "Hello"

      yourname <– readline("What is your name?")

      print(paste(h, yourname))

      

Remember to type the print() function as part of your script. Sourced scripts behave differently from interactive code in printing results. In interactive mode, a result is printed without needing to use a print() function. But when you source a script, output is by default printed only if you have an explicit print() function.

      You can type multiple lines of code into the source editor without having each line evaluated by R. Then, when you’re ready, you can send the instructions to R – in other words, source the script.

      When you use RGui or RStudio, you can do this in one of three ways:

      ✔ Send an individual line of code from the editor to the console. Click the line of code you want to run, and then press Ctrl+R in RGui. In RStudio, you can press Ctrl+Enter or click the Run button.

      ✔ Send a block of highlighted code to the console. Select the block of code you want to run, and then press Ctrl+R (in RGui) or Ctrl+Enter (in RStudio).

      ✔ Send the entire script to the console (which is called sourcing a script). In RGui, click anywhere in your script window, and then choose Edit ⇒ Run all. In RStudio, click anywhere in the source editor, and press Ctrl+Shift+S or click the Source button.

      

These keyboard shortcuts are defined only in RGui or RStudio. If you use a different source editor, you may have different options.

Now you can send the entire script to the R console. To do this, click the Source button in the top-right corner of the editor window or choose Edit⇒Source. The script starts, reaches the point where it asks for input, and then waits for you to enter your name in the console window. Your screen should now look like Figure 2-5. Notice that the Environment pane now lists the two objects you created: h and yourname.

       Figure 2-5: Sending a script to the console in RStudio.

      

When you click the Source button, source('~/.active-rstudio-document') appears in the console. What RStudio actually does here is save your script in a temporary file and then use the R function source() to call that script in the console. Remember this function; you’ll meet it again.

Echoing your work

If you click on the little arrow next to the Source button in RStudio, you see two different source options, as shown in Figure 2-6. By clicking the Source button before, you used the option without echo. This means that R will run the complete script at once, but won't send any output to the console.

       Figure 2-6: Sourcing your code with or without echo in RStudio

      If you click on the second option, R runs again the complete script in one go, but this time it will show every individual line in the console. So both options differ only in the output you see. You can safely try out both options to compare.

      You can use the echo option also outside RStudio by using the source() function with the argument echo set to TRUE. We explain functions and arguments in Chapter 3, and far more detailed again in Chapter 8.

      

Whether

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