First, watch the video, then read about the projects and R working directories. The video demonstrates one of the ways you can efficiently manage your files in a project. The discussed file structure will work in many cases but may need to be revised when large data are used and it is impossible or impractical to move the data to the local data folders. Also, the video assumes that each script file (for data loading, cleaning, plotting, etc.) is relatively large; hence it makes sense to keep the code separately so it is more manageable. Suppose each file (for data loading, cleaning, visualizing, and statistical analysis) contains just a few lines. In that case, it might be more practical to keep the codes together in a single script – you are free to decide based on the needs and size of your project.
Paths and Directories
Paths and directories are a little complicated because there are two basic styles of paths: Mac/Linux and Windows. There are three chief ways in which they differ:
-
The most important difference is how you separate the components of the path. Mac and Linux uses slashes (e.g.
plots/diamonds.pdf
) and Windows uses backslashes (e.g.plots\diamonds.pdf
). R can work with either type (no matter what platform you're currently using), but unfortunately, backslashes mean something special to R, and to get a single backslash in the path, you need to type two backslashes! That makes life frustrating, so I recommend always using the Linux/Mac style with forward slashes. -
Absolute paths (i.e. paths that point to the same place regardless of your working directory) look different. In Windows they start with a drive letter (e.g.
C:
) or two backslashes (e.g.\\servername
) and in Mac/Linux they start with a slash "/" (e.g./users/hadley
). You should never use absolute paths in your scripts, because they hinder sharing: no one else will have exactly the same directory configuration as you. -
The last minor difference is the place that
~
points to.~
is a convenient shortcut to your home directory. Windows doesn't really have the notion of a home directory, so it instead points to your documents directory.