+ - 0:00:00
Notes for current slide
Notes for next slide
These slides are viewed best by Chrome and occasionally need to be refreshed if elements did not load properly. See here for PDF .


Press the right arrow to progress to the next slide!

1/33


ETC5510: Introduction to Data Analysis

Week 6, part A


Style, file paths, & functions

Lecturer: Nicholas Tierney & Stuart Lee

Department of Econometrics and Business Statistics

ETC5510.Clayton-x@monash.edu

April 2020


Recap

  • Missing Data
  • Web Scraping
1/33

Upcoming due dates

  • Midsemester test: Opens after class on the 29th April
  • Assignment 2: 13th May (Released this week)
  • Practical Exam: 3rd June
  • Project: 8th June (See examples of past projects in assessments)
2/33

Midsemester test

  • Completed on Moodle as an online MCQ
  • Will be available from 29th April 8pm until 1st of May 11.59pm
  • Once started you will have 1 hour and 10 minutes to complete
  • Based on materials from weeks 1 - 5
3/33

How do I study for midsemester?

  • Take practice midsem available on course site
  • Revise lecture slides and draw mental models for core concepts
  • Look over the lab exercises
  • Look over relevant chapters in R4DS and complete exercises
4/33

Practical Exam?

  • A live data analysis
  • ~ 1 Hour to complete
5/33

Project?

  • Collect / find your own data
  • Clean the data
  • Determine interesting questions to answer about the data
  • Plan how to execute analysis of the data
  • Communicate the idea, data cleaning, and analysis (oral presentation)
  • Further details are on the course website
6/33

Lecture Overview

  • Organising your own folders
  • File paths and Rstudio projects
  • (Intro to) Using functions
7/33

File Paths and organising yourself

  • It's important when you start working on your own machine that you understand file storage hygiene.
  • It helps prevent unexpected problems and makes you more productive
  • You'll spend less time fighting against strange file paths.
  • Not sure what a file path is? We will explain that as well!
8/33

Your Turn

  1. What your normal "workflow" is for starting a new project / assessment
  2. Possible challenges that might arise when maintaining your project / assessment
  3. What is a file path?
9/33

What even is a file path?

  • This all might be a bit confusing if you don't know what a file path is.
  • A file path is: "the machine-readable directions to where files on your computer live."
  • So, this file path:
/Users/njtierney/rmd4sci-materials/demo-gapminder.Rmd

Describes the location of the file "demo-gapminder.Rmd".

10/33

What even is a file path

We could visualise this path:

/Users/njtierney/rmd4sci-materials/demo-gapminder.Rmd

as:

users
└── njtierney
└── rmd4sci-materials
└── demo-gapminder.Rmd
11/33

What even is a file path

  • To read in the gapminder.csv file, you might need to write code like this:
gapminder <- read_csv("/Users/njtierney/Desktop/rmd4sci-materials/data/gapminder.csv")

This is a problem, because this is not portable code.

12/33

A Mantra: Start a new project - start an RStudio project

