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
Press the right arrow to progress to the next slide!
Lecturer: Nicholas Tierney & Stuart Lee
Department of Econometrics and Business Statistics
ETC5510.Clayton-x@monash.edu
April 2020
/Users/njtierney/rmd4sci-materials/demo-gapminder.Rmd
Describes the location of the file "demo-gapminder.Rmd".
We could visualise this path:
/Users/njtierney/rmd4sci-materials/demo-gapminder.Rmd
as:
users └── njtierney └── rmd4sci-materials └── demo-gapminder.Rmd
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.
setwd("c:/really/long/file/path/to/this/directory)
setwd
code 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")
data <- read_csv("c:/really/long/file/path/to/this/directory/data/mydata.csv")
setwd()
setwd()
:setwd()
setwd()
:If you have an RStudio project file inside the rmd4sci-materials
folder, you can instead write the following:
gapminder <- read_csv("data/gapminder.csv")
health.csv
file in the following given file path? "/Users/miles/etc5510/week1/data/health.csv"
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)
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.
An rstudio project helps keep related work together in the same place. Amongst other things, they:
This helps keep you sane, because:
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.
here
package to provide the full path directory, in a compact way.here::here("data")
returns
[1] "/Users/njtierney/Desktop/rmd4sci-materials/data"
here
packagehere::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 calledgapminder.csv
, can you please give me the full path to that file?
here
packageThis is really handy for a few reasons:
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
In this lesson we've:
here
packageApproach 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
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
It's just important to have a system
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()
Lecturer: Nicholas Tierney & Stuart Lee
Department of Econometrics and Business Statistics
ETC5510.Clayton-x@monash.edu
April 2020
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 |