Skip to content

Commit

Permalink
Merge pull request #41 from sritchie73/test-knitr-dev
Browse files Browse the repository at this point in the history
Converted code blocks in challenge to R markdown
  • Loading branch information
aammd committed Jun 5, 2015
2 parents 7603d8b + 7fad4f9 commit 630314f
Show file tree
Hide file tree
Showing 40 changed files with 1,558 additions and 957 deletions.
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
80 changes: 40 additions & 40 deletions 01-rstudio-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
layout: page
title: R for reproducible scientific analysis
subtitle: Introduction to R and RStudio
minutes: 15
minutes: 45
---



> ### 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 All @@ -19,7 +19,7 @@ minutes: 15
### Introduction to RStudio

Welcome to the R portion of the Software Carpentry workshop.
Welcome to the R portion of the Software Carpentry workshop.

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
Expand Down Expand Up @@ -50,26 +50,26 @@ There are two main ways one can work within Rstudio.
1. This is great way to start and work as all workings are saved for latter reference and can be read latter.

> #### Tip: Pushing to the interactive R console {.callout}
> To run the current line click on the `Run` button just above the file pane. Or use the short cut which can be see
> To run the current line click on the `Run` button just above the file pane. Or use the short cut which can be see
> by hovering the mouse over the button.
>
> To run a block of code, select it and then `Run`. 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 `Run`,
> you can use the next button along, `Re-run the previous region`. This will run the previous code block inculding
> of code within a block of code you have just run. There is no need to reselct the section and `Run`,
> you can use the next button along, `Re-run the previous region`. This will run the previous code block inculding
> the modifications you have made.
>
### Introduction to R

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
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 `R` in your commandline environment.

The first thing you will see in the R interactive session is a bunch of information,
The first thing you will see in the R interactive session is a bunch of information,
followed by a ">" 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
of a "Read, evaluate, print loop": you type in commands, R tries to execute them, and
then returns a result.

#### Using R as a calculator
Expand All @@ -88,7 +88,7 @@ The simplest thing you could do with R is do arithmetic:
~~~

And R will print out the answer, with a preceding "[1]". Don't worry about this
And R will print out the answer, with a preceding "[1]". Don't worry about this
for now, we'll explain that later. For now think of it as indicating ouput.

Just like bash, if you type in an incomplete command, R will wait for you to
Expand All @@ -102,7 +102,7 @@ complete it:
+
~~~

Any time you hit return and the R session shows a "+" instead of a ">", it
Any time you hit return and the R session shows a "+" instead of a ">", 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 ">"
prompt.
Expand All @@ -115,7 +115,7 @@ prompt.
>
> 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
> taking much longer than you expect), or to get rid of the code you're
> currently writing.
>
Expand Down Expand Up @@ -169,7 +169,7 @@ But this can get unwieldy when not needed:


The text I've typed after each line of code is called a comment. Anything that
follows on from the octothorpe (or hash) symbol `#` is ignored by R when it
follows on from the octothorpe (or hash) symbol `#` is ignored by R when it
executes code.

Really small or large numbers get a scientific notation:
Expand Down Expand Up @@ -218,7 +218,7 @@ sin(1) # trigonometry functions


~~~{.output}
[1] 0.8415
[1] 0.841471
~~~

Expand Down Expand Up @@ -254,7 +254,7 @@ exp(0.5) # e^(1/2)


~~~{.output}
[1] 1.649
[1] 1.648721
~~~

Expand All @@ -263,13 +263,13 @@ 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.

This is one advantage that RStudio has over R on its own, it
has autocompletion abilities that allow you to more easily
has autocompletion abilities that allow you to more easily
look up functions, their arguments, and the values that they
take.

Typing a `?` before the name of a command will open the help page
for that command. As well as providing a detailed description of
the command and how it works, scrolling ot the bottom of the
the command and how it works, scrolling ot the bottom of the
help page will usually show a collection of code examples which
illustrate command usage. We'll go through an example later.

Expand Down Expand Up @@ -350,19 +350,19 @@ We can also do comparison in R:
~~~

