forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-making-packages-R.Rmd
157 lines (122 loc) · 4.3 KB
/
08-making-packages-R.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
---
layout: page
title: Programming with R
subtitle: Making packages in R
minutes: 30
---
```{r, include = FALSE}
source("tools/chunk-options.R")
```
> ## Learning Objectives {.objectives}
>
> Quick summary on how (and why) making R packages
Why should you make your own R packages?
**Reproducible research!**
An R package is the **basic unit of reusable code**.
If you want to reuse code later or want others to be able to use your code, you should put it in a package.
An R package requires two components:
- a DESCRIPTION file with metadata about the package
- an R directory with the code
*There are other optional components. Go [here](http://adv-r.had.co.nz/Package-basics.html) for much more information.*
DESCRIPTION file
----------------
Package: Package name
Title: Brief package description
Description: Longer package description
Version: Version number(major.minor.patch)
Author: Name and email of package creator
Maintainer: Name and email of package maintainer (who to contact with issues)
License: Abbreviation for an open source license
The package name can only contain letters and numbers and has to start with a letter.
.R files
--------
Functions don't all have to be in one file or each in separate files.
How you organize them is up to you.
Suggestion: organize in a logical manner so that you know which file holds which functions.
Making your first R package
---------------------------
Let's turn our temperature conversion functions into an R package.
```{r}
fahr_to_kelvin <- function(temp) {
#Converts Fahrenheit to Kelvin
kelvin <- ((temp - 32) * (5/9)) + 273.15
kelvin
}
```
```{r}
kelvin_to_celsius <- function(temp) {
#Converts Kelvin to Celsius
Celsius <- temp - 273.15
Celsius
}
```
```{r}
fahr_to_celsius <- function(temp) {
#Converts Fahrenheit to Celsius using fahr_to_kelvin() and kelvin_to_celsius()
temp_k <- fahr_to_kelvin(temp)
result <- kelvin_to_celsius(temp_k)
result
}
```
We will use the `devtools` and `roxygen2` packages, which make creating packages in R relatively simple.
First, install the `devtools` package, which will allow you to install the `roxygen2` package from GitHub ([code][]).
[code]: https://github.com/klutometis/roxygen
```{r, eval=FALSE}
install.packages("devtools")
library("devtools")
install_github("klutometis/roxygen")
library("roxygen2")
```
Set your working directory, and then use the `create` function to start making your package.
Keep the name simple and unique.
- package_to_convert_temperatures_between_kelvin_fahrenheit_and_celsius (BAD)
- tempConvert (GOOD)
```{r, eval=FALSE}
setwd(parentDirectory)
create("tempConvert")
```
Add our functions to the R directory.
Place each function into a separate R script and add documentation like this:
```{r}
#' Convert Fahrenheit to Kelvin
#'
#' This function converts input temperatures in Fahrenheit to Kelvin.
#' @param temp The input temperature.
#' @export
#' @examples
#' fahr_to_kelvin(32)
fahr_to_kelvin <- function(temp) {
#Converts Fahrenheit to Kelvin
kelvin <- ((temp - 32) * (5/9)) + 273.15
kelvin
}
```
The `roxygen2` package reads lines that begin with `#'` as comments to create the documentation for your package.
Descriptive tags are preceded with the `@` symbol. For example, `@param` has information about the input parameters for the function.
Now, we will use `roxygen2` to convert our documentation to the standard R format.
```{r, eval=FALSE}
setwd("./tempConvert")
document()
```
Take a look at the package directory now.
The /man directory has a .Rd file for each .R file with properly formatted documentation.
Now, let's load the package and take a look at the documentation.
```{r, eval=FALSE}
setwd("..")
install("tempConvert")
?fahr_to_kelvin
```
Notice there is now a tempConvert environment that is the parent environment to the global environment.
```{r, eval=FALSE}
search()
```
Now that our package is loaded, let's try out some of the functions.
```{r}
fahr_to_celsius(32)
fahr_to_kelvin(212)
kelvin_to_celsius(273.15)
```
> ## Challenge - Creating a package for distribution {.challenge}
>
> - Create some new functions for your tempConvert package to convert from Kelvin to Fahrenheit or from Celsius to Kelvin or Fahrenheit.
> - Create a package for our `analyze` function so that it will be easy to load when more data arrives.