setwd("c:/really/long/file/path/to/this/directory)
  • What do you think the setwd code does?
13/33

What does setwd() do?

  • "set my working directory to this specific working directory".

  • It means that you can read in data and other things like this:

data <- read_csv("data/mydata.csv")
  • Instead of
data <- read_csv("c:/really/long/file/path/to/this/directory/data/mydata.csv")
14/33

Using setwd()

  • This has the effect of making the file paths work in your file
  • This is a problem because, among other things, using setwd():
    • Has 0% chance of working on someone else's machine (this includes you in >6 months)
    • Your file is not self-contained and portable. (Think: "What if this folder moved to /Downloads, or onto another machine?")
  • To get this to work, you need to hand edit the file path to your machine.
  • This is painful. And when you do this all the time, it gets old, fast.
15/33

Using setwd()

  • This has the effect of making the file paths work in your file
  • This is a problem because, among other things, using setwd():
    • Has 0% chance of working on someone else's machine (this includes you in >6 months)
    • Your file is not self-contained and portable. (Think: "What if this folder moved to /Downloads, or onto another machine?")
  • To get this to work, you need to hand edit the file path to your machine.
  • This is painful. And when you do this all the time, it gets old, fast.

If you have an RStudio project file inside the rmd4sci-materials folder, you can instead write the following:

gapminder <- read_csv("data/gapminder.csv")
15/33

Your Turn: Think about this before discussion

  • (1-2 minutes) What folders are above the health.csv file in the following given file path?

"/Users/miles/etc5510/week1/data/health.csv"

  • and the result of using the below code in demo-gapminder.Rmd, then using the code, and then moving this to another location, say inside your C drive?
setwd("Downloads/etc5510/week1/week1.Rmd)
16/33

Is there an answer to the madness?

  • This file path situation is a real pain.
  • Is there an answer to the madness?
17/33

Is there an answer to the madness?

  • This file path situation is a real pain.
  • Is there an answer to the madness?

The answer is yes!

I highly recommend when you start on a new idea, new research project, paper. Anything that is new. It should start its life as an rstudio project.

17/33

Rstudio projects

An rstudio project helps keep related work together in the same place. Amongst other things, they:

  • Keep all your files together
  • Set the working directory to the project directory
  • Starts a new session of R
  • Restore previously edited files into the editor tabs
  • Restore other rstudio settings
  • Allow for multiple R projects open at the same time.
18/33

Rstudio projects

This helps keep you sane, because:

  • Your projects are each independent.
  • You can work on different projects at the same time.
  • Objects and functions you create and run from project idea won't impact one another.
  • You can refer to your data and other projects in a consistent way.

And finally, the big one

RStudio projects help resolve file path problems, because they automatically set the working directory to the location of the rstudio project.

19/33

The "here" package

  • RStudio projects help resolve file path problems
  • In some cases you might have many folders in your r project. To help navigate them appropriately, you can use the here package to provide the full path directory, in a compact way.
here::here("data")

returns

[1] "/Users/njtierney/Desktop/rmd4sci-materials/data"
20/33

The here package

here::here("data", "gapminder.csv")

returns

[1] "/Users/njtierney/Desktop/rmd4sci-materials/data/gapminder.csv"

You can read the above here code as:

In the folder data, there is a file called gapminder.csv, can you please give me the full path to that file?

21/33

The here package

This is really handy for a few reasons:

  1. It makes things completely portable
  2. Rmarkdown documents have a special way of looking for files, this helps eliminate file path pain.
  3. If you decide to not use RStudio projects, you have code that will work on any machine
22/33

Remember

If the first line of your R script is

setwd("C:\Users\jenny\path\that\only\I\have")

I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.

-- Jenny Bryan

23/33

Aside: How to create an RStudio project

24/33

Summary of file paths and rstudio projects

In this lesson we've:

  • Learnt what file paths are
  • How to setup an rstudio project
  • How to construct full file paths with the here package
25/33

Recommendations on how to file structure in ETC5510

26/33

File structures for class

Approach 1: Folder per week

/Users/njtierney/etc5510/week_1/
users
└── njtierney
└── etc5510
└── etc5510.Rproj
└── week_1
└── lecture_1.html
└── lecture_1.pdf
└── ida-exercise-1.Rmd
└── data
└── file.csv
└── week_2
└── lecture_2.html
└── lecture_2.pdf
└── ida-exercise-2.Rmd
└── data
└── file.csv
27/33

File structures for class

Approach 2: flater structure

/Users/njtierney/etc5510/
users
└── njtierney
└── etc5510
└── etc5510.Rproj
└── lecture_1.html
└── lecture_1.pdf
└── ida-exercise-1.Rmd
└── data
└── data.csv
28/33

Remember: There is no one true "correct" file format

It's just important to have a system

29/33

Motivating Functions

30/33

Do you see any problems with this code?

st_episode <- st %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
got_episode <- got %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
twd_episode <- got %>%
html_nodes(".np_right_arrow .bp_sub_heading") %>%
html_text() %>%
str_replace(" episodes", "") %>%
as.numeric()
31/33

Next Lecture: Why functions?

  • Automate common tasks in a power powerful and general way than copy-and-pasting:
    • You can give a function an evocative name that makes your code easier to understand.
    • As requirements change, you only need to update code in one place, instead of many.
    • You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
32/33

Next Lecture: Why functions?

  • Automate common tasks in a power powerful and general way than copy-and-pasting:
    • You can give a function an evocative name that makes your code easier to understand.
    • As requirements change, you only need to update code in one place, instead of many.
    • You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
  • Down the line: Improve your reach as a data scientist by writing functions (and packages!) that others use
32/33

Take the lab quiz!

33/33


ETC5510: Introduction to Data Analysis

Week 6, part A


Style, file paths, & functions

Lecturer: Nicholas Tierney & Stuart Lee

Department of Econometrics and Business Statistics

ETC5510.Clayton-x@monash.edu

April 2020


Recap

  • Missing Data
  • Web Scraping
1/33
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow