Time for action - setting your R working directory

To demonstrate how to use the setwd(dir) and getwd() functions, we will set our working directory to the desktop:

  1. Determine the file path to your desktop. Note that this path will vary depending on your operating system and the structure of your hard drive. An example, which you should replace with your computer's path, is shown here:
    > #set the R working directory
    > #this should be the first thing you do every time you open R
    > #I am going to set the R working directory to my desktop
    at "/Users/johnmquick/Desktop"
    > #you should replace the sample location with the path to
    your desktop
    
  2. Use the setwd(dir) command to set your working directory to the path identified in step 1:
    > #use setwd(dir) to set the R working directory
    > setwd(dir = "/Users/johnmquick/Desktop")
    
  3. Verify that your working directory was set appropriately using the getwd() command:
    > #use getwd() to display the current R working directory
    > getwd()
    [1] "/Users/johnmquick/Desktop"
    

What just happened?

The working directory is the default location on your computer where R assumes all of your work is being conducted at a given time. For example, if you were to import data from an external file, R would automatically look for it in your working directory. Furthermore, all file path arguments in functions are evaluated relative to the working directory. Therefore, it is important to set your working directory each time you use R.

We set our R working directory using the setwd(dir) function and then verified its location using the getwd() command.

In setwd(dir), the dir argument accepts a path to the folder that is to become the working directory. For example, the path "/Users/johnmquick/Desktop" tells the setwd(dir) function to locate the Desktop folder within the johnmquick folder of the Users folder. It then sets this destination as the working directory.

After submitting the setwd(dir) command, R will drop down to the next line without providing any output:

> setwd(dir = "/Users/johnmquick/Desktop")
>

In one sense, this is good, because you would have received an error if the command failed. However, R can also be confusing at times, because it does not always provide you with feedback indicating the result of your commands.

Note

Note that in our setwd(dir) function, the dir = portion can be optionally omitted. In R, so long as a function's arguments occur in the default order, they do not have to be explicitly stated in the code. However, if only certain arguments are used, or if they are used in a different order, they must be stated explicitly. We will encountered several examples of both cases throughout this book.

Thankfully, we can use the getwd() command to verify the current working directory:

> getwd()
[1] "/Users/johnmquick/Desktop"

By using getwd() after setwd(dir), you can verify that your working directory has been defined appropriately. Remember that setting your working directory is the first thing you should do every time you launch R.

Pop quiz

  1. Which of the following is not true of the R working directory?

    a. It is set using the setwd(dir) command.

    b. It is displayed using the getwd() command.

    c. It is the default location where R assumes your work is being conducted.

    d. It only needs to be set once.

  2. Which of the following is true of the R console?

    a. It returns output for no functions entered by the user.

    b. It returns output for some functions entered by the user.

    c. It returns output for all functions entered by the user.

    d. It returns output for all functions, but not comments, entered by the user.

  3. In setwd(dir), dir is which of the following?

    a. A variable.

    b. A function.

    c. An argument.

    d. An element.

Have a go hero

Set your R working directory to a location of your choice using the setwd(dir) function. Then verify the location of your working directory using the getwd() command. It may be useful to designate a specific folder for all of your R work or for each individual project that you engage in. For example, you may want to create a specific folder on your computer for all of the activities that we will complete in this book. You could then set that location as your R working directory. Remember that your working directory should be set each time you open R.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.87.250