Difference between revisions of "RPR-Console"

From "A B C"
Jump to navigation Jump to search
m (Created page with "<div id="BIO"> <div class="b1"> Using the R console </div> {{Vspace}} <div class="keywords"> <b>Keywords:</b>  The console, arrow keys, ctrl-enter to execute co...")
 
m
 
(7 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%;">
Using the R console
+
Console and scripts
  </div>
+
<div style="padding:5px; margin-top:20px; margin-bottom:10px; background-color:#b3dbce; font-size:30%; font-weight:200; color: #000000; ">
 
+
(Code interactively or fom scripts.)
  {{Vspace}}
+
</div>
 
 
<div class="keywords">
 
<b>Keywords:</b>&nbsp;
 
The console, arrow keys, ctrl-enter to execute code
 
 
</div>
 
</div>
  
{{Vspace}}
+
{{Smallvspace}}
 
 
  
__TOC__
 
 
{{Vspace}}
 
 
 
{{DEV}}
 
 
{{Vspace}}
 
  
 +
<div style="padding:5px; border:1px solid #000000; background-color:#b3dbce33; font-size:85%;">
 +
<div style="font-size:118%;">
 +
<b>Abstract:</b><br />
 +
<section begin=abstract />
 +
This unit discusses use working from the RStudio console vs. working from scripts.
 +
<section end=abstract />
 +
</div>
 +
<!-- ============================  -->
 +
<hr>
 +
<table>
 +
<tr>
 +
<td style="padding:10px;">
 +
<b>Objectives:</b><br />
 +
This unit discusses principles of working interactively vs. working from scripts.
 +
</td>
 +
<td style="padding:10px;">
 +
<b>Outcomes:</b><br />
 +
After working through this unit you ...
 +
* ... are familiar with the advantages of working from script files;
 +
* ... should begin always working from script files in your own practice.
  
 +
</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 ==
 
<!-- included from "../components/RPR-Console.components.wtxt", section: "abstract" -->
 
...
 
  
{{Vspace}}
+
{{Smallvspace}}
  
  
== This unit ... ==
 
=== Prerequisites ===
 
<!-- included from "../components/RPR-Console.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-Console.components.wtxt", section: "objectives" -->
 
...
 
 
 
{{Vspace}}
 
 
 
 
 
=== Outcomes ===
 
<!-- included from "../components/RPR-Console.components.wtxt", section: "outcomes" -->
 
...
 
 
 
{{Vspace}}
 
 
 
 
 
=== Deliverables ===
 
<!-- included from "../components/RPR-Console.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]].
 
<!-- 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}}
 
{{Vspace}}
Line 70: Line 65:
  
 
=== Evaluation ===
 
=== Evaluation ===
<!-- included from "../components/RPR-Console.components.wtxt", section: "evaluation" -->
 
<!-- included from "ABC-unit_components.wtxt", section: "eval-none" -->
 
 
<b>Evaluation: NA</b><br />
 
<b>Evaluation: NA</b><br />
:This unit is not evaluated for course marks.
+
<div style="margin-left: 2rem;">This unit is not evaluated for course marks.</div>
 
 
{{Vspace}}
 
 
 
 
 
</div>
 
<div id="BIO">
 
 
== Contents ==
 
== Contents ==
<!-- included from "../components/RPR-Console.components.wtxt", section: "contents" -->
 
 
===Typing code or executing it?===
 
===Typing code or executing it?===
  
 +
The RStudio "console" is the bottom-left pane into which you can type commands for execution, and which will contain the output of <code>print()</code> or<code>cat()</code> commands.
  
As you have seen in the '''R''' script ...
+
Commands can be ...
;you can type, or copy/paste code into the '''R''' or RStudio console, or if you are working with the R script in '''R''' or RStudio, just execute from the script.
+
* directly typed into the console;
 +
* copied by you from the Wiki and pasted into the console;
 +
* or executed from a script (i.e. a text-file of '''R''' commands that can run as a program), either by selecting and executing the code, or by executing the entire script with the <code>source("&lt;filename&gt;")</code> command.
  
In principle, commands can be copied by you from the Wiki and pasted into the console, or into a script (i.e. a text-file of '''R''' commands that can run as a program)  - obviously, you don't need to copy the comments. In addition, I use [http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi syntax highlighting] on '''R'''-code, to colour language keywords, numbers, strings, etc. different from other text. This improves readability but keep in mind that the colours you see on your computer may be different.
+
However, note the following: '''it is convenient to copy/paste code, but you don't learn how to write code through that'''. Practice has shown that it is better to actually type commands, even if you are just re-typing code from a book or online. Actively typing out the code character by character ensures you are reading and translating the code, and notice if anything is not entirely clear.<ref>We use a predictive mental contents-model when we type - something like an inbuilt autocorrect-suggestion mechanism; thus if you type something unfamiliar or surprising (e.g. a subtle detail of syntax), you will notice and be able to figure out the issue. ''Pasting'' code is a merely mechanical activity.</ref> In computer code, every single character matters. For example, I expect that by typing out commands you will be much less likely to confuse <code>=</code> with <code><-</code> or even <code>==</code>. Also, you will sometimes mistype and create errors. That's actually good, because you quickly learn to spot errors, fix them, and resume. That way you build confidence.
  
