Getting started with setting up a project, working directory, subfolders, and loading data.
We will set up a working directory, complete with an Rproject and subfolders. We will show you how to load your data and save your data.
For details about this dataset, see Post #2 by Akhila and Stephanie.
For details about Using parellel iteration and writing functions to generate plots, see Post #3 by Joanna
You are probibly framilar with this process as a typical way to start any RStudio project. The process goes something like this:
Start RStudio.
Under the File menu, click on New Project.
Choose New Directory, then New Project.
Enter a name for this new folder (or “directory”), and choose a convenient location for it. This will be your working directory for the project (e.g., ~ /funprog_blog).
Click on Create Project.
here()
function to help with file pathwaysLet’s start this time by figuring out one of the most difficult aspects of learning to code initially… where are we?? OR… where does my computer think we are pointed? File pathways are difficult to navigate, keep straight and understand when you first begin to code. We will use the here()
function so the results in our path are always relative to our projects root directory. First understand where we are.
here::here()
[1] "C:/Users/Stephanie Gluck/OneDrive - University Of Oregon/2020Spring/EDLD610/funprog_blog"
# create_setup(here::here())
# create_setup(here::here("temporary-wd"))
Now that we know the file pathway, this thing: "[1] "/Users/brockrowley/Phd in SPED/2020Spring/funprog_blog"
we know where we are (root).
We want to keep all our data, analyses, and text in one, self-contained, single folder, called the working directory. We will use the fs package
to do this work for us.
fs::dir_create(here::here("temporary-wd"))
usethis::proj_activate(here::here("temporary-wd"))
create_setup <- function(dir = getwd()) {
to_create <- c("data", "scripts", "plots")
purrr::walk(to_create, ~{
fs::dir_create(file.path(dir, .x))
})
}
create_setup(here::here("temporary-wd"))
In the code chunk above, we used “temporary-wd” as a placeholder for whatever you want to call your project. If you replace “temporary-wd” with whatever you want to call your project, you will create a new RStudio project, complete with three new folders inside (data, scripts, plots) to get started.
The most common way to get data into R is to store it as a CSV. Drop your data file into the folder we created above called “data” and run the following code chunk to load your data. base R has a read_csv()
command from readr. readr is part of the tidyverse, and read_csv()
is fast.
If you want to read in just one file, you’d do something like this:
files <- list.files(here::here("data"),
full.names = TRUE)
d <- read_csv(files[1]) %>%
janitor::clean_names()
But, if you have a bunch of different files, and each file has the same column headers (e.g., files separated by grade), then you could do something like this:
d <- map_df(files, read_csv, .id = "file")
In the above code chunk the .id
keeps the information from the file name in the first column, as file names often have useful information about the data (e.g., grade level or content area and such).
To write a data frame to a CSV, use the write_csv()
function. By default, the file will be written to the working directory.
#write_csv(data, path = "cool_name.csv")
You just remove the hashtag (#) from the #write_csv
and put the name of your .csv data to be saved where it says "cool_name.csv"