> #### Tip: Comparing Numbers {.callout}
>
> A word of warning about comparing numbers: you should
>
> A word of warning about comparing numbers: you should
> never use `==` to compare two numbers unless they are
> integers (a data type which can specifically represent
> only whole numbers).
> only whole numbers).
>
> Computers may only represent decimal numbers with a
> Computers may only represent decimal numbers with a
> certain degree of precision, so two numbers which look
> the same when printed out by R, may actually have
> different underlying representations and therefore be
> different by a small margin of error (called Machine
> numeric tolerance).
>
> different underlying representations and therefore be
> different by a small margin of error (called Machine
> numeric tolerance).
>
> Instead you should use the `all.equal` function.
>
> Further reading: [http://floating-point-gui.de/](http://floating-point-gui.de/)
Expand Down Expand Up @@ -406,7 +406,7 @@ log(x)


~~~{.output}
[1] -3.689
[1] -3.688879
~~~

Expand Down Expand Up @@ -437,7 +437,7 @@ different conventions for long variable names, these include
* underscores\_between_words
* camelCaseToSeparateWords

What you use is up to you, but **be consistent**.
What you use is up to you, but **be consistent**.

It is also possible to use the `=` operator for assignment:

Expand Down Expand Up @@ -471,9 +471,9 @@ ls()
~~~

> #### Tip: hidden objects {.callout}
>
>
> Just like in the shell, `ls` will hide any variables or functions starting
> with a "." by default. To list all objects, type `ls(all.names=TRUE)`
> with a "." by default. To list all objects, type `ls(all.names=TRUE)`
> instead
>
Expand Down Expand Up @@ -522,7 +522,7 @@ function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE,
}
else all.names
}
<bytecode: 0x7f9fca043558>
<bytecode: 0x7fd893942358>
<environment: namespace:base>
~~~
Expand All @@ -543,13 +543,13 @@ rm(list = ls())
~~~

In this case we've combined the two. Just like the order of operations, anything
inside the innermost brackets is evaluated first, and so on.
inside the innermost brackets is evaluated first, and so on.

In this case we've specified that the results of `ls` should be used for the
`list` argument in `rm`. When assigning values to arguments by name, you *must*
use the `=` operator!!
In this case we've specified that the results of `ls` should be used for the
`list` argument in `rm`. When assigning values to arguments by name, you *must*
use the `=` operator!!

If instead we use `<-`, there will be unintended side effects, or you may just
If instead we use `<-`, there will be unintended side effects, or you may just
get an error message:


Expand All @@ -560,11 +560,11 @@ rm(list <- ls())


~~~{.output}
Error: ... must contain names or character strings
Error in rm(list <- ls()): ... must contain names or character strings
~~~

> #### Tip: Warnings vs. Errors {.callout}
> #### Tip: Warnings vs. Errors {.callout}
>
> Pay attention when R does something unexpected! Errors, like above,
> are thrown when R cannot proceed with a calculation. Warnings on the
Expand All @@ -578,10 +578,11 @@ Error: ... must contain names or character strings

