R Series

How do I work with Directory in R?

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

Zigya Acadmey

Share
Published by
Zigya Acadmey

Recent Posts

Understanding Standard Form of Numbers: An Explanation With Examples

Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…

6 months ago

How to deal with stress and anxiety in college

Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…

7 months ago

Why is Sociology Important These Days?

Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…

7 months ago

How to Convert Inches to mm

Some info about Inch Inches are a unique measure that persuades us that even the…

9 months ago

Antilogarithms – Definition, Methods, and Examples

You should be familiar with logarithms to understand antilogarithms in a better manner. Logarithms involve…

10 months ago

नाटककार सुरेंद्र वर्मा

यहां "नाटककार सुरेंद्र वर्मा" पुस्तक की पीडीएफ विद्यार्थी, शोधार्थी और जो इसका अभ्यास के लिए…

10 months ago