Working directories

The working directory is the default location where R will wait for files y'all desire to load and where it will put any files you salve. One of the great things about using RStudio Projects is that when yous open a project information technology volition automatically set up your working directory to the appropriate location. Yous can check the file path of your working directory past looking at bar at the top of the Panel pane. Note: the ~ symbol to a higher place is shorthand for /Users/nhy163/ on a Mac computer (the same on Linux computers).

Y'all tin also apply the getwd() function in the Console which returns the file path of the current working directory.

In the example above, the working directory is a folder called 'first_project' which is a subfolder of "Teaching' in the 'Alex' folder which in turn is in a 'Documents' binder located in the 'nhy163' folder which itself is in the 'Users' binder. On a Windows based computer our working directory would as well include a bulldoze letter of the alphabet (i.east.C:/Users/nhy163/Documents/Alex/Teaching/first_project).

If y'all weren't using an RStudio Project then you lot would have to set your working directory using the setwd() function at the get-go of every R script (something we did for many years).

                                          setwd('/Users/nhy163/Documents/Alex/Didactics/first_project')                      

However, the problem with setwd() is that it uses an absolute file path which is specific to the computer you lot are working on. If you lot desire to send your script to someone else (or if you're working on a unlike computer) this accented file path is not going to work on your friend/colleagues reckoner every bit their directory configuration will exist different (you are unlikely to have a directory structure /Users/nhy163/Documents/Alex/Education/ on your estimator). This results in a project that is not self-contained and not easily portable. RStudio solves this problem by allowing you lot to use relative file paths which are relative to the Root project directory. The Root project directory is simply the directory that contains the .Rproj file (first_project.Rproj in our case). If yous desire to share your analysis with someone else, all you need to practice is copy the entire project directory and send to your to your collaborator. They would then just need to open up the project file and whatsoever R scripts that contain references to relative file paths will but work. For example, let's say that you've created a subdirectory chosen raw_data in your Root project directory that contains a tab delimited datafile called mydata.txt (we will cover directory structures below). To import this datafile in an RStudio projection using the read.table() office (don't worry virtually this now, nosotros will comprehend this in much more particular in Affiliate 3) all you need to include in your R script is

                          dataf                <-                read.table('raw_data/mydata.txt',                header =                Truthful,                                            sep =                '                \t                ')                      

Because the file path raw_data/mydata.txt is relative to the project directory information technology doesn't thing where you collaborator saves the projection directory on their estimator information technology will still work.

If you weren't using an RStudio projection and so yous would have to use either of the options below neither of which would work on a dissimilar calculator.

                                          setwd("/Users/nhy163/Documents/Alex/Didactics/first_project/")                            dataf                <-                read.table("raw_data/mydata.txt",                header =                TRUE,                sep =                "                \t                ")                                            # or                                          dataf                <-                read.tabular array("/Users/nhy163/Documents/Alex/Teaching/first_project/raw_data/mydata.txt",                              header =                Truthful,                sep =                "                \t                ")                      

For those of you who desire to take the notion of relative file paths a step further, take a look at the here() function in the here package. The here() function allows you to automagically build file paths for any file relative to the project root directory that are besides operating system doubter (works on a Mac, Windows or Linux automobile). For example, to import our mydata.txt file from the raw_data directory just use

                                          library(here)                # yous may demand to install the hither packet commencement                            dataf                <-                read.table(here("raw_data",                "mydata.txt"),                                            header =                True,                sep =                '                \t                ',                              stringsAsFactors =                True)                                            # or without loading the here package                                          dataf                <-                read.table(hither::                here("raw_data",                "mydata.txt"),                                            header =                TRUE,                sep =                '                \t                ',                                            stringsAsFactors =                TRUE)