Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted code blocks in challenge to R markdown #41

Merged
merged 31 commits into from
Jun 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cbf18a9
Moved solutions to end of lesson
sritchie73 Jun 4, 2015
1096537
Added section heading at end of lesson for challenge solutions
sritchie73 Jun 4, 2015
33025cf
Removed redundancy in challenge solution titles.
sritchie73 Jun 4, 2015
262d66d
Created section heading for challenge solutions for topic 5
sritchie73 Jun 4, 2015
889fe0f
Added "Solution to" back into challenge solution titles for those wis…
sritchie73 Jun 4, 2015
3c2f14e
Added challenge solutions heading at end of topic 6
sritchie73 Jun 4, 2015
549ce63
Fixed capitalization of solution headings in topic 6
sritchie73 Jun 4, 2015
f96401d
Added section heading at end of topic 7 for challenge solutions
sritchie73 Jun 4, 2015
c8d0c69
Fixed headings of challenge solutions for topic 7
sritchie73 Jun 4, 2015
23f57ce
Added section heading for challenge solutions to topic 8
sritchie73 Jun 4, 2015
10543f0
fixed titles of topic solutions
sritchie73 Jun 4, 2015
23c10aa
Added section heading for challenge solutions to topic 9
sritchie73 Jun 4, 2015
cbb4d06
Fixed titles for challenge solutions for topic 9
sritchie73 Jun 4, 2015
16e12da
Fixed heading levels of challenges and solutions
sritchie73 Jun 4, 2015
051dee2
Unified learning objectives header levels across files
sritchie73 Jun 4, 2015
51b9821
Added generated files
sritchie73 Jun 4, 2015
82af2a2
Updated challenge code blocks in topic 1 to be knitted
sritchie73 Jun 5, 2015
14bc593
Updated challenge code blocks in topic 2 to be knitted
sritchie73 Jun 5, 2015
c0701a7
Updated challenge code blocks in topic 3 to be knitted
sritchie73 Jun 5, 2015
039dd6f
Updated challenge code blocks in topic 4 to be knitted
sritchie73 Jun 5, 2015
71c8706
Updated challenge code blocks in topic 5 to be knitted
sritchie73 Jun 5, 2015
2b937a9
bugfix topic 4
sritchie73 Jun 5, 2015
a5818f3
Converted code in challenges and solutions in topic 6 to Rmarkdown
sritchie73 Jun 5, 2015
b42db49
Converted code in challenges and solutions in topic 7 to Rmarkdown
sritchie73 Jun 5, 2015
39ff966
Converted code in challenges and solutions in topic 8 to Rmarkdown
sritchie73 Jun 5, 2015
0e0654c
Converted code in challenges and solutions in topic 9 to Rmarkdown
sritchie73 Jun 5, 2015
69b4f16
Converted code in challenges and solutions in topic 12 to Rmarkdown
sritchie73 Jun 5, 2015
f577938
bigfix
sritchie73 Jun 5, 2015
4b7ae11
generated figures
sritchie73 Jun 5, 2015
f226829
generated lesson files
sritchie73 Jun 5, 2015
7d92fce
Script to check knitr version is high enough to render code blocks in…
sritchie73 Jun 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions 01-rstudio-intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ minutes: 45
source("tools/chunk-options.R")
```

> ### Learning Objectives {.objectives}
> ## Learning objectives {.objectives}
>
> * To gain familiarity with the various panes in the RStudio IDE
> * To gain familiarity with the buttons, short cuts and options in the Rstudio IDE
Expand Down Expand Up @@ -383,12 +383,12 @@ rm(list <- ls())
> Draw diagrams showing what variables refer to what values after each
> statement in the following program:
>
> ~~~ {.r}
> ```{r, eval=FALSE}
> mass <- 47.5
> age <- 122
> mass <- mass * 2.3
> age <- age - 20
> ~~~
> ```
>

> #### Challenge 2 {.challenge}
Expand Down
50 changes: 25 additions & 25 deletions 01-rstudio-intro.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="title">R for reproducible scientific analysis</h1>
<a href="index.html"><h1 class="title">R for reproducible scientific analysis</h1></a>
<h2 class="subtitle">Introduction to R and RStudio</h2>
<div id="learning-objectives" class="objectives panel panel-warning">
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h3><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h3>
<h2><span class="glyphicon glyphicon-certificate"></span>Learning objectives</h2>
</div>
<div class="panel-body">
<ul>
Expand All @@ -42,7 +42,7 @@ <h3><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h3
<li>To be able to call functions</li>
</ul>
</div>
</div>
</section>
<h3 id="introduction-to-rstudio">Introduction to RStudio</h3>
<p>Welcome to the R portion of the Software Carpentry workshop.</p>
<p>Throughout this lesson, we’re going to teach you some of the fundamentals of the R language as well as some best practices for organising code for scientific projects that will make your life easier.</p>
Expand All @@ -64,15 +64,15 @@ <h4 id="work-flow-within-rstudio">Work flow within Rstudio</h4>
<li>Start writing in an .R file and use Rstudio’s command / short cut to push current line, selected lines or modified lines to the interactive R console.</li>
<li>This is great way to start and work as all workings are saved for latter reference and can be read latter.</li>
</ol>
<div id="tip-pushing-to-the-interactive-r-console" class="callout panel panel-info">
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Pushing to the interactive R console</h4>
</div>
<div class="panel-body">
<p>To run the current line click on the <code>Run</code> button just above the file pane. Or use the short cut which can be see by hovering the mouse over the button.</p>
<p>To run a block of code, select it and then <code>Run</code>. If you have modified a line of code within a block of code you have just run. There is no need to reselct the section and <code>Run</code>, you can use the next button along, <code>Re-run the previous region</code>. This will run the previous code block inculding the modifications you have made.</p>
</div>
</div>
</aside>
<h3 id="introduction-to-r">Introduction to R</h3>
<p>A lot of your time in R will be spent in the R interactive console. This is where you will run all of your code, and can be a useful environment to try out ideas before adding them to an R script file. This console in RStudio is the same as the one you would get if you just typed in <code>R</code> in your commandline environment.</p>
<p>The first thing you will see in the R interactive session is a bunch of information, followed by a “&gt;” and a blinking cursor. In many ways this is similar to the shell environment you learnt about during the shell lessons: it operates on the same idea of a “Read, evaluate, print loop”: you type in commands, R tries to execute them, and then returns a result.</p>
Expand All @@ -86,15 +86,15 @@ <h4 id="using-r-as-a-calculator">Using R as a calculator</h4>
<pre class="sourceCode r"><code class="sourceCode r">&gt;<span class="st"> </span><span class="dv">1</span> +</code></pre>
<pre class="output"><code>+</code></pre>
<p>Any time you hit return and the R session shows a “+” instead of a “&gt;”, it means it’s waiting for you to complete the command. If you want to cancel a command you can simply hit “Esc” and RStudio will give you back the “&gt;” prompt.</p>
<div id="tip-cancelling-commands" class="callout panel panel-info">
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Cancelling commands</h4>
</div>
<div class="panel-body">
<p>If you’re using R from the commandline instead of from within RStudio, you need to use <code>Ctrl+C</code> instead of <code>Esc</code> to cancel the command. This applies to Mac users as well!</p>
<p>Cancelling a command isn’t just useful for killing incomplete commands: you can also use it to tell R to stop running code (for example if its taking much longer than you expect), or to get rid of the code you’re currently writing.</p>
</div>
</div>
</aside>
<p>When using R as a calculator, the order of operations is the same as you would have learnt back in school.</p>
<p>From highest to lowest precedence:</p>
<ul>
Expand Down Expand Up @@ -129,7 +129,7 @@ <h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Cancelling commands</h
<h4 id="mathematical-functions">Mathematical functions</h4>
<p>R has many built in mathematical functions. To call a function, we simply type its name, follow by and open and closing bracket. Anything we type inside those brackets is called the function’s arguments:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">sin</span>(<span class="dv">1</span>) <span class="co"># trigonometry functions</span></code></pre>
<pre class="output"><code>[1] 0.8415
<pre class="output"><code>[1] 0.841471
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dv">1</span>) <span class="co"># natural logarithm</span></code></pre>
<pre class="output"><code>[1] 0
Expand All @@ -138,7 +138,7 @@ <h4 id="mathematical-functions">Mathematical functions</h4>
<pre class="output"><code>[1] 1
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">exp</span>(<span class="fl">0.5</span>) <span class="co"># e^(1/2)</span></code></pre>
<pre class="output"><code>[1] 1.649
<pre class="output"><code>[1] 1.648721
</code></pre>
<p>Don’t worry about trying to remember every function in R. You can simply look them up on google, or if you can remember the start of the function’s name, use the tab completion in RStudio.</p>
<p>This is one advantage that RStudio has over R on its own, it has autocompletion abilities that allow you to more easily look up functions, their arguments, and the values that they take.</p>
Expand All @@ -163,7 +163,7 @@ <h4 id="comparing-things">Comparing things</h4>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> &gt;=<span class="st"> </span>-<span class="dv">9</span> <span class="co"># greater than or equal to</span></code></pre>
<pre class="output"><code>[1] TRUE
</code></pre>
<div id="tip-comparing-numbers" class="callout panel panel-info">
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Comparing Numbers</h4>
</div>
Expand All @@ -173,7 +173,7 @@ <h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Comparing Numbers</h4>
<p>Instead you should use the <code>all.equal</code> function.</p>
<p>Further reading: <a href="http://floating-point-gui.de/" class="uri">http://floating-point-gui.de/</a></p>
</div>
</div>
</aside>
<h4 id="variables-and-assignment">Variables and assignment</h4>
<p>We can store values in variables using the assignment operator <code>&lt;-</code>, like this:</p>
<pre class="sourceCode r"><code class="sourceCode r">x &lt;-<span class="st"> </span><span class="dv">1</span>/<span class="dv">40</span></code></pre>
Expand All @@ -184,7 +184,7 @@ <h4 id="variables-and-assignment">Variables and assignment</h4>
<p>More precisely, the stored value is a <em>decimal approximation</em> of this fraction called a <a href="http://en.wikipedia.org/wiki/Floating_point">floating point number</a>.</p>
<p>Look for the <code>Environment</code> tab in one of the panes of RStudio, and you will see that <code>x</code> and its value have appeared. Our variable <code>x</code> can be used in place of a number in any calculation that expects a number:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(x)</code></pre>
<pre class="output"><code>[1] -3.689
<pre class="output"><code>[1] -3.688879
</code></pre>
<p>Notice also that variables can be reassigned:</p>
<pre class="sourceCode r"><code class="sourceCode r">x &lt;-<span class="st"> </span><span class="dv">100</span></code></pre>
Expand All @@ -208,14 +208,14 @@ <h4 id="managing-your-environment">Managing your environment</h4>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ls</span>()</code></pre>
<pre class="output"><code>[1] &quot;hook_in&quot; &quot;hook_out&quot; &quot;x&quot;
</code></pre>
<div id="tip-hidden-objects" class="callout panel panel-info">
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pushpin"></span>Tip: hidden objects</h4>
</div>
<div class="panel-body">
<p>Just like in the shell, <code>ls</code> will hide any variables or functions starting with a “.” by default. To list all objects, type <code>ls(all.names=TRUE)</code> instead</p>
</div>
</div>
</aside>
<p>Note here that we didn’t given any arguments to <code>ls</code>, but we still needed to give the brackets to tell R to call the function.</p>
<p>If we type <code>ls</code> by itself, R will print out the source code for that function!</p>
<pre class="sourceCode r"><code class="sourceCode r">ls</code></pre>
Expand Down Expand Up @@ -251,7 +251,7 @@ <h4><span class="glyphicon glyphicon-pushpin"></span>Tip: hidden objects</h4>
}
else all.names
}
&lt;bytecode: 0x7f9fca043558&gt;
&lt;bytecode: 0x7fd893942358&gt;
&lt;environment: namespace:base&gt;
</code></pre>
<p>You can use <code>rm</code> to delete objects you no longer need:</p>
Expand All @@ -262,18 +262,18 @@ <h4><span class="glyphicon glyphicon-pushpin"></span>Tip: hidden objects</h4>
<p>In this case we’ve specified that the results of <code>ls</code> should be used for the <code>list</code> argument in <code>rm</code>. When assigning values to arguments by name, you <em>must</em> use the <code>=</code> operator!!</p>
<p>If instead we use <code>&lt;-</code>, there will be unintended side effects, or you may just get an error message:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(list &lt;-<span class="st"> </span><span class="kw">ls</span>())</code></pre>
<pre class="output"><code>Error: ... must contain names or character strings
<pre class="output"><code>Error in rm(list &lt;- ls()): ... must contain names or character strings
</code></pre>
<div id="tip-warnings-vs.errors" class="callout panel panel-info">
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pushpin"></span>Tip: Warnings vs. Errors</h4>
</div>
<div class="panel-body">
<p>Pay attention when R does something unexpected! Errors, like above, are thrown when R cannot proceed with a calculation. Warnings on the other hand usually mean that the function has run, but it probably hasn’t worked as expected.</p>
<p>In both cases, the message that R prints out usually give you clues how to fix a problem.</p>
</div>
</div>
<div id="challenge-1" class="challenge panel panel-success">
</aside>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pencil"></span>Challenge 1</h4>
</div>
Expand All @@ -284,23 +284,23 @@ <h4><span class="glyphicon glyphicon-pencil"></span>Challenge 1</h4>
mass &lt;-<span class="st"> </span>mass *<span class="st"> </span><span class="fl">2.3</span>
age &lt;-<span class="st"> </span>age -<span class="st"> </span><span class="dv">20</span></code></pre>
</div>
</div>
<div id="challenge-2" class="challenge panel panel-success">
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pencil"></span>Challenge 2</h4>
</div>
<div class="panel-body">
<p>Run the code from the previous challenge, and write a command to compare mass to age. Is mass larger than age?</p>
</div>
</div>
<div id="challenge-3" class="challenge panel panel-success">
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4><span class="glyphicon glyphicon-pencil"></span>Challenge 3</h4>
</div>
<div class="panel-body">
<p>Clean up your working environment by deleting the mass and age variables.</p>
</div>
</div>
</section>
</div>
</div>
</article>
Expand Down