-
Notifications
You must be signed in to change notification settings - Fork 2
/
05-write-some-code.Rmd
87 lines (60 loc) · 1.83 KB
/
05-write-some-code.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
```{r echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# Coding in a package {#write-some-code}
## Edit code, run, modify, repeat
When developing a package, find whatever method is most efficient for you
to develop the package quickly.
You want to be able to edit code, then run/test it, modify, rerun, modify, etc.
until you are happy with it.
For some this may mean working in an IDE (RStudio) - for others this means
a text editor and console - for others maybe Vim or Emacs.
Get used to the keyboard shortcuts or buttons to make this flow easy and
quick.
`devtools::load_all()` (or equivalent buttons/shortcuts in IDE) is your friend - though beware you still want to check your package outside of a load all context (See \@ref(package-checks))
## Code
### Exercise
__Write your own function__
* Write a function
* Put it in a file in your R package
## Docs
Time to document your function!
Above your function start each line with `#'`. There's many, many
options for documenting functions.
### Exercise
__Let's do a minimal set:__
* Title
* Export tag
* Parameter definitions
* Return tag
* Examples
```r
#' Multiply a number by 2
#'
#' @export
#' @param x a numeric value. required
#' @return a number
#' @examples
#' multiply2(x = 5)
multiply2 <- function(x) {
stopifnot(is.numeric(x))
x * 2
}
```
## Generate documentation
In RStudio push the document button or keyboard shortcut, or on the command line
run `devtools::document()` or similar.
Notice what state your `NAMESPACE` file is in before and after.
## Install the package
Now install the package.
* in RStudio: Install and Restart
* command line: `devtools::install()`
* restart your R session
* load your package `library(packagename)`
* Can you run your function?
* if not, let's dig in and sort it out
## Yay!