Difference between revisions of "RPR-Setup"

From "A B C"
Jump to navigation Jump to search
m
m
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
<div id="BIO">
+
<div id="ABC">
  <div class="b1">
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce; font-size:300%; font-weight:400; color: #000000; width:100%;">
 
Setup R to work with it
 
Setup R to work with it
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(R projects; working with git version control via RStudio; the history mechanism and why not to use it; .Rprofile to customize startup behaviour; the working directory.)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
R projects; working with git version control via RStudio; the history mechanism and why not to use it; .Rprofile to customize startup behaviour; the working directory
 
 
</div>
 
</div>
  
{{Vspace}}
+
{{Smallvspace}}
  
  
__TOC__
+
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce33; font-size:85%;">
 
+
<div style="font-size:118%;">
{{Vspace}}
+
<b>Abstract:</b><br />
 
+
<section begin=abstract />
 
+
This unit discusses the setup of a working session with RStudio.
{{DEV}}
+
<section end=abstract />
 
+
</div>
{{Vspace}}
+
<!-- ============================  -->
 
+
<hr>
 +
<table>
 +
<tr>
 +
<td style="padding:10px;">
 +
<b>Objectives:</b><br />
 +
This unit will ...
 +
* ... introduce R projects;
 +
* ... start working with git version control via RStudio;
 +
* ... discuss the history mechanism and why not to use it;
 +
* ... mention .Rprofile to customize startup behaviour; and
 +
* ... teach you to define the Working Directory of an R session.
 +
</td>
 +
<td style="padding:10px;">
 +
<b>Outcomes:</b><br />
 +
After working through this unit you ...
 +
* ... have verified that you can install R projects from GitHub;
 +
* ... know what the <code>.Rprofile</code> file is for;
 +
* ... can get and set the path of the current Working Directory.
  
 +
</td>
 +
</tr>
 +
</table>
 +
<!-- ============================  -->
 +
<hr>
 +
<b>Deliverables:</b><br />
 +
<section begin=deliverables />
 +
<li><b>Time management</b>: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.</li>
 +
<li><b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.</li>
 +
<li><b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].</li>
 +
<section end=deliverables />
 +
<!-- ============================  -->
 +
<hr>
 +
<section begin=prerequisites />
 +
<b>Prerequisites:</b><br />
 +
This unit builds on material covered in the following prerequisite units:<br />
 +
*[[RPR-Installation|RPR-Installation (Installing R and RStudio)]]
 +
<section end=prerequisites />
 +
<!-- ============================  -->
 
</div>
 
</div>
<div id="ABC-unit-framework">
 
== Abstract ==
 
<section begin=abstract />
 
<!-- included from "../components/RPR-Setup.components.wtxt", section: "abstract" -->
 
...
 
<section end=abstract />
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
== This unit ... ==
 
=== Prerequisites ===
 
<!-- included from "../components/RPR-Setup.components.wtxt", section: "prerequisites" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "notes-prerequisites" -->
 
You need to complete the following units before beginning this one:
 
*[[RPR-Installation]]
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
=== Objectives ===
+
__TOC__
<!-- included from "../components/RPR-Setup.components.wtxt", section: "objectives" -->
 
...
 
  
 
{{Vspace}}
 
{{Vspace}}
  
  
=== Outcomes ===
+
=== Evaluation ===
<!-- included from "../components/RPR-Setup.components.wtxt", section: "outcomes" -->
+
<b>Evaluation: NA</b><br />
...
+
<div style="margin-left: 2rem;">This unit is not evaluated for course marks.</div>
 +
== Contents ==
  
{{Vspace}}
+
=== Your Course Folder===
  
 +
