I strongly recommend that when working in R that you work within the framework of RStudio project. That way you divide your “work into multiple contexts, each with their own working directory, workspace, history, and source documents”.
To start a new project do: “File” > “New Project” … and select either “New Directory” or “Exisiting Directory”.
To open up an existing project do: “File” > “Open Project …” or “File” > “Recent Projects”
For detailed information see Using Projects.
I strongly recommend that within each project you keep similar directory structure. What structure you adopt is based on personal preference. My approach has been to have the following standard directories:
read
-function. These data may be untidy and/or unclean. You may want to have subdirectory within this directories, e.g. if you have a large project with disparate data sources.
source
-function (see further discussion below).I then generally used the root directory of a project to store R markdown source documents (document with the extension .Rmd). As an example take a look at the directory structure of the project upon which these webpages you are reading are based (crfmr).
R being primarily a Scripting language one does not access files through use of the mouse-click approch. That is if one wants to stick with having reproducable code.
Within R there is always a specified working directory. The current working directory can always be obtained by the getwd
-function:
getwd()
## [1] "/net/hafkaldi/export/home/haf/einarhj/prj2/crfmr"
Now to get information of what is in the R working directory one can use the dir
-function:
dir()
## [1] "calendar_plot.R" "catch_and_effort.Rmd"
## [3] "cleaning.Rmd" "crfm_import_export.Rmd"
## [5] "crfmr.Rproj" "data-raw"
## [7] "dplyr_old.Rmd" "dplyr.Rmd"
## [9] "dynamic_reports.Rmd" "food_for_thought.Rmd"
## [11] "ggplot2.Rmd" "glm.Rmd"
## [13] "img" "importing_excel_data_101.Rmd"
## [15] "importing.Rmd" "index.Rmd"
## [17] "installing.Rmd" "last_day_meeting_notes.Rmd"
## [19] "mapping_vms.Rmd" "modelr.Rmd"
## [21] "monthly_landing_report.Rmd" "more_grammar.Rmd"
## [23] "not_used" "R"
## [25] "README.md" "README.Rmd"
## [27] "rep" "rstudio_projects.html"
## [29] "rstudio_projects.Rmd" "simulated_case.Rmd"
## [31] "stvincent.Rmd" "tidyr_case1.Rmd"
## [33] "tidyr_case2.Rmd" "tidyr_introduction.Rmd"
The equivalent path from the computer root directory (in windose that is normally mapped as “C:”) would be (not run):
dir("/home/haf/einarhj/prj2/crfmr")
Within R one can “move around” using either _absolute or relative path. E.g if one wants to get information of what is in a sub-directory one can do (that is an absolute path - not run):
dir("/home/haf/einarhj/prj2/crfmr/data-raw")
The equivalent relative path from current R working directory is then:
dir("data-raw")
## [1] "all_length_data.xls" "artfish" "artfish_tidy.xlsx"
## [4] "artfish.xlsx" "crfm" "example-ca.csv"
## [7] "excel_example.xlsx" "iccat_codes.xlsx" "logbook.xlsx"
## [10] "stvincent" "suriname" "tidyr"
## [13] "tidyr.xlsx"
If one wants to obtain what is in the directory above the current R working (absolute path - not run):
dir("/home/haf/einarhj/prj2")
The equivalent relative path given the current R working directory is obtained by the use a double dot (“..”):
dir("..")
## [1] "af_afli" "campana" "crfm2017"
## [4] "crfm2017_old" "crfmr" "crfmr_20170210"
## [7] "datras" "doodle" "pax_calculations"
## [10] "paxdown" "prj2.Rproj" "splatter"
## [13] "splatter1" "vms"
One could get information of what is in two directories up from the current R working directory using (relative path):
dir("../..")
## [1] "all_other_stuff" "ass" "bin"
## [4] "Burocratics" "cronjobs" "db"
## [7] "Desktop" "Documents" "Downloads"
## [10] "Dropbox" "education" "fishvice"
## [13] "linux" "Mail" "Music"
## [16] "personal" "Pictures" "prj"
## [19] "prj2" "public_html" "r"
## [22] "reikn" "src" "stasi"
## [25] "User" "users"
Which is the same using the absolute path (not run):
dir("/home/haf/einarhj")
to be added …
to be added …