Project Structure

~10 Minutes

Lego clock

Learning Objectives

  1. Describe project and script
  2. Identify working directory
  3. Manage folder structure

Project or Script

Scripts are text files of code.
Projects are folder structures. An R project:

  • divides work into multiple parts.
  • contains code, data, images, etc.
  • defines the root directory in RStudio.

New RStudio Project

Let's make a new project!

File -> New Project

Select "New Directory".
We won't cover Version Control today.

RStudio: New Project

RStudio Project Type

Create an Empty Project

I promise, you don't want to do either of the others.

RStudio: New Project

RStudio Project Name

Directory Name: foo

~ is your home directory

Don't start a repository.
RStudio creates a git repo at the project root.

RStudio: New Project

Spacer

Spacer

"Open in new session" opens a new window.

Our New Project


R version 3.3.3 (2017-03-06) -- "Another Canoe"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> getwd()
[1] "/home/abby/foo"
    

Locate your new folder.

foo.Rproj is an RStudio file.

Your Turn! Create a script.


## Load some data:
data(cars)

## Look at it:
str(cars)
head(cars)
?cars

## Play with it: 
colMeans(cars)
plot(cars$speed, cars$dist)
    

Your Turn! Save your script.

Save the file as my_cars.R. What happens?

What does this code return? Does it make sense?


getwd()
    

Your Turn! Restart your project.

Close RStudio and restart it.

Open your project.

Folder Structure

Create your own structure.
Today's example is modeled on R packages.

Goals:

  • Separate data management from analysis.
  • Emphasize documentation and reproducibity.
  • Compatible with version control sites.

Folder Structure Example

Root Project Folder (foo):

  • data/
    • README.md
    • something_awesome.R
    • something_awesome.Rda
  • R/
    • README.md
    • awesome_function.R
  • README.md
  • explore.R
  • some_awesome_analysis.R

Documentation

Start your R journey with documentation!

  • Document everything.
  • README.md is a naming convention.
  • .md are text files written in Markdown.

We will cover Markdown language in a future workshop.
For now, use plain text.

And Now You Know!

Q & A

Next up: Packages