Your Course Folder [[FND-Biocomputing_setup#Course_Folder|should already exist]].
  
=== Deliverables ===
+
Take note! When you write a Windows paths in an '''R''' command,  you have to use the "wrong" forward slash to separte directories and files. '''R''' will translate these "Unix-style"" paths into Windows-style paths automatically when it negotiates with the operating system. But the backslash is interpreted as an "escape" character that gives the character the follows it a special meaning.<ref>For example <code>C:Documents\new</code> would be interpreted as <code>C:Documents&lt;linebreak&gt;ew</code> because <code>\n</code> is the linebreak character. Even though that's actually the path name on Windows, in an R command you have to write <code>C:Documents/new</code></ref>
<!-- included from "../components/RPR-Setup.components.wtxt", section: "deliverables" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-time_management" -->
 
*<b>Time management</b>: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-journal" -->
 
*<b>Journal</b>: Document your progress in your [[FND-Journal|Course Journal]]. Some tasks may ask you to include specific items in your journal. Don't overlook these.
 
<!-- included from "ABC-unit_components.wtxt", section: "deliverables-insights" -->
 
*<b>Insights</b>: If you find something particularly noteworthy about this unit, make a note in your [[ABC-Insights|'''insights!''' page]].
 
  
{{Vspace}}
+
;Folder name and path examples
 +
*<span class="right"> <tt>/Users/Pierette/Documents/BCB420</tt></span>&nbsp;&nbsp;◁&nbsp;Looking good on a Mac.
 +
*<span class="right"> <tt>C:\Users\Pulcinella\Documents\CBW</tt></span>&nbsp;&nbsp;◁&nbsp;Looking good on a Windows computer.
 +
*<span class="right"> <tt>"C:/Users/Pulcinella/Documents/CBW"</tt></span>&nbsp;&nbsp;◁&nbsp;Looking good inside '''R''' on a Windows computer (note the quotation marks!).
  
  
=== Evaluation ===
+
*<span class="wrong"> <tt>C:\Users\Pantalone\Documents\BCH1441 (2017)</tt></span>&nbsp;&nbsp;◁&nbsp;Wrong. No special characters please.
<!-- included from "../components/RPR-Setup.components.wtxt", section: "evaluation" -->
+
*<span class="wrong"> <tt>/Users/Brighella/Documents/UofT Stuffz/Courses/more/Comp Sys biol. course</tt></span>&nbsp;&nbsp;◁&nbsp;Wrong. Please read instructions more carefully.
<!-- included from "ABC-unit_components.wtxt", section: "eval-none" -->
+
*<span class="wrong"> <tt>C:\Users\Tartaglia\Documents\KUWTK\&lt;Coursecode&gt;</tt></span>&nbsp;&nbsp;◁&nbsp;I can't even ...
<b>Evaluation: NA</b><br />
 
:This unit is not evaluated for course marks.
 
  
 
{{Vspace}}
 
{{Vspace}}
  
 
</div>
 
<div id="BIO">
 
== Contents ==
 
<!-- included from "../components/RPR-Setup.components.wtxt", section: "contents" -->
 
 
==="Projects"===
 
==="Projects"===
  
Line 110: Line 119:
 
* Enter <code><nowiki>https://github.com/hyginn/R_Exercise-BasicSetup</nowiki></code> as the Repository URL.
 
* Enter <code><nowiki>https://github.com/hyginn/R_Exercise-BasicSetup</nowiki></code> as the Repository URL.
 
* Type a <code>&lt;tab&gt;</code> character, the '''Project directory name''' field should then autofill to read <code>R_Exercise-BasicSetup</code>
 
* Type a <code>&lt;tab&gt;</code> character, the '''Project directory name''' field should then autofill to read <code>R_Exercise-BasicSetup</code>
* Click on '''Browse...''' to find your project directory. (The one that you have created above). Click '''Open'''.
+
* Click on '''Browse...''' to find your Course Folder. (The one that you have [[FND-Biocomputing_setup#Course_Folder|already created]]). Click '''Open'''.
 
* Click '''Create Project'''; the project files should be downloaded and the console should prompt you to type <code>init()</code> to begin.
 
* Click '''Create Project'''; the project files should be downloaded and the console should prompt you to type <code>init()</code> to begin.
 
* Type <code>init()</code> into the console pane.
 
* Type <code>init()</code> into the console pane.
Line 144: Line 153:
 
;I get an error message like "directory exists and is not empty".
 
;I get an error message like "directory exists and is not empty".
  
:A directory with the name of the project already exists in the location in which you are asking RStudio to create the project. Either delete the existing directory, or install the project into a different parent directory.
+
:A directory with the name of the project already exists in the location in which you are asking RStudio to create the project (the Course Folder). Either delete the existing directory, or install the project into a different parent directory.
  
  
Line 166: Line 175:
  
  
To locate a file in a computer, one has to specify the ''filename'' and the directory in which the file is stored; this is also called the ''path'' of the file. The "working directory" for '''R''' is either the directory in which the '''R'''-program has been installed, or some other directory, as initialized by a startup script. You can execute the command <code>getwd()</code> to list what the "Working Directory" is currently set to:
+
To locate a file in a computer, one has to specify the ''filename'' and the directory in which the file is stored; this is also called the ''path'' of the file. However R uses a default "working directory"", which is assumed if no path is specified. This "working directory" for '''R''' is either the directory in which the '''R'''-program has been installed, or some other directory, that has been defined in a startup script, or specifically defined with the command <code>setwd("<working Directory>")</code> at any time. You can execute the command <code>getwd()</code> to list what the Working Directory is <b>currently</b> set to:
  
  
<source lang="rsplus">
+
<pre>
 
> getwd()
 
> getwd()
 
[1] "/Users/steipe/R"
 
[1] "/Users/steipe/R"
</source>
+
</pre>
  
In RStudio, the contents of the working directory is listed in the Files Pane.
+
In RStudio, the contents of the working directory is listed in the Files Pane (lower-right).
  
It is convenient to put all your '''R'''-input and output files into a project specific directory and then define this to be the "Working Directory". The '''R''' working directory is the directory that '''R''' uses when you don't specify a path. Think of it as the default directory. Use the {{c|setwd()}} command for this. {{c|setwd()}} requires an ''argument'' that you type between the parentheses: a string with the directory path, or a variable containing such a string. Strings in R are delimited with {{c|"}} or <code>'</code> characters. If the directory does not exist, an <span style="color:#EE0000;">Error</span> will be reported. Make sure you have created the directory. On Mac and Unix systems, the usual shorthand notation for relative paths can be used: <code>~</code> for the home directory, <code>.</code> for the current directory, <code>..</code> for the parent of the current directory.
+
It is convenient to put all your '''R'''-input and output files into a project specific directory and then define this to be the "Working Directory". Use the {{c|setwd()}} command for this. {{c|setwd()}} requires an ''argument'' that you type between the parentheses: a string with the directory path, or a variable containing such a string. Strings in R are delimited with {{c|"}} or <code>'</code> characters. If the directory does not exist, an <span style="color:#EE0000;">Error</span> will be reported. Make sure you have created the directory. On Mac and Unix systems, the usual shorthand notation for relative paths can be used: <code>~</code> for the home directory, <code>.</code> for the current directory, <code>..</code> for the parent of the current directory.
  
If you use a '''windows''' system, you need know that backslashes &ndash; "\" &ndash; have a special meaning for '''R''', they work as ''escape characters''. For example the string "\n" means ''newline'', and "\t" means ''tab''. Thus '''R''' gets confused when you put backslashes into string literals, such as <u>Windows path names</u>. '''R''' has a simple solution: you simply use forward slashes instead of backslashes when you specify paths, and '''R''' will translate them correctly when it talks to your operating system. Instead of <code>C:\documents\projectfiles</code> you write <code>C:/documents/projectfiles</code>. Also note that on Windows the <code>~</code> tilde is a shorthand for the directory in which '''R''' is installed, not the user's home directory.
+
If you use a '''Windows''' system, you need know that backslashes &ndash; "\" &ndash; have a special meaning for '''R''', they work as ''escape characters''. For example the string "\n" means ''newline'', and "\t" means ''tab''. Thus '''R''' gets confused when you put backslashes into string literals, such as <u>Windows path names</u>. '''R''' has a simple solution: you simply use forward slashes instead of backslashes when you specify paths, and '''R''' will translate them correctly when it talks to your operating system. Instead of <code>C:\documents\projectfiles</code> you write <code>C:/documents/projectfiles</code>. Also note that on Windows the <code>~</code> tilde is a shorthand for the directory in which '''R''' is installed, not the user's home directory.
  
  
{{console|My home directory...
+
<small>My home directory...</small>
|> setwd("~") # Note: ~ is the "tilde" - the squiggly line - not the straight hyphen
+
<pre>
 +
> setwd("~") # Note: ~ is the "tilde" - the squiggly line - not the straight hyphen
 
> getwd()
 
> getwd()
 
[1] "/Users/steipe"
 
[1] "/Users/steipe"
}}
+
</pre>
 +
 
  
{{console|Relative path: home directory, up one level, then down into chen's home directory)
+
<small>Relative path: home directory, up one level, then down into chen's home directory)</small>
|> setwd("~/../chen")
+
<pre>
 +
> setwd("~/../chen")
 
> getwd()
 
> getwd()
 
[1] "/Users/chen"
 
[1] "/Users/chen"
}}
+
</pre>
 +
 
  
{{console|Absolute path: specify the entire string)
+
<small>Absolute path: specify the entire string)</small>
|> setwd("/Users/steipe/abc/R_samples")
+
<pre>
 +
> setwd("/Users/steipe/abc/R_samples")
 
> getwd()
 
> getwd()
 
[1] "Users/steipe/abc/R_samples"
 
[1] "Users/steipe/abc/R_samples"
}}
+
</pre>
  
  
In RStudio you can use the '''Session &rarr; Set Working Directory''' menu. This includes the useful option to set the current project directory as the working directory.
+
In RStudio you can use the '''Session &rarr; Set Working Directory''' menu. This includes the useful option to set the current project directory as the working directory<ref>Projects that I create for teaching are configured to use this option by default, thus once the project is loaded, the Working Directory should already be correctly set.</ref>.
  
  
Line 207: Line 221:
 
Since you have gone through the script of the ''BasicSetup'' project, your working directory should be set to this project directory (I have configured the project to do this automatically.)
 
Since you have gone through the script of the ''BasicSetup'' project, your working directory should be set to this project directory (I have configured the project to do this automatically.)
 
# Figure out the path to its parent directory - i.e. the course- or workshop directory you created at the beginning.
 
# Figure out the path to its parent directory - i.e. the course- or workshop directory you created at the beginning.
# Use {{c|setwd("&lt;your/path/and/directory/name&gt;")}} to set the working directory to the course directory.
+
# Use {{c|setwd("&lt;your/path/and/directory/name&gt;")}} to set the Working Directory to the Course Folder.
 
# Confirm that this has worked by typing {{c|getwd()}} and {{c|list.files()}}.
 
# Confirm that this has worked by typing {{c|getwd()}} and {{c|list.files()}}.
 
}}
 
}}
Line 218: Line 232:
  
  
Often, when working on a project, you would like to start off in your working directory right away when you start up '''R''', instead of typing the <code>setwd()</code> command. This is easily done in a special '''R'''-script that is executed automatically on startup<ref>Actually, the first script that runs is '''Rprofile.site''' which is found on Linux and Windows machines in the <code>C:\Program Files\R\R-{version}\etc</code> directory. But not on Macs.</ref>. The name of the script is <code>.Rprofile</code> and '''R''' expects to find it in the user's home directory. You can edit these files with a simple text editor like Textedit (Mac), Notepad (windows) or Gedit (Linux) - or, of course, by opening it in '''R''' itself<ref>Operating systems commonly hide files whose name starts with a period "." from normal directory listings. '''All''' files however are displayed in RStudio's File pane. Nevertheless, it is useful to know how to view such files by default. On Macs, you can configure the Finder to show you such "hidden files" by default. To do this:
+
Often, when working on a project, you would like to start off in your working directory right away when you start up '''R''', instead of typing the <code>setwd()</code> command. This is easily done in a special '''R'''-script that is executed automatically on startup<ref>Actually, the first script that runs is '''Rprofile.site''' which is found on Linux and Windows machines in the <code>C:\Program Files\R\R-{version}\etc</code> directory. But not on Macs.</ref>. The name of the script is <code>.Rprofile</code> and '''R''' expects to find it in the user's home directory. You can edit these files with a simple text editor like Textedit (Mac), Notepad (windows) or Gedit (Linux) - or, of course, by opening it in RStudio - don't forget that a code editor is also a text editor<ref>Operating systems commonly hide files whose name starts with a period "." from normal directory listings. '''All''' files however are displayed in RStudio's File pane. Nevertheless, it is useful to know how to view such files by default. On Macs, you can configure the Finder to show you such "hidden files" by default. To do this:
 
(i) Open a terminal window; (ii) Type: <code>$defaults write com.apple.Finder AppleShowAllFiles YES</code> (iii) Restart the Finder by accessing '''Force quit''' (under the Apple menu), selecting the Finder and clicking '''Relaunch'''. (iV) If you ever want to revert this, just do the same thing but set the default to <code>NO</code> instead.</ref>.
 
(i) Open a terminal window; (ii) Type: <code>$defaults write com.apple.Finder AppleShowAllFiles YES</code> (iii) Restart the Finder by accessing '''Force quit''' (under the Apple menu), selecting the Finder and clicking '''Relaunch'''. (iV) If you ever want to revert this, just do the same thing but set the default to <code>NO</code> instead.</ref>.
  
Line 228: Line 242:
  
 
For more details, use '''R''''s help function:
 
For more details, use '''R''''s help function:
<source lang="rsplus">
+
<pre>
 
> ?Startup
 
> ?Startup
  
</source>
+
</pre>
 +
 
 +
{{Vspace}}
 +
 
 +
{{task|1=
 +
Just for information:
 +
* locate the .Rprofile file in the RStudio file pane;
 +
* click on it to open it in the text-editing window.
 +
 
 +
This way you could change it and save the changes. However, don't do that now but
 +
* Close the file again.
 +
}}
 +
 
  
 
{{Vspace}}
 
{{Vspace}}
Line 238: Line 264:
  
  
During an '''R''' session, you might define a large number of variables, data structures, load packages and scripts etc. All of this information is stored in the so-called "Workspace". When you quit '''R''' you have the option to save the Workspace; it will then be restored in your next session. However, restoring the Workspace from a previous state is potentially a bad idea: if you load data or variables in a startup script, they may be overwritten with a corrupted version that you happened to save in the workspace when you last quit. This is very hard to troubleshoot. Essentially, when you save and reload your Workspace habitually, you have overlapping and potentially conflicting behaviour of startup script and Workplace restore.
+
During an '''R''' session, you might define a large number of R-objects: variables, data structures, functions etc., and you might load packages and scripts. All of this information is stored in the so-called "Workspace". When you quit '''R''' you have the option to save the Workspace; it will then be restored in your next session. Now, you might think: how convenient - I can just stop R, and when I restart it, it will go into the same state as it was. But no. Restoring the Workspace from a previous state is actually a bad idea: if you load data or variables in a startup script, they may be overwritten with a corrupted version that you happened to save in the workspace when you last quit. This is very hard to troubleshoot. Essentially, when you save and reload your Workspace habitually, you have overlapping and potentially conflicting behaviour of startup script and Workspace restore.
  
What I prefer and recommend instead is the following:
+
What I recommend instead is the following:
 
* Never save the Workspace.
 
* Never save the Workspace.
 
* Always work from scripts.
 
* Always work from scripts.
 
* Write your scripts so that you can easily recreate all objects you need to continue your analysis.
 
* Write your scripts so that you can easily recreate all objects you need to continue your analysis.
* If some objects are expensive to compute, you can always <code>save()</code> and later <code>load()</code> them explicitly. In fact, restoring the Workspace does the same thing, but you have less control regarding whether the version of your objects are correct, and what temporary variables may be loaded as well.
+
* If some objects are expensive to compute, you can always <code>save()</code> and later <code>load()</code> them explicitly (or better: use <code>saveRDS()</code> and <code>readRDS()</code> ... why?). In fact, restoring the Workspace does the same thing, but you have less control regarding whether the version of your objects are correct, and what temporary variables may be loaded as well.
 
* In this way, you work with '''explicit''' instructions, not '''implicit''' behaviour.
 
* In this way, you work with '''explicit''' instructions, not '''implicit''' behaviour.
 
* Explicit beats implicit.
 
* Explicit beats implicit.
  
  
{{console|List the current workspace contents: initially it is empty. (R reports an object of type "character" with a length of 0.)
+
<small>List the current workspace contents: initially it only contains the <code>init()</code> function that was loaded from the .Rprofile script on startup. </small>
|> ls()
+
<pre>
character(0)
+
> ls()
 +
[1] "init"
 
>
 
>
}}
+
</pre>
 +
 
  
{{console|Initialize three variables
+
<small>Initialize three variables</small>
|> a <- 3
+
<pre>
|> b <- 4
+
> a <- 3
|> c <- sqrt(a^2 +b^2)
+
> b <- 4
 +
> c <- sqrt(a^2 +b^2)
 
> ls()
 
> ls()
[1] "a"  "b"  "c"
+
[1] "a"  "b"  "c"  "init"
 
>
 
>
}}
+
</pre>
  
{{console|Save one item in an .RData file.
+
<small>Save one item in an .RData file.</small>
|> save(a, file = "tmp.RData")
+
<pre>
}}
+
> save(a, file = "tmp.RData")
 +
</pre>
  
{{console|Remove one item from the Workspace. (Note: the argument for <code>rm()</code> is not the string "''a''", but the variable name ''a''. No quotation marks!)
+
<small>Remove one item from the Workspace. (Note: the argument for <code>rm()</code> is not the string "''a''", but the variable name ''a''. No quotation marks!)</small>
|> rm(a)
+
<pre>
 +
> rm(a)
 
> ls()
 
> ls()
[1] "b"  "c"
+
[1] "b"  "c"  "init"
 
>
 
>
}}
+
</pre>
  
{{console|Load what you previously saved.
+
<small>Load what you previously saved.</small>
|> load("tmp.RData")
+
<pre>
 +
> load("tmp.RData")
 
> ls()
 
> ls()
[1] "a"  "b"  "c"
+
[1] "a"  "b"  "c"  "init"
}}
+
</pre>
  
  
Note: you can <code>save()</code> more than one item in an .RData file. When you then <code>load()</code> the file, all of the objects it contains are loaded. You don't '''assign''' the objects - they are being '''restored'''.
+
Note: you can <code>save()</code> more than one item in an .RData file. When you then <code>load()</code> the file, all of the objects it contains are loaded. You don't '''assign''' these objects - they are being '''restored'''.
  
  
 
<small>We can use the output of {{c|ls()}} as input to {{c|rm()}} to remove all items from the workspace. (cf. {{c|?rm}} for details)</small>
 
<small>We can use the output of {{c|ls()}} as input to {{c|rm()}} to remove all items from the workspace. (cf. {{c|?rm}} for details)</small>
<source lang="rsplus">
+
<pre>
 
rm(list = ls())
 
rm(list = ls())
 
> ls()
 
> ls()
 
character(0)
 
character(0)
 
>
 
>
</source>
+
</pre>
 
 
  
The contents of the workspace is displayed in RStudio's Environment Pane (top-right). You can see a little "brush" icon at the top that you can click to remove all items from the workspace.
 
  
{{Vspace}}
+
The contents of the workspace is displayed in RStudio's Environment Pane (top-right). You can see a little "broom" icon at the top that you can click to remove all items from the workspace.
  
  
Line 303: Line 333:
  
  
== Further reading, links and resources ==
 
<!-- {{#pmid: 19957275}} -->
 
<!-- {{WWW|WWW_GMOD}} -->
 
<!-- <div class="reference-box">[http://www.ncbi.nlm.nih.gov]</div> -->
 
 
{{Vspace}}
 
 
 
== Notes ==
 
<!-- included from "../components/RPR-Setup.components.wtxt", section: "notes" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "notes" -->
 
<references />
 
 
{{Vspace}}
 
 
 
</div>
 
<div id="ABC-unit-framework">
 
 
== Self-evaluation ==
 
== Self-evaluation ==
<!-- included from "../components/RPR-Setup.components.wtxt", section: "self-evaluation" -->
 
 
<!--
 
<!--
 
=== Question 1===
 
=== Question 1===
Line 339: Line 350:
  
 
-->
 
-->
 +
== Further reading, links and resources ==
 +
<!-- {{#pmid: 19957275}} -->
 +
<!-- {{WWW|WWW_GMOD}} -->
 +
<!-- <div class="reference-box">[http://www.ncbi.nlm.nih.gov]</div> -->
 +
== Notes ==
 +
<references />
  
 
{{Vspace}}
 
{{Vspace}}
  
 
 
{{Vspace}}
 
 
 
<!-- included from "ABC-unit_components.wtxt", section: "ABC-unit_ask" -->
 
 
----
 
 
{{Vspace}}
 
 
<b>If in doubt, ask!</b> If anything about this learning unit is not clear to you, do not proceed blindly but ask for clarification. Post your question on the course mailing list: others are likely to have similar problems. Or send an email to your instructor.
 
 
----
 
 
{{Vspace}}
 
  
 
<div class="about">
 
<div class="about">
Line 367: Line 368:
 
:2017-08-05
 
:2017-08-05
 
<b>Modified:</b><br />
 
<b>Modified:</b><br />
:2017-08-05
+
:2018-05-02
 
<b>Version:</b><br />
 
<b>Version:</b><br />
:0.1
+
:1.1.1
 
<b>Version history:</b><br />
 
<b>Version history:</b><br />
*0.1 First stub
+
*1.1.1 Maintenance
 +
*1.1 Fixed display bug with "=" in template code; moved to GeSHi formatting.
 +
*1.0 Completed to first live version
 +
*0.1 Material collected from previous tutorial
 
</div>
 
</div>
[[Category:ABC-units]]
 
<!-- included from "ABC-unit_components.wtxt", section: "ABC-unit_footer" -->
 
  
 
{{CC-BY}}
 
{{CC-BY}}
  
 +
[[Category:ABC-units]]
 +
{{UNIT}}
 +
{{LIVE}}
 
</div>
 
</div>
 
<!-- [END] -->
 
<!-- [END] -->

Latest revision as of 09:29, 25 September 2020

Setup R to work with it

(R projects; working with git version control via RStudio; the history mechanism and why not to use it; .Rprofile to customize startup behaviour; the working directory.)


 


Abstract:

This unit discusses the setup of a working session with RStudio.


Objectives:
This unit will ...

  • ... introduce R projects;
  • ... start working with git version control via RStudio;
  • ... discuss the history mechanism and why not to use it;
  • ... mention .Rprofile to customize startup behaviour; and
  • ... teach you to define the Working Directory of an R session.

Outcomes:
After working through this unit you ...

  • ... have verified that you can install R projects from GitHub;
  • ... know what the .Rprofile file is for;
  • ... can get and set the path of the current Working Directory.

Deliverables:

  • Time management: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.
  • Journal: Document your progress in your Course Journal. Some tasks may ask you to include specific items in your journal. Don't overlook these.
  • Insights: If you find something particularly noteworthy about this unit, make a note in your insights! page.

  • Prerequisites:
    This unit builds on material covered in the following prerequisite units:


     



     



     


    Evaluation

    Evaluation: NA

    This unit is not evaluated for course marks.

    Contents

    Your Course Folder

    Your Course Folder should already exist.

    Take note! When you write a Windows paths in an R command, you have to use the "wrong" forward slash to separte directories and files. R will translate these "Unix-style"" paths into Windows-style paths automatically when it negotiates with the operating system. But the backslash is interpreted as an "escape" character that gives the character the follows it a special meaning.[1]

    Folder name and path examples
    • /Users/Pierette/Documents/BCB420  ◁ Looking good on a Mac.
    • C:\Users\Pulcinella\Documents\CBW  ◁ Looking good on a Windows computer.
    • "C:/Users/Pulcinella/Documents/CBW"  ◁ Looking good inside R on a Windows computer (note the quotation marks!).


    • C:\Users\Pantalone\Documents\BCH1441 (2017)  ◁ Wrong. No special characters please.
    • /Users/Brighella/Documents/UofT Stuffz/Courses/more/Comp Sys biol. course  ◁ Wrong. Please read instructions more carefully.
    • C:\Users\Tartaglia\Documents\KUWTK\<Coursecode>  ◁ I can't even ...


     

    "Projects"

    We will make extensive use of "projects" in class. Read more about projects in RStudio here.


     

    Git Version control

    We will also make extensive use of version control. In fact, we will now load a project via Git version control from its free, public repository on GitHub.

    Task:


     

    Then do the following:

    • open RStudio
    • Select File → NewProject...
    • Click on Version Control
    • Click on Git
    • Enter https://github.com/hyginn/R_Exercise-BasicSetup as the Repository URL.
    • Type a <tab> character, the Project directory name field should then autofill to read R_Exercise-BasicSetup
    • Click on Browse... to find your Course Folder. (The one that you have already created). Click Open.
    • Click Create Project; the project files should be downloaded and the console should prompt you to type init() to begin.
    • Type init() into the console pane.

    An R script should load.

    • Explore the script and follow its instructions.


     

    What could possibly go wrong?...


    I get an error message
    "Git not found".
    The simplest reason is that you may have had RStudio open while installing git. Just restart RStudio.
    The executable for Git (the Git "program" - "git.exe" on Windows, "git" elsewhere) needs to be on your system's path, or correctly specified in RStudio's options. The correct "path" to Git will depend on your operating system, and how git was installed. To find where git is installed –
    On Mac and Unix systems, open a Terminal window[2] and type which git. This will either print the path (Yay), or tell you that git is not found. The latter could have two reasons: either git has no been installed in the first place, or it has been installed in a non-standard location by whatever installation manager you have used. Ask Google to help you figure out how to solve your specific case.
    On Windows you can find the location of the executable by searching "git.exe" in your "programs and files". Once it's been found, right click on it and select "Open file location" from the options. It might be in C:\Program Files\Git\cmd\git.exe but the exact location depends on your operating system.
    Once you know the path to your git executable, open FilePreferences, click on the Git/SVN option, click on the Browse button, and find the correct folder. On Macs you may need to click <shift> <command> G to open the "Go to ..." dialogue, then type the top-folder of the path (e.g. /usr) and click your way down to folder where the program lives. Find the installation directory and select git.exe. Then click "ok".
    Then try again to create the project and let us know what happened in case it still did not work.


     
    I get an error message like "directory exists and is not empty".
    A directory with the name of the project already exists in the location in which you are asking RStudio to create the project (the Course Folder). Either delete the existing directory, or install the project into a different parent directory.


     
    The git icon has disappeared.
    I have seen this happen when somehow the path to git has changed.
    (A) Make sure the correct path to git is set in your FilePreferences → Git/SVN.
    (B) Open ToolsProject options...Git/SVN. Next to Version control system git must be selected, not (None). If it is (None), change this to git. If that's not an option, the path is not correct. Go back to (A).
    (C) I think you may need to restart RStudio then and reload your project via the Files → Recent projects... menu for the git icon and the version control options to reappear.



     

    Working directory

    To locate a file in a computer, one has to specify the filename and the directory in which the file is stored; this is also called the path of the file. However R uses a default "working directory"", which is assumed if no path is specified. This "working directory" for R is either the directory in which the R-program has been installed, or some other directory, that has been defined in a startup script, or specifically defined with the command setwd("<working Directory>") at any time. You can execute the command getwd() to list what the Working Directory is currently set to:


    > getwd()
    [1] "/Users/steipe/R"
    

    In RStudio, the contents of the working directory is listed in the Files Pane (lower-right).

    It is convenient to put all your R-input and output files into a project specific directory and then define this to be the "Working Directory". Use the setwd() command for this. setwd() requires an argument that you type between the parentheses: a string with the directory path, or a variable containing such a string. Strings in R are delimited with " or ' characters. If the directory does not exist, an Error will be reported. Make sure you have created the directory. On Mac and Unix systems, the usual shorthand notation for relative paths can be used: ~ for the home directory, . for the current directory, .. for the parent of the current directory.

    If you use a Windows system, you need know that backslashes – "\" – have a special meaning for R, they work as escape characters. For example the string "\n" means newline, and "\t" means tab. Thus R gets confused when you put backslashes into string literals, such as Windows path names. R has a simple solution: you simply use forward slashes instead of backslashes when you specify paths, and R will translate them correctly when it talks to your operating system. Instead of C:\documents\projectfiles you write C:/documents/projectfiles. Also note that on Windows the ~ tilde is a shorthand for the directory in which R is installed, not the user's home directory.


    My home directory...

    > setwd("~") # Note: ~ is the "tilde" - the squiggly line - not the straight hyphen
    > getwd()
    [1] "/Users/steipe"
    


    Relative path: home directory, up one level, then down into chen's home directory)

    > setwd("~/../chen")
    > getwd()
    [1] "/Users/chen"
    


    Absolute path: specify the entire string)

    > setwd("/Users/steipe/abc/R_samples")
    > getwd()
    [1] "Users/steipe/abc/R_samples"
    


    In RStudio you can use the Session → Set Working Directory menu. This includes the useful option to set the current project directory as the working directory[3].


    Task:
    Since you have gone through the script of the BasicSetup project, your working directory should be set to this project directory (I have configured the project to do this automatically.)

    1. Figure out the path to its parent directory - i.e. the course- or workshop directory you created at the beginning.
    2. Use setwd("<your/path/and/directory/name>") to set the Working Directory to the Course Folder.
    3. Confirm that this has worked by typing getwd() and list.files().

    The Working Directory functions can also be accessed through the Menu, under Misc.


     

    .Rprofile - startup commands

    Often, when working on a project, you would like to start off in your working directory right away when you start up R, instead of typing the setwd() command. This is easily done in a special R-script that is executed automatically on startup[4]. The name of the script is .Rprofile and R expects to find it in the user's home directory. You can edit these files with a simple text editor like Textedit (Mac), Notepad (windows) or Gedit (Linux) - or, of course, by opening it in RStudio - don't forget that a code editor is also a text editor[5].

    Besides setting the working directory, other items that might go into such a file could be

    • libraries that you often use
    • constants that are not automatically defined
    • functions that you would like to preload.


    For more details, use R's help function:

    > ?Startup
    
    


     

    Task:
    Just for information:

    • locate the .Rprofile file in the RStudio file pane;
    • click on it to open it in the text-editing window.

    This way you could change it and save the changes. However, don't do that now but

    • Close the file again.


     

    The "Workspace"

    During an R session, you might define a large number of R-objects: variables, data structures, functions etc., and you might load packages and scripts. All of this information is stored in the so-called "Workspace". When you quit R you have the option to save the Workspace; it will then be restored in your next session. Now, you might think: how convenient - I can just stop R, and when I restart it, it will go into the same state as it was. But no. Restoring the Workspace from a previous state is actually a bad idea: if you load data or variables in a startup script, they may be overwritten with a corrupted version that you happened to save in the workspace when you last quit. This is very hard to troubleshoot. Essentially, when you save and reload your Workspace habitually, you have overlapping and potentially conflicting behaviour of startup script and Workspace restore.

    What I recommend instead is the following:

    • Never save the Workspace.
    • Always work from scripts.
    • Write your scripts so that you can easily recreate all objects you need to continue your analysis.
    • If some objects are expensive to compute, you can always save() and later load() them explicitly (or better: use saveRDS() and readRDS() ... why?). In fact, restoring the Workspace does the same thing, but you have less control regarding whether the version of your objects are correct, and what temporary variables may be loaded as well.
    • In this way, you work with explicit instructions, not implicit behaviour.
    • Explicit beats implicit.


    List the current workspace contents: initially it only contains the init() function that was loaded from the .Rprofile script on startup.

    > ls()
     [1] "init"
    >
    


    Initialize three variables

    > a <- 3
    > b <- 4
    > c <- sqrt(a^2 +b^2)
    > ls()
    [1] "a"   "b"   "c"   "init"
    >
    

    Save one item in an .RData file.

    > save(a, file = "tmp.RData")
    

    Remove one item from the Workspace. (Note: the argument for rm() is not the string "a", but the variable name a. No quotation marks!)

    > rm(a)
    > ls()
    [1] "b"   "c"   "init"
    >
    

    Load what you previously saved.

    > load("tmp.RData")
    > ls()
    [1] "a"   "b"   "c"   "init"
    


    Note: you can save() more than one item in an .RData file. When you then load() the file, all of the objects it contains are loaded. You don't assign these objects - they are being restored.


    We can use the output of ls() as input to rm() to remove all items from the workspace. (cf. ?rm for details)

    rm(list = ls())
    > ls()
    character(0)
    >
    


    The contents of the workspace is displayed in RStudio's Environment Pane (top-right). You can see a little "broom" icon at the top that you can click to remove all items from the workspace.



     


    Self-evaluation

    Further reading, links and resources

    Notes

    1. For example C:Documents\new would be interpreted as C:Documents<linebreak>ew because \n is the linebreak character. Even though that's actually the path name on Windows, in an R command you have to write C:Documents/new
    2. The Terminal app is in the Utilities sub-folder of your Applications folder.
    3. Projects that I create for teaching are configured to use this option by default, thus once the project is loaded, the Working Directory should already be correctly set.
    4. Actually, the first script that runs is Rprofile.site which is found on Linux and Windows machines in the C:\Program Files\R\R-{version}\etc directory. But not on Macs.
    5. Operating systems commonly hide files whose name starts with a period "." from normal directory listings. All files however are displayed in RStudio's File pane. Nevertheless, it is useful to know how to view such files by default. On Macs, you can configure the Finder to show you such "hidden files" by default. To do this: (i) Open a terminal window; (ii) Type: $defaults write com.apple.Finder AppleShowAllFiles YES (iii) Restart the Finder by accessing Force quit (under the Apple menu), selecting the Finder and clicking Relaunch. (iV) If you ever want to revert this, just do the same thing but set the default to NO instead.


     


    About ...
     
    Author:

    Boris Steipe <boris.steipe@utoronto.ca>

    Created:

    2017-08-05

    Modified:

    2018-05-02

    Version:

    1.1.1

    Version history:

    • 1.1.1 Maintenance
    • 1.1 Fixed display bug with "=" in template code; moved to GeSHi formatting.
    • 1.0 Completed to first live version
    • 0.1 Material collected from previous tutorial

    CreativeCommonsBy.png This copyrighted material is licensed under a Creative Commons Attribution 4.0 International License. Follow the link to learn more.