However, note the following: '''while it is convenient to copy/paste code, you don't learn how to write code through that'''. Practice has shown that it is better to actually type commands, even if you are just re-typing code from a book or online. Actively typing out the code character by character ensures you are reading and translating the code, and notice if anything is not entirely clear.<ref>I think we are using a predictive mental model when we type - something like an inbuilt autocorrect-suggestion mechanism; thus if you type something unfamiliar or surprising (e.g. a subtle detail of syntax), you will notice and be able to figure out the issue. ''Pasting'' code by contrast is merely mechanical.</ref> In computer code, every single character matters. For example, I expect that by typing out commands you will be much less likely to confuse <code>=</code> with <code><-</code> or even <code>==</code>. Also, you will sometimes mistype and create errors. That's actually good, because you quickly learn to spot errors, fix them, and resume. That way you build confidence.
+
Regarding the larger scripts we load from GitHub - these are too long to be retyped. Select the line, or parts of the line, or a larger block of code that you want to execute, then press <code>&lt;command&gt; R</code>, or <code>&lt;command&gt;&lt;enter&gt;</code> (depending on your operating system) to execute the selected block. In this case you'll need extra effort to discipline yourself and actually read and understand every single character and command.
  
However, for the project files we load from GitHub - these are too long to be retyped by you. Select the line, or parts of the line, or a larger block of code that you want to execute, then press <code>&lt;command&gt; R</code>, or <code>&lt;command&gt;&lt;enter&gt;</code> (depending on your operating system) to execute the selected block. In this case you'll need extra effort to discipline yourself to read and understand every single character and command. The point is not to execute the scripts. The point is to '''understand''' their contents. The best way to do this is to edit the code, vary parameters, try alternatives and in general '''play'''.
+
'''The point is not to execute the scripts. The point is to understand the contents.'''
  
However, actually '''working''' with code is another story. In this case scripts are indispensable for development. I type all steps of a typical analysis into a script - in this way I can always come back and reproduce the analysis: this makes the work reproducible. And I execute commands from the script, not the console - that's the easiest way to modify and develop. Making script and console work hand in hand is the way to work with '''R'''. There are four major advantages:
+
The best way to do this is to edit the code, vary parameters, try alternatives and in general '''play'''.
 +
 
 +
Actually '''working''' with code - i.e. developing code for the lab - is another story. In this case scripts are indispensable for development. I type all my R code into a script, never directly into the console - in this way it is much easier to come back, change things and recreate whatever analysis I was doing: this is essential for "reproducible research". I type into the script, save the script from time to time, and execute commands from the script, not the console - that's the easiest way to modify and develop. Making script and console work hand in hand is the way to work with '''R'''. There are four major advantages:
  
 
# The script is an accurate record of my procedure so I know exactly what I have done;
 
# The script is an accurate record of my procedure so I know exactly what I have done;
# I add numerous comments to record what I was thinking when I developed it;
+
# I add numerous comments, to record what I was thinking when I developed it;
 
# I can immediately reproduce the entire analysis from start to finish, simply by rerunning the script;
 
# I can immediately reproduce the entire analysis from start to finish, simply by rerunning the script;
# I can reuse parts easily, thus making new analyses quick to develop.
+
# I can reuse parts easily, thus making new analyses quick to develop;
# If I keep my script under version control, I can return to previous versions and undo errors. <small>That was number five.</small>
+
# If I keep my script under version control, I can return to previous versions and undo errors. <small>These were actually five points.</small>
  
 
{{Vspace}}
 
{{Vspace}}
Line 106: Line 97:
 
===User interface===
 
===User interface===
  
 
+
Both '''R''' and RStudio have a GUI<ref>A '''GUI''' is a Graphical User Interface, it has windows and menu items, as opposed to a "command line interface".</ref> to lay out common tasks. For example, there are a number of menu items, many of which are similar to other programs you will have worked with ("File", "Edit", "Format", "Window", "Help"  ...). But note: '''all''' of these tasks can also be accessed through the command line in the console.
Both '''R''' and RStudio have a GUI<ref>A '''GUI''' is a Graphical User Interface, it has windows and menu items, as opposed to a "command line interface".</ref> to lay out common tasks. For example, there are a number of menu items, many of which are similar to other programs you will have worked with ("File", "Edit", "Format", "Window", "Help"  ...). All of these tasks can also be accessed through the command line in the console.
 
  
 
In general, GUIs are useful when you are not sure what you want to do or how to go about it; the command line is much more powerful when you have more experience and know your way around in principle. '''R''' gives you both options.
 
In general, GUIs are useful when you are not sure what you want to do or how to go about it; the command line is much more powerful when you have more experience and know your way around in principle. '''R''' gives you both options.
 
Let's look at some functions of the '''R''' console and associated windows that relate to '''how''' you work, not '''what''' you do.
 
  
 
{{Vspace}}
 
{{Vspace}}
  
 
{{Vspace}}
 
 
 
== 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-Console.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-Console.components.wtxt", section: "self-evaluation" -->
 
 
<!--
 
<!--
 
=== Question 1===
 
=== Question 1===
Line 155: Line 120:
  
 
-->
 
-->
 +
== 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 183: Line 138:
 
:2017-08-05
 
:2017-08-05
 
<b>Modified:</b><br />
 
<b>Modified:</b><br />
:2017-08-05
+
:2020-09-15
 
<b>Version:</b><br />
 
<b>Version:</b><br />
:0.1
+
:1.1
 
<b>Version history:</b><br />
 
<b>Version history:</b><br />
*0.1 First stub
+
*1.1 Removed GeSHi calls. Maintenance
 +
*1.0.1 Maintenance
 +
*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:28, 25 September 2020

Console and scripts

(Code interactively or fom scripts.)


 


Abstract:

This unit discusses use working from the RStudio console vs. working from scripts.


Objectives:
This unit discusses principles of working interactively vs. working from scripts.

Outcomes:
After working through this unit you ...

  • ... are familiar with the advantages of working from script files;
  • ... should begin always working from script files in your own practice.

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

    Typing code or executing it?

    The RStudio "console" is the bottom-left pane into which you can type commands for execution, and which will contain the output of print() orcat() commands.

    Commands can be ...

    • directly typed into the console;
    • copied by you from the Wiki and pasted into the console;
    • or executed from a script (i.e. a text-file of R commands that can run as a program), either by selecting and executing the code, or by executing the entire script with the source("<filename>") command.

    However, note the following: it is convenient to copy/paste code, but you don't learn how to write code through that. Practice has shown that it is better to actually type commands, even if you are just re-typing code from a book or online. Actively typing out the code character by character ensures you are reading and translating the code, and notice if anything is not entirely clear.[1] In computer code, every single character matters. For example, I expect that by typing out commands you will be much less likely to confuse = with <- or even ==. Also, you will sometimes mistype and create errors. That's actually good, because you quickly learn to spot errors, fix them, and resume. That way you build confidence.

    Regarding the larger scripts we load from GitHub - these are too long to be retyped. Select the line, or parts of the line, or a larger block of code that you want to execute, then press <command> R, or <command><enter> (depending on your operating system) to execute the selected block. In this case you'll need extra effort to discipline yourself and actually read and understand every single character and command.

    The point is not to execute the scripts. The point is to understand the contents.

    The best way to do this is to edit the code, vary parameters, try alternatives and in general play.

    Actually working with code - i.e. developing code for the lab - is another story. In this case scripts are indispensable for development. I type all my R code into a script, never directly into the console - in this way it is much easier to come back, change things and recreate whatever analysis I was doing: this is essential for "reproducible research". I type into the script, save the script from time to time, and execute commands from the script, not the console - that's the easiest way to modify and develop. Making script and console work hand in hand is the way to work with R. There are four major advantages:

    1. The script is an accurate record of my procedure so I know exactly what I have done;
    2. I add numerous comments, to record what I was thinking when I developed it;
    3. I can immediately reproduce the entire analysis from start to finish, simply by rerunning the script;
    4. I can reuse parts easily, thus making new analyses quick to develop;
    5. If I keep my script under version control, I can return to previous versions and undo errors. These were actually five points.


     

    User interface

    Both R and RStudio have a GUI[2] to lay out common tasks. For example, there are a number of menu items, many of which are similar to other programs you will have worked with ("File", "Edit", "Format", "Window", "Help" ...). But note: all of these tasks can also be accessed through the command line in the console.

    In general, GUIs are useful when you are not sure what you want to do or how to go about it; the command line is much more powerful when you have more experience and know your way around in principle. R gives you both options.


     

    Self-evaluation

    Further reading, links and resources

    Notes

    1. We use a predictive mental contents-model when we type - something like an inbuilt autocorrect-suggestion mechanism; thus if you type something unfamiliar or surprising (e.g. a subtle detail of syntax), you will notice and be able to figure out the issue. Pasting code is a merely mechanical activity.
    2. A GUI is a Graphical User Interface, it has windows and menu items, as opposed to a "command line interface".


     


    About ...
     
    Author:

    Boris Steipe <boris.steipe@utoronto.ca>

    Created:

    2017-08-05

    Modified:

    2020-09-15

    Version:

    1.1

    Version history:

    • 1.1 Removed GeSHi calls. Maintenance
    • 1.0.1 Maintenance
    • 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.