17.1 R Projects

A good way to start with your analysis is to first set up an R project in RStudio. R projects create a new environment associated with one specific folder on your computer which contain all the data and R code you need for your analyses. Conducting your analyses in an R project means that all objects you create are temporarily saved in your project and will be accessible the next time you resume working on it. To create a new R project, you can click on the “R project” field in the upper-right corner of your RStudio window, and then on “New Project…” in the dropdown menu.

You can then create a New Directory, a new folder on your computer, which will then become the working directory of your project.

Then, click on “New Project”.

I will give my new project the name “Meta-Analysis Project”. The project folder will be stored in my ~Documents/R folder.

After clicking “Create Project”, your R project is set. Under the Files tile in the lower-right pane of your RStudio window, you will see all data currently stored in your project folder. By clicking on “New Folder”, you can also create subfolders directly in RStudio, for example a folder containing all your data.

A great feature of R projects is that we do not have to use absolute paths to the files we want to reference or import. We only have to name the file name, or, if the file is in a subfolder, the subfolder name and the file name. For example, let us assume I have a dataset data.xlsx stored in my data subfolder. Using the readxl package, I can easily import the dataset by only specifying the “data” folder and the file name.

read_excel("data/data.xlsx")

It is also possible to save all objects listed in my upper-right Environment as an R Data (.rda) file in the project folder. Let us assume I have a data.frame called dat stored in my Global Environment. I can save this dataset in my data folder with this code:

save(dat, file = "data/dat.rda")

I can then load the data set back into my Global Environment again following the same logic:

load("data/dat.rda")

\[~\]

banner