Chapter 3 R Projects

Hello!

This tutorial will provide a basic overview of R projects and markdowns!

3.1 Working Directory

The Working Directory is the folder that your computer is “focused on.” When you import data from your computer into R, you must make sure that the dataset and the R file are in the same folder.

You can use the function getwd() to figure out the working directory you are in. You can set the working directory using the setwd() function, or by going to Session –> Set Working Directory.

getwd()
## [1] "C:/Users/comm-lukito/Documents/J381M/2023_J381M_bookdown"
#setwd()

In the above chunk of code, setwd() does not run because it is “commented.”

3.2 Projects

While getwd() and setwd() are useful for navigating files within RStudio, your projects will most likely be stored in one folder (as in: you wat your code and data in the same general place on your computer so you’re not hopping back between multiple folders). Thus: Projects. When you create a project, you associate it with a working directory, making it easier to return back to that project.

For folder maintenance (and my sanity), I try to stick to “one research project, one R project”, though my more complex projects (with multiple papers) may be more like “one research paper, one R project.” In this class, I recommend doing one of the following: * One week, one new project (this means you will have about 14) * One dataset, one project (this class will use 3 core datasets)

To create a new project, click the “File” dropdown menu in the upper left corner, and then “New Project”. This will cause a pop up window to appear; the pop up should look like the screenshot below.

Creating a new directory Let’s start by clicking “New Directory”.

Creating a new project You can create a lot of different project types, but we’ll do “New Project” for this exercise (for the optional Quarto portion below, you will be using other project types). Once you click new project, you will be instructed to give that project a name. You will also indicate where the working directory will be housed.

Because R is case sensitive, I strongly recommend using only lowercase letters and no spaces (hence, hypens).

Once you click “Create Project”, R Studio will refresh and bring you to your new project space. At first glance, you may not see a difference. But if you look in the upper-right hand corner, you will find information about the project that you are in.

In the folder you created, you will also see a project icon. To open up your project in the future, you can use this to open up R studio and it will automatically open up that specific project space.

3.3 RMarkdowns

Now that our project is set up, let’s make our first rmarkdown file. rmarkdown files are a great way for you to both write code and text together. This textbook, for example, is written with rmd files. Compared to “.r” files (a simple space to write code), “.rmd” (rmarkdown) files can be published in a beautiful way that allows for a mix of code and writing, making it popular for textbooks, blogs/websites, applications, and collaborative work.

We can create an rmd file by going to “File” in the upper left corner, and selecting “R Markdown” (this file will be saved in your project).

Then, a pop up window will appear. In it, you will write a title for your file (this appears at the top of the rendered file) and your name (which appear underneath the title). You can also indicate the date, but the default is to save the date of the file when it is rendered.

You will also notice that the pop-up contains options for the type of output file you would like to create. HTML is the default option, but PDF is also sometimes used. It is less common to convert the rmd into a doc file as the latter is not meant to show code, and because this conversion is the least accurate.

Once you click “OK”, the Rmarkdown will be generated. More likely, you will see a file that looks like this:

Don’t forget to save this file. Note that when you create it, it will be titled “Untitled1”, so rename this file with something you can recognize. Similar to the working directory/new project, I encourage fully lowercase titles with hyphens.