Sunday, September 9, 2018

Lesson 2: Working Directory & File Operations in R

So you got your first program behind you!  Now we'll take a look at how you can determine where R is storing your new script.  The following is a tutorial on how to determine your "working directory", set your working directory and then do some simple file operations while you're in the "working directory".

So, you might be asking "What exactly is the working directory"?  The working directory is where R-Studio saves your scripts as well as R-Studio's .Rdata files that contain workspace  settings and data so that you can load a past session and pick-up where you left off.  Additionally, the working directory is where R-Studio will look if you ask it to read a file if you do not provide an absolute path.

STEP#1 - CHECK YOUR WORKING DIRECTORY

To display the working directory in the R-Studio console you can use the 'getwd' function.

getwd()

My working directory is currently set to:

[1] "/Users/RGuy/Documents"

STEP#2 - SET YOUR WORKING DIRECTORY

Next, to change the working directory  to my Desktop in R-Studio, you can use the 'setwd' function.

work_directory = "~/Desktop"
setwd(work_directory)

STEP#3 - SEE CONTENTS OF WORKING DIRECTORY

Next, let's display the contents of our working directory
dir(work_directory)

The contents of my working directory currently are:
[1] "a.jpg"       "b.jpg"      "Documents"   "temp_submit"

STEP#4 - PERFORM VARIOUS FILE OPERATIONS

You can perform various file operations while in R-Studio. Let's store a list of the files and folders in the working directory and then do the following: a) Copy a file, b) Rename a file, c) Determine time file was last modified, and lastly d) Determine size of file. Store a list of the working directory's contents (files & folders)
directory_contents = dir(work_directory)

a. Copy 'a.jpg' and rename the it to 'a_copy.jpg'.  Remember that 'a.jpg was the first item list in the working directory contents. Therefore, since we stored the directory's content information in the 'directory_content' variable as a list, we can reference item #1 (a.jpg) in the list by typing 'directory_contents[1]'.
file.copy(directory_contents[1], "a_copy.jpg")

b. Next we'll rename 'a_copy.jpg' to 'c.jpg'
file.rename("a_copy.jpg", "c.jpg")

c. Now, we'll determine the date & time the file 'c.jpg' was last modified
file.mtime("c.jpg")

d. Finally, we'll see what the size of the file 'c.jpg' is in bytes.  NOTE: Divide value by 1,000 for kilobytes, and 1,000,000 for megabytes.
file.size("c.jpg")

STEP 5: SAVING YOUR SCRIPT

Only one more step.  Let's save that script you just created by clicking on "File" -> "Save As..." and let's name your script "R_Lesson2.R"  Click "Save".
Congratulations!  You've completed Lesson 2!

DOWNLOAD CODE Here is the code from my GitHub gist "R Lesson 2 - R-Studio Working Directory & File Operations" in case you'd rather just copy and paste it and then play around with it.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.