R/C2/Working-directories-in-RStudio/English

From Script | Spoken-Tutorial
Revision as of 15:40, 24 April 2019 by Sudhakarst (Talk | contribs)

Jump to: navigation, search

Title of script: Working directories in RStudio

Authors: Shaik Sameer (IIIT Vadodara) and Sudhakar Kumar (IIT Bombay)

Keywords: R, RStudio, working directory, read csv

Visual Cue Narration
Show slide

Opening slide

Welcome to this tutorial on Working directories in RStudio.
Show slide

Objectives

In this tutorial, you will learn about:
  • Working directories in RStudio
  • How to read and store csv files in R
Show slide

Pre-requisites

To understand this tutorial, you should know:
  • R and RStudio
  • Basic commands of R

If not, please locate the relevant tutorials on R on this website.

Show slide

Systems requirements

This tutorial is recorded on
  • Ubuntu Linux OS 16.04
  • R version 3.2.3
  • RStudio version 1.1.456

Install R version 3.2.0 or higher.

Text on screen

Download CaptaincyData.csv file from the Code files link

We will use the captaincy data set which we created earlier in this series.

Download this data set CaptaincyData.csv from the Code files link of this tutorial.

[Computer screen]

Highlight CaptaincyData.csv in the folder myProject

I have downloaded and moved this data set to the folder myProject on my Desktop.

I shall set this folder as my Working Directory.

Show slide

R scripts

  • Remember we had created two scripts, myfirstscript.R and mysecondscript.R
  • Both were saved in the folder myProject, on the Desktop.
Show slide

Working directory

  • R always points to a default directory on our computer
  • This directory is where R will look, by default, for files you ask it to load.
We will find out what our current Working Directory is.

Let us switch to RStudio.

[RStudio]

Console >> type getwd()

In the Console window, type getwd parentheses and press Enter.
Point to /home/fossee in Console The current Working Directory is shown in the Console.

Note down the directory path.

Now, we will learn how to set our Working Directory.
[RStudio]

Highlight myProject folder on Desktop

I will set the folder myProject on my Desktop as my Working Directory.
I am maximizing the Files and Plots window of RStudio.
Go to graphical output window at the bottom right >> select the Files tab At the top of the Files and Plots window, click on the Files tab.
Click on the button with three dots at the right corner Go to the upper right corner of the same window.

Below the refresh icon, click on the button with three dots.

Point to Go To Folder Go To Folder window appears.
Select myProject folder >> click Open Select the folder which you want to make as your Working Directory and click Open.

I am selecting myProject located on my Desktop.

Point to the two scripts under Files tab Under Files tab, you can see that the two scripts created earlier, have been loaded.
[RStudio]

Select More >> Set As Working Directory

Now, below the Files tab, click on the More button with the blue gear symbol.

From the dropdown menu, select the Set As Working Directory option.

Now this folder is set as your Working Directory.

Point to the Console You may verify this by looking at the Console.
Now, I am minimizing the Files and Plots window.
There is one other way to set the Working Directory also.
[RStudio]

Console >> type setwd("/home/fossee/

Desktop/myProject")

In the Console, type setwd.

Now, inside parentheses, type the desired folder path within double quotes.

Press Enter.

Highlight CaptaincyData.csv in the File tab Now, we will learn how to read and store a csv file called CaptaincyData.csv.
We shall declare a variable captaincy to store CaptaincyData.csv.
[RStudio]

captaincy = read.csv("CaptaincyData.csv")

In the Console window, type captaincy space equal sign space read.csv

Now, CaptaincyData.csv inside double quotes in parentheses and press Enter.

Highlight read.csv read.csv function is used to read a file and to create a data-frame from it.
[RStudio]

View(captaincy)

Highlight the data frame in Source window

Now, we use View function to see the stored data frame.

In the Console window, type View captaincy in parentheses and press Enter.

The contents of captaincy data frame appears in the Source window.

I am maximizing the Source window to see the captaincy data set.
This brings us to the end of this tutorial. Let us summarize.
Show slide

Summary

In this tutorial, we have learned
  • How to get and set a working directory
  • How to read and store a csv file
Show slide

Assignment

We now suggest an assignment.
  • Create a new folder on your computer and make it your Working Directory.
Show slide

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Show slide

Spoken Tutorial Workshops

We conduct workshops using Spoken Tutorials and give certificates.

Please contact us.

Show Slide

Forum to answer questions

Pls post your timed queries in this forum.
Show Slide

Forum to answer questions

Pls post your general queries in this forum.
Show Slide

Textbook Companion

The FOSSEE team coordinates coding of solved examples of popular books.

We give honorarium and certificates to those who do this.

For more details, please visit these sites.

Show Slide

Acknowledgement

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

This script for this tutorial was contributed by Shaik Sameer (FOSSEE Fellow 2018).

This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Sudhakarst