Press "Enter" to skip to content

How do I work with Directory in R?

Zigya Acadmey 0

After installing R and RStudio, the question is now how to start using R/RStudio. In this article, we’ll describe how to run RStudio and to set up your working directory.

Using R outside RStudio

If its your first time using R

List of some common command you will require before stating R

cd       // Change directory
mkdir    // Make a directory
rmdir    // Remove a directory
dir()    // List all files and subdirectories in that directory
getwd()  // To get the current working directory
setwd()  // To set the current working directory

Under Wiwndows and MAC OSX

  1. Create a subdirectory, named R, in your “Documents” folder. This subfolder will also be known as the working directory, will be used by R to read and save files.
  2. Now Launch R by double-clicking on the R icon.
  3. Specify your working directory to R:
    • On Windows: File -> Change
    • On MAC OSX: Tools -> Change the working directory

Under Linux

  • Open the shell prompt
  • Create a working directory, named R, using “mkdir” command
$ mkdir R
$ cd R/
  • Start the R Program with the command “R”:
$ R
  • To quit R program , use this:
$ q()

Under R inside RStudio

Launch RStudio under Windows, MAC OSX and Linux

After installing R and RStudio, launch RStudio from your computer “application folders”.

Code editor

RStudio is a four pane work-space for 1) creating file containing R script, 2) viewing command histories, 3) typing R commands, 4) viewing plots and more.

Change your working directory

  1. Create a sub-directory named “R” in your “Documents” folder
  2. From RStudio, use the menu to change your working directory under Session > Set Working Directory > Choose Directory.
  3. Choose the directory you’ve just created in step 1

You can also you console to set and view working directory

  • To view the working directory type
> getwd()
[1] "C:/Users/Admin/Desktop"
  • To change or set working directory type
> setwd("C:/Users/Admin/Documents/R")

Set a default working directory

A default working directory is a folder where RStudio goes, every time you open it. You can change the default working directory from RStudio menu under: Tools –> Global options –> click on “Browse” to select the default working directory you want.

Closing R/RStudio session

Each time you close R/RStudio, you will be asked whether you want to save the data from your R session. If you decide to save, the data will be available in future R sessions.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *