forked from tracykteal/R-genomics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00-before-we-start.Rmd
235 lines (178 loc) · 9.63 KB
/
00-before-we-start.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
layout: topic
title: Before we start
author: Data Carpentry contributors
minutes: 15
---
```{r, echo=FALSE, purl=FALSE}
knitr::opts_chunk$set(results='hide', fig.path='img/r-lesson-')
```
------------
> ## Learning Objectives
>
> * Navigate the RStudio interface
> * Get set up with a working directory with a `data/` folder inside
> * Know how to get help, and understand how to ask well formulated questions
------------
# RStudio
Start RStudio -- Let's start by learning about our tool.
* Console, Scripts, Environments, Plots
* Code and workflow are more reproducible if we can document everything that we
do.
* Our end goal is not just to "do stuff" but to do it in a way that anyone can
easily and exactly replicate our workflow and results.
# Before we get started
* Under the `File` menu, click on `New project`, choose `New directory`, then
`Empty project`
* Enter a name for this new folder, and choose a convenient location for
it. This will be your **working directory** for the rest of the day
(e.g., `~/data-carpentry`)
* Click on "Create project"
* Under the `Files` tab on the right of the screen, click on `New Folder` and
create a folder named `data` within your newly created working directory.
(e.g., `~/data-carpentry/data`)
* Create a new R script (File > New File > R script) and save it in your working
directory (e.g. `data-carpentry-script.R`)
Your working directory should now look like this:

# Interacting with R
There are two main ways of interacting with R: using the console or by using
script files (plain text files that contain your code).
The console window (in RStudio, the bottom left panel) is the place where R is
waiting for you to tell it what to do, and where it will show the results of a
command. You can type commands directly into the console, but they will be
forgotten when you close the session. It is better to enter the commands in the
script editor, and save the script. This way, you have a complete record of what
you did, you can easily show others how you did it and you can do it again later
on if needed. You can copy-paste into the R console, but the Rstudio script
editor allows you to 'send' the current line or the currently selected text to
the R console using the `Ctrl-Enter` shortcut.
If R is ready to accept commands, the R console shows a `>` prompt. If it
receives a command (by typing, copy-pasting or sent from the script editor using
`Ctrl-Enter`), R will try to execute it, and when ready, show the results and
come back with a new `>`-prompt to wait for new commands.
If R is still waiting for you to enter more data because it isn't complete yet,
the console will show a `+` prompt. It means that you haven't finished entering
a complete command. This is because you have not 'closed' a parenthesis or
quotation. If you're in Rstudio and this happens, click inside the console
window and press `Esc`; this should help you out of trouble.
# Basics of R
R is a versatile, open source programming/scripting language that's useful both
for statistics but also data science. Inspired by the programming language S.
* Open source software under GPL.
* Superior (if not just comparable) to commercial alternatives. R has over 7,000
user contributed packages at this time. It's widely used both in academia and
industry.
* Available on all platforms.
* Not just for statistics, but also general purpose programming.
* For people who have experience in programmming: R is both an object-oriented
and a so-called [functional language](http://adv-r.had.co.nz/Functional-programming.html)
* Large and growing community of peers.
## Organizing your working directory
You should separate the original data (raw data) from intermediate datasets that
you may create for the need of a particular analysis. For instance, you may want
to create a `data/` directory within your working directory that stores the raw
data, and have a `data_output/` directory for intermediate datasets and a
`figure_output/` directory for the plots you will generate.
## Seeking help
### I know the name of the function I want to use, but I'm not sure how to use it
If you need help with a specific function, let's say `barplot()`, you can type:
```{r, eval=FALSE}
?barplot
```
If you just need to remind yourself of the names of the arguments, you can use:
```{r, eval=FALSE}
args(lm)
```
If the function is part of a package that is installed on your computer but
don't remember which one, you can type:
```{r, eval=FALSE}
??geom_point
```
### I want to use a function that does X, there must be a function for it but I don't know which one...
If you are looking for a function to do a particular task, you can use
`help.search()` (but only looks through the installed packages):
```{r, eval=FALSE}
help.search("kruskal")
```
If you can't find what you are looking for, you can use the
[rdocumention.org](http://www.rdocumentation.org) website that search through
the help files across all packages available.
### I am stuck... I get an error message that I don't understand
Start by googling the error message. However, this doesn't always work very well
because often, package developers rely on the error catching provided by R. You
end up with general error messages that might not be very helpful to diagnose a
problem (e.g. "subscript out of bounds").
However, you should check stackoverflow. Search using the `[r]` tag. Most
questions have already been answered, but the challenge is to use the right
words in the search to find the answers:
[http://stackoverflow.com/questions/tagged/r](http://stackoverflow.com/questions/tagged/r)
The [Introduction to R](http://cran.r-project.org/doc/manuals/R-intro.pdf) can
also be dense for people with little programming experience but it is a good
place to understand the underpinnings of the R language.
The [R FAQ](http://cran.r-project.org/doc/FAQ/R-FAQ.html) is dense and technical
but it is full of useful information.
### Asking for help
The key to get help from someone is for them to grasp your problem rapidly. You
should make it as easy as possible to pinpoint where the issue might be.
Try to use the correct words to describe your problem. For instance, a package
is not the same thing as a library. Most people will understand what you meant,
but others have really strong feelings about the difference in meaning. The key
point is that it can make things confusing for people trying to help you. Be as
precise as possible when describing your problem
If possible, try to reduce what doesn't work to a simple reproducible
example. If you can reproduce the problem using a very small `data.frame`
instead of your 50,000 rows and 10,000 columns one, provide the small one with
the description of your problem. When appropriate, try to generalize what you
are doing so even people who are not in your field can understand the question.
To share an object with someone else, if it's relatively small, you can use the
function `dput()`. It will output R code that can be used to recreate the exact same
object as the one in memory:
```{r, results='show'}
dput(head(iris)) # iris is an example data.frame that comes with R
```
If the object is larger, provide either the raw file (i.e., your CSV file) with
your script up to the point of the error (and after removing everything that is
not relevant to your issue). Alternatively, in particular if your questions is
not related to a `data.frame`, you can save any R object to a file:
```{r, eval=FALSE}
saveRDS(iris, file="/tmp/iris.rds")
```
The content of this file is however not human readable and cannot be posted
directly on stackoverflow. It can however be sent to someone by email who can read
it with this command:
```{r, eval=FALSE}
some_data <- readRDS(file="~/Downloads/iris.rds")
```
Last, but certainly not least, **always include the output of `sessionInfo()`**
as it provides critical information about your platform, the versions of R and
the packages that you are using, and other information that can be very helpful
to understand your problem.
```{r, results='show'}
sessionInfo()
```
### Where to ask for help?
* Your friendly colleagues: if you know someone with more experience than you,
they might be able and willing to help you.
* Stackoverlow: if your question hasn't been answered before and is well
crafted, chances are you will get an answer in less than 5 min.
* The [R-help](https://stat.ethz.ch/mailman/listinfo/r-help): it is read by a
lot of people (including most of the R core team), a lot of people post to it,
but the tone can be pretty dry, and it is not always very welcoming to new
users. If your question is valid, you are likely to get an answer very fast
but don't expect that it will come with smiley faces. Also, here more than
everywhere else, be sure to use correct vocabulary (otherwise you might get an
answer pointing to the misuse of your words rather than answering your
question). You will also have more success if your question is about a base
function rather than a specific package.
* If your question is about a specific package, see if there is a mailing list
for it. Usually it's included in the DESCRIPTION file of the package that can
be accessed using `packageDescription("name-of-package")`. You may also want
to try to email the author of the package directly.
* There are also some topic-specific mailing lists (GIS, phylogenetics, etc...),
the complete list is [here](http://www.r-project.org/mail.html).
### More resources
* The [Posting Guide](http://www.r-project.org/posting-guide.html) for the R
mailing lists.
* [How to ask for R help](http://blog.revolutionanalytics.com/2014/01/how-to-ask-for-r-help.html)
useful guidelines