> #### Challenge 1 {.challenge}
>
> Draw diagrams showing what variables refer to what values after each
> Draw diagrams showing what variables refer to what values after each
> statement in the following program:
>
> ~~~ {.r}
>
> ~~~{.r}
> mass <- 47.5
> age <- 122
> mass <- mass * 2.3
Expand All @@ -600,4 +601,3 @@ Error: ... must contain names or character strings
> Clean up your working environment by deleting the mass and age
> variables.
>
6 changes: 3 additions & 3 deletions 02-project-intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ minutes: 30
source("tools/chunk-options.R")
```

> ## Learning Objectives {.objectives}
> ## Learning objectives {.objectives}
>
> * To be able to create self-contained projects in RStudio
> * To be able to use git from within RStudio
Expand Down Expand Up @@ -114,11 +114,11 @@ get shared between projects.
> 2. Load the library
> 3. Initialise the project:
>
> ~~~ {.r}
> ```{r, eval=FALSE}
> install.packages("ProjectTemplate")
> library(ProjectTemplate)
> create.project("../my_project", merge.strategy = "allow.non.conflict")
> ~~~
> ```
>
> For more information on ProjectTemplate and its functionality visit the
> home page [ProjectTemplate](http://projecttemplate.net/index.html)
Expand Down
17 changes: 9 additions & 8 deletions 02-project-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
layout: page
title: R for reproducible scientific analysis
subtitle: Project management with RStudio
minutes: 15
minutes: 30
---



> ## Learning Objectives {.objectives}
> ## Learning objectives {.objectives}
>
> * To be able to create self-contained projects in RStudio
> * To be able to use git from within RStudio
Expand Down Expand Up @@ -49,7 +49,7 @@ A good project layout will ultimately make your life easier:
Fortunately, there are tools and packages which can help you manage your work effectively.

One of the most powerful and useful aspects of RStudio is its project management
functionality. We'll be using this today to create a self-contianed, reproducible
functionality. We'll be using this today to create a self-contained, reproducible
project.


Expand Down Expand Up @@ -100,19 +100,20 @@ analysis. This makes it easier later, as many of my analyses are exploratory
and don't end up being used in the final project, and some of the analyses
get shared between projects.

> #### Tip: ProjectTempate - a possible solution {.callout}
> #### Tip: ProjectTemplate - a possible solution {.callout}
>
> One way to automate the management of projects is to install the third-party package, `ProjectTemplate`.
> This package will set up an ideal directory structure for project management.
> This is very useful as it enables you to have your analysis pipeline/workflow organised and structured.
> Together with the default Rstudio project functionality and Git you will be able to keep track of your
> Together with the default RStudio project functionality and Git you will be able to keep track of your
> work as well as be able to share your work with collaborators.
>
> 1. Install `ProjectTemplate`.
> 2. Load the library
> 3. Initialise the project:
>
> ~~~ {.r}
>
> ~~~{.r}
> install.packages("ProjectTemplate")
> library(ProjectTemplate)
> create.project("../my_project", merge.strategy = "allow.non.conflict")
Expand Down Expand Up @@ -143,7 +144,7 @@ one to store the analysis scripts.
> make updates to code in multiple places.
>
> In this case I find it useful to make "symbolic links", which are essentially
> shortcuts to files somewhere else on a filesystem. On linux and OSX you can
> shortcuts to files somewhere else on a filesystem. On Linux and OS X you can
> use the `ln -s` command, and on windows you can either create a shortcut or
> use the `mklink` command from the windows terminal.
>
Expand All @@ -153,7 +154,7 @@ one to store the analysis scripts.
Now we have a good directory structure we will now place/save the data file in the `data/` directory.

> #### Challenge 1 {.challenge}
> Download the gapminer data from [here](https://github.com/resbaz/r-novice-gapminder-files).
> Download the gapminder data from [here](https://github.com/resbaz/r-novice-gapminder-files).
>
> 1. Use the `Download ZIP` located on the right hand side menu, last option. To download the `.zip` file to
> your downloads folder.
Expand Down
23 changes: 12 additions & 11 deletions 03-seeking-help.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ source("tools/chunk-options.R")
```


> ### Learning Objectives {.objectives}
> ## Learning objectives {.objectives}
>
> * To be able read R help files for functions and special operators.
> * To be able to use CRAN task views to identify packages to solve a problem.
Expand Down Expand Up @@ -114,19 +114,20 @@ your issue.
> What is the difference between the `sep` and `collapse` arguments?
>
> #### Solution to Challenge 1 {.challenge}
### Other ports of call

* [Quick R](http://www.statmethods.net/)
* [RStudio cheat sheets](http://www.rstudio.com/resources/cheatsheets/)
* [Cookbook for R](http://www.cookbook-r.com/)

## Challenge solutions

> #### Solution to challenge 1 {.challenge}
>
> Look at the help for the `paste` function. You'll need to use this later.
>
> ~~~ {.r}
> ```{r, eval=FALSE}
> help("paste")
> ?paste
> ~~~
> ```
>

### Other ports of call

* [Quick R](http://www.statmethods.net/)
* [RStudio cheat sheets](http://www.rstudio.com/resources/cheatsheets/)
* [Cookbook for R](http://www.cookbook-r.com/)

0 comments on commit 630314f

Please sign in to comment.