-
Notifications
You must be signed in to change notification settings - Fork 1
/
tips_and_tricks.Rmd
95 lines (74 loc) · 3.02 KB
/
tips_and_tricks.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
---
title: Tips and Tricks
author: Robert McDonald
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output:
html_document:
toc: true
---
```{r setup, echo=FALSE, message=FALSE}
library(tidyverse)
library(knitr)
opts_chunk$set(error = TRUE
,collapse = TRUE)
```
# Tips
## Setting up your working environment
* Make sure that you always start R with a clean environment
* In RStudio:
* In "Global Options", ***uncheck*** the box reading "Restore
.RData into workspace at startup"
* Set "Save workspace to .RData on exit" to ***Never***
* In other editors, always answer "n" to the `Save workspace image?
[y/n/c]` prompt when you exit R
* At least once, you should execute `options()` at the command line
and take the time to look at the output. This shows you various
options you can set globally for the current session. Here are two
you might wish to change to suit your preferences:
* Output width: Set to 120 with `options(width=120)` (default is `r#
getOption("width")`)
* Number of digits: Set to 3 with `options(digits=3)` (default is `r#
getOption("digits")`)
* Set both at once: `options(width = 100, digits = 3)`
* You can save and restore a set of options:
```{r}
options('width', 'digits')
saveopt <- options('width', 'digits') ## save options
saveopt
options(width = 150, digits = 12)
options('width', 'digits')
options(saveopt) ## restore
options('width', 'digits')
```
Note that when you restart R, options return to their original defaults.
## Debugging
* First and most important: **read the error messages**. Even if you
find them confusing, they convey information about what went wrong
and where it occurred
* If you can't quickly find the problem, save your documents and
restart R. Sometimes the problem goes away with a clear environment
*
# Make sure you know which function you're using
* Function names are not unique. The same function name can be used in
multiple packages, and the functions may do very different things.
For example, `dplyr` uses `filter`, which is also provided by the
`stats` package, and `summarize` is also provided by the `Hmisc`
package. If you get an incomprehensible error message associated
with a `dplyr` function name, one of the first things to try is to
qualify the function name. Here is an example in which the first
calculation fails but the second succeeds.^[Note that standard
practice in Python is to qualify function names so that this
confusion does not occur. For example you would write `import Pandas
as pd`, and then call Pandas functions with a `pd.` prefix.]
Note that if you use the British spelling, `summarise`, this issue
does not occur. (The `dplyr` package recognizes both spellings, the
`Hmisc` package does not.)
```{r, message=FALSE}
library(Hmisc) ## some other package may have invoked this
mtcars %>% group_by(gear) %>%
summarize(N = n())
mtcars %>% group_by(gear) %>%
dplyr::summarize(N = n())
mtcars %>% group_by(gear) %>%
summarise(N = n())
```