-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffv_devtools_create_package.Rmd
518 lines (427 loc) Β· 21.4 KB
/
ffv_devtools_create_package.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
---
title: "Using devtools to Create a R Package"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Using devtools to Create a R Package}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(PkgTestR)
```
In the example/tutorial below, we will create a R package from scratch. Here are some alternative tutorials:
- [R Packages](https://r-pkgs.org/) by Hadley Wickham and Jenny Bryan.
- [R package primer a minimal tutorial](https://kbroman.org/pkg_primer/) from [https://kbroman.org/](https://kbroman.org/).
We will be using the following functions from [devtools](https://devtools.r-lib.org/index.html):
- *devtools::create()*: create a package folder structure.
- *devtools::document()*: generate .rd files in man and update NAMESPACE.
+ Examples and Vignettes:
- *devtools::run_examples()* to test examples from .R files,
- *devtools::build_vignettes()* to create HTML etc for vignette files.
- *devtools::check()*: builds and check package locally.
- *devtools::install()*: building a package source directory into a single bundled file.
- *devtools::load_all()*: load from source to memory
Additionally, we also use:
- *devtools::build()*: build the binary *tar.gz* file.
- *devtools::build_manual()*: building the PDF manual including function markups.
Limited set of commands to run during development:
```{r, eval = FALSE}
rm(list=ls())
setwd("G:/repos/PkgTestR")
devtools::document()
devtools::run_examples()
devtools::install()
devtools::load_all()
pkgdown::build_site()
```
Full set of commands to run:
```{r, eval = FALSE}
# Clean and set directory
rm(list=ls())
setwd("G:/repos/PkgTestR")
# write documentation files
devtools::document()
devtools::run_examples()
devtools::build_vignettes()
# check for errors
devtools::check()
# Install and load
devtools::install()
devtools::load_all()
# build local binary and pdf manual
devtools::build()
devtools::build_manual()
# locally building site
pkgdown::build_site()
```
## Initialize a Skeleton Package
With [devtools](https://devtools.r-lib.org/), we create a new package.
First, use the create function, [devtools:create("PATH_TO_NEW_PRJ_FOLDER", open=TRUE)](https://devtools.r-lib.org/reference/create.html), to generate the folder of interest. The *devtools:create(PATH, open)* function:
+ Creates these files: DESCRIPTION, NAMESPACE, .gitignore, .Rbuildignore, and PkgTestR.Rproj
- the *.gitignore* file can be replaced by the contents in github's [R.gitignore](https://github.com/github/gitignore/blob/main/R.gitignore)
+ Creates the R folder.
+ The second optional parameter *open* will start a new session in RStudio desktop with the package opened. When opened, bottom right panel will be set to the project directory. On the left, in the console panel, top bar status should have the project directory as well. Note that a hidden folder *.Rproj.user*, which is ignored in the *.gitignore* file, might begin populating with temporary contents when working in RStudio with the project.
```{r, eval = FALSE}
devtools::create("G:/repos/PkgTestR", open=TRUE)
# β Creating 'G:/repos/PkgTestR/'
# β Setting active project to 'G:/repos/PkgTestR'
# β Creating 'R/'
# β Writing 'DESCRIPTION'
# Package: PkgTestR
# Title: What the Package Does (One Line, Title Case)
# Version: 0.0.0.9000
# Authors@R (parsed):
# * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
# Description: What the package does (one paragraph).
# License: `use_mit_license()`, `use_gpl3_license()` or friends to
# pick a license
# Encoding: UTF-8
# Roxygen: list(markdown = TRUE)
# RoxygenNote: 7.2.0
# β Writing 'NAMESPACE'
# β Writing 'PkgTestR.Rproj'
# β Adding '^PkgTestR\\.Rproj$' to '.Rbuildignore'
# β Adding '.Rproj.user' to '.gitignore'
# β Adding '^\\.Rproj\\.user$' to '.Rbuildignore'
# β Opening 'G:/repos/PkgTestR/' in new RStudio session
# β Setting active project to '<no active project>'
```
Second, go the the just created DESCRIPTION (e.g., "G:/repos/PkgTestR/DESCRIPTION") file, and modify Title and author information.
```{bash, eval = FALSE}
Package: PkgTestR
Title: Testing the Creation of a R Package
Version: 0.0.0.9000
Authors@R:
person("Fan", "Wang", , "fanwecon@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2640-5420"))
Description: Creating a R package and a website with pkgdown
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
```
Third, create a LICENSE file with for example [MIT License](https://en.wikipedia.org/wiki/MIT_License) in the root directory just created (e.g., [LICENSE](https://github.com/FanWangEcon/Tex4Econ/blob/master/nontex/rprjtemplate/LICENSE))
## Write a Function
Now, we write a function with markups, to be stored in a file in the */R* directory under root. Below, we have a slightly modified function from a [roxygen2](https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html) vignette. This function is in [R/ff_rtest_add.R](https://github.com/FanWangEcon/PkgTestR/blob/master/R/ff_rtest_add.R)
```{r, eval = FALSE}
#' Add together two numbers
#'
#' @description This function provides an example with Roxygen2 markups.
#' We can internally reference the sister function [ff_rtest_mul_basic()].
#'
#' @param ar_fl_x Scalar or vector of numbers
#' @param ar_fl_y Scalar or vector of numbers
#' @return The sum of \code{ar_fl_x} and \code{ar_fl_y}
#' @export
#' @examples
#' ff_rtest_add_basic(1, 1)
#' ff_rtest_add_basic(c(10,10), c(1,3))
#' @references
#' \url{https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html}
#' @seealso [ff_rtest_mul_basic()] for multiplication.
#' @author Fan Wang, \url{http://fanwangecon.github.io}
ff_rtest_add_basic <- function(ar_fl_x, ar_fl_y) {
ar_fl_sum <- ar_fl_x + ar_fl_y
return(ar_fl_sum)
}
```
## Document, Check, and Install Package
In the steps below, we will run the functions from devtools shown below to create and install a package with several functions and vignettes.
```{r, eval = FALSE}
# First
devtools::document(pkg="G:/repos/PkgTestR")
# Second
devtools::run_examples(pkg="G:/repos/PkgTestR")
devtools::build_vignettes(pkg="G:/repos/PkgTestR")
# Third
devtools::check(pkg="G:/repos/PkgTestR")
# Fourth
devtools::install(pkg="G:/repos/PkgTestR")
```
After running the file above, then can check from inside R *?ff_rtest_add_basic* and see the examples.
### devtools::document()
We will create documentations and install the package using [devtools](https://devtools.r-lib.org/).
First, we will document the function that we have created, using [devtools::document()](https://devtools.r-lib.org/reference/document.html). This updates *NAMESPACE* for functions that have *@export*, and also generate R documention files in the *man* folder.
```{r, eval = FALSE}
devtools::document(pkg="G:/repos/PkgTestR")
```
The function we have created, *ff_rtest_add_basic()*, now has a R documentation file, [man/ff_rtest_add_basic.Rd](https://github.com/FanWangEcon/PkgTestR/blob/master/man/ff_rtest_add_basic.Rd). The file is automatically generated by roxygen2, and looks like this:
```{rd, eval = FALSE}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ff_rtest_add.R
\name{ff_rtest_add_basic}
\alias{ff_rtest_add_basic}
\title{Add together two numbers}
\usage{
ff_rtest_add_basic(ar_fl_x, ar_fl_y)
}
\arguments{
\item{ar_fl_x}{Scalar or vector of numbers}
\item{ar_fl_y}{Scalar or vector of numbers}
}
\value{
The sum of \code{ar_fl_x} and \code{ar_fl_y}
}
\description{
This function provides an example with Roxygen2 markups.
We can internally reference the sister function \code{\link[=ff_rtest_mul_basic]{ff_rtest_mul_basic()}}.
}
\examples{
ff_rtest_add_basic(1, 1)
ff_rtest_add_basic(c(10,10), c(1,3))
}
\references{
\url{https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html}
}
\seealso{
\code{\link[=ff_rtest_mul_basic]{ff_rtest_mul_basic()}} for multiplication.
}
\author{
Fan Wang, \url{http://fanwangecon.github.io}
}
```
### Run Examples and Vignettes
In the second step, we run examples, which are testing specific functions as come from Roxygen2 example snippets that enter the help file for functions, and we also generate files based on vignettes, which are tutorial general guidance files that might or might not be associated with specific functions in the package.
#### devtools::run_examples()
We test the examples for the functions [devtools::run_examples](https://devtools.r-lib.org/reference/run_examples.html).
```{r, eval = FALSE}
devtools::run_examples(pkg="G:/repos/PkgTestR")
# βΉ Updating PkgTestR documentation
# βΉ Loading PkgTestR
# ββ Running 1 example files ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ PkgTestR ββ
# βΉ Loading PkgTestR
#
# > # Name: ff_rtest_add_basic
# > # Title: Add together two numbers
# > # Aliases: ff_rtest_add_basic
# >
# > # ** Examples
# >
# > ff_rtest_add_basic(1, 1)
# [1] 2
#
# > ff_rtest_add_basic(c(10,10), c(1,3))
# [1] 11 13
# βΉ Loading PkgTestR
```
#### devtools::build_vignettes()
We write a vignette file, following the instructions on [this page](https://kbroman.org/pkg_primer/pages/vignettes.html). In fact, this [present page (vignettes/ffv_devtools_create_package.Rmd)](https://github.com/FanWangEcon/PkgTestR/blob/master/vignettes/ffv_devtools_create_package.Rmd) is the vignette page. We can automatically create a vignette using [usethis::use_vignette](https://usethis.r-lib.org/reference/use_vignette.html). This will make some automated updates to DESCRIPTION and make other appropriate changes.
```{r, eval = FALSE}
usethis::use_vignette("ffv_pkgdown_github", title = "Generate Package Website with pkgdown and Github Pages")
# > usethis::use_vignette("ffv_pkgdown_github", title = "Generate Package Website with pkgdown and Github Pages")
# β Setting active project to 'G:/repos/PkgTestR'
# β Adding 'inst/doc' to '.gitignore'
# β Adding '*.html', '*.R' to 'vignettes/.gitignore'
# β Writing 'vignettes/ffv_pkgdown_github.Rmd'
# β’ Modify 'vignettes/ffv_pkgdown_github.Rmd'
```
Note that, front matter at the top of an otherwise standard markdown file makes this a vignette file:
```{md, eval = FALSE}
---
title: "Put the title of your vignette here"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Put the title of your vignette here}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```
Also note, need to add *VignetteBuilder* to the DESCRIPTION file under root:
```{bash, eval = FALSE}
Suggests: knitr, rmarkdown
VignetteBuilder: knitr
```
We use the [devtools::build_vignette](https://devtools.r-lib.org/reference/build_vignettes.html) function, which will generate a *doc* folder if that does not exist, and will store the HTML/R/RMD versions of the vignette file, with the same name, there. Note that this is different from where pkgdown will generate vignette files, which are to be a part of the project website.
```{r, eval = FALSE}
devtools::build_vignettes(pkg="G:/repos/PkgTestR")
# βΉ Building PkgTestR vignettes
# βΉ Moving ffv_devtools_create_package.html and ffv_devtools_create_package.R to doc/
# βΉ Copying ffv_devtools_create_package.Rmd to doc/
# βΉ Building vignette index
```
### devtools::check()
Third, we update the documentation, build and load the package, and check the package locally with [devtools::check](https://devtools.r-lib.org/reference/check.html). This can be very slow, but it will show errors, warnings, and notes. All errors and warnings have to be resolved before the package can be released to CRAN. Note that *check()* also updates the documentation .Rd files based on the functions and Roxygen2 markups in the R folder.
```{r, eval = FALSE}
devtools::check(pkg="G:/repos/PkgTestR")
# βΉ Updating PkgTestR documentation
# βΉ Loading PkgTestR
# ββ Building βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ PkgTestR ββ
# Setting env vars:
# β’ CFLAGS : -Wall -pedantic -fdiagnostics-color=always
# β’ CXXFLAGS : -Wall -pedantic -fdiagnostics-color=always
# β’ CXX11FLAGS: -Wall -pedantic -fdiagnostics-color=always
# β’ CXX14FLAGS: -Wall -pedantic -fdiagnostics-color=always
# β’ CXX17FLAGS: -Wall -pedantic -fdiagnostics-color=always
# β’ CXX20FLAGS: -Wall -pedantic -fdiagnostics-color=always
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# β checking for file 'G:\repos\PkgTestR/DESCRIPTION'
# β preparing 'PkgTestR':
# β checking DESCRIPTION meta-information ...
# β checking for LF line-endings in source and make files and shell scripts
# β checking for empty or unneeded directories
# β building 'PkgTestR_0.0.0.9000.tar.gz'
#
# ββ Checking βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ PkgTestR ββ
# Setting env vars:
# β’ _R_CHECK_CRAN_INCOMING_REMOTE_: FALSE
# β’ _R_CHECK_CRAN_INCOMING_ : FALSE
# β’ _R_CHECK_FORCE_SUGGESTS_ : FALSE
# β’ NOT_CRAN : true
# ββ R CMD check ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# β using log directory 'C:/Users/fan/AppData/Local/Temp/RtmpozS3Eh/PkgTestR.Rcheck' (361ms)
# β using R version 4.2.1 (2022-06-23 ucrt)
# β using platform: x86_64-w64-mingw32 (64-bit)
# β using session charset: UTF-8
# β using options '--no-manual --as-cran'
# β checking for file 'PkgTestR/DESCRIPTION' ...
# β this is package 'PkgTestR' version '0.0.0.9000'
# β package encoding: UTF-8
# β checking package namespace information ...
# β checking package dependencies (1.3s)
# β checking if this is a source package
# β checking if there is a namespace ...
# β checking for executable files (467ms)
# β checking for hidden files and directories ...
# β checking for portable file names ...
# β checking serialization versions ...
# β checking whether package 'PkgTestR' can be installed (1.8s)
# β checking installed package size ...
# β checking package directory
# β checking for future file timestamps ...
# β checking DESCRIPTION meta-information (391ms)
# β checking top-level files
# β checking for left-over files ...
# β checking index information
# β checking package subdirectories ...
# β checking R files for non-ASCII characters ...
# β checking R files for syntax errors ...
# β checking whether the package can be loaded ...
# β checking whether the package can be loaded with stated dependencies ...
# β checking whether the package can be unloaded cleanly ...
# β checking whether the namespace can be loaded with stated dependencies ...
# β checking whether the namespace can be unloaded cleanly ...
# β checking loading without being on the library search path ...
# β checking dependencies in R code ...
# β checking S3 generic/method consistency (561ms)
# β checking replacement functions ...
# β checking foreign function calls ...
# β checking R code for possible problems (3.1s)
# β checking Rd files (344ms)
# β checking Rd metadata ...
# β checking Rd line widths ...
# β checking Rd cross-references ...
# β checking for missing documentation entries ...
# β checking for code/documentation mismatches (703ms)
# β checking Rd \usage sections (778ms)
# β checking Rd contents ...
# β checking for unstated dependencies in examples ...
# β checking examples (656ms)
# β checking for non-standard things in the check directory ...
# β checking for detritus in the temp directory ...
#
#
# ββ R CMD check results βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ PkgTestR 0.0.0.9000 ββββ
# Duration: 15s
#
# 0 errors β | 0 warnings β | 0 notes β
```
### devtools::install()
Using [devtools::install](https://devtools.r-lib.org/reference/install.html), install the package locally.
```{r, eval = FALSE}
# devtools::build(pkg="G:/repos/PkgTestR", path="G:/repos/", manual=TRUE)
devtools::install(pkg="G:/repos/PkgTestR", build=TRUE, force=TRUE)
# β checking for file 'G:\repos\PkgTestR/DESCRIPTION' (353ms)
# β preparing 'PkgTestR':
# β checking DESCRIPTION meta-information ...
# β checking for LF line-endings in source and make files and shell scripts
# β checking for empty or unneeded directories
# β building 'PkgTestR_0.0.0.9000.tar.gz'
# Running "C:/PROGRA~1/R/R-42~1.1/bin/x64/Rcmd.exe" INSTALL \
# "C:\Users\fan\AppData\Local\Temp\RtmpozS3Eh/PkgTestR_0.0.0.9000.tar.gz" --install-tests
# * installing to library 'C:/Users/fan/AppData/Local/R/win-library/4.2'
# * installing *source* package 'PkgTestR' ...
# ** using staged installation
# ** R
# ** byte-compile and prepare package for lazy loading
# ** help
# *** installing help indices
# ** building package indices
# ** testing if installed package can be loaded from temporary location
# ** testing if installed package can be loaded from final location
# ** testing if installed package keeps a record of temporary installation path
# * DONE (PkgTestR)
```
## Build Binary and Manual
To share the package, we build a *.tar.gz* file, which is by default, if path is not specified, stored in the parent folder of the current package. We also create a manual for using the functions' Roxygen2 markups.
First, building the binary.
```{r, eval = FALSE}
# Update/build the binary
devtools::build(pkg="G:/repos/PkgTestR", path="G:/repos/")
# β checking for file 'G:\repos\PkgTestR/DESCRIPTION'
# β preparing 'PkgTestR':
# β checking DESCRIPTION meta-information ...
# β installing the package to build vignettes
# β creating vignettes (4.8s)
# β checking for LF line-endings in source and make files and shell scripts
# β checking for empty or unneeded directories
# β building 'PkgTestR_0.0.0.9000.tar.gz'
#
# [1] "G:/repos//PkgTestR_0.0.0.9000.tar.gz"
```
Second, creating the PDF Manual. This combines .Rd files together and comverts to PDF.
```{r, eval = FALSE}
# Create PDF
devtools::build_manual(pkg="G:/repos/PkgTestR", path="G:/repos/")
# Hmm ... looks like a package
# Converting Rd files to LaTeX
# Creating pdf output from LaTeX ...
# This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/W32TeX) (preloaded format=pdflatex)
# restricted \write18 enabled.
# ...
# ...
# Output written on Rd2.pdf (3 pages, 67578 bytes).
# Transcript written on Rd2.log.
# Saving output to 'G:/repos//PkgTestR_0.0.0.9000.pdf' ...
# Done
```
## Using an Installed Package
After we have developed a function, with proper documentation/markup, and also vignette, we can now install the function, load it in memory, and start using the function.
When working with development code, conveninent to use the [devtools::load_all](https://devtools.r-lib.org/reference/load_all.html) function, which will take source code, and put it in memory.
```{r, eval = FALSE}
# Load all code to memory
devtools::load_all(path="G:/repos/PkgTestR")
# Check out documentations for each function
?PkgTestR::ff_rtest_add_basic
?PkgTestR::ff_rtest_mul_basic
# Test the functions
PkgTestR::ff_rtest_add_basic(1,2)
PkgTestR::ff_rtest_mul_basic(1,2)
# Show all Vignettes
vignette(package="PkgTestR")
# Show a particular Vignette
vignette("ffv_devtools_create_package", package="PkgTestR")
```
## pkgdown Build Site
Use [pkgdown](https://pkgdown.r-lib.org/articles/pkgdown.html) to create a website to show the package. See the page [Generate Package Website with pkgdown and Github Pages](https://fanwangecon.github.io/PkgTestR/articles/ffv_pkgdown_github.html) for more information.
```{r, eval = FALSE}
# Run once to configure package to use pkgdown
usethis::use_pkgdown()
# Run to build the website
pkgdown::build_site()
```
## Errors
### Could not find function
After writing a function, while running *devtools::run_examples()*, get the error message "could not find function "function_name"". This is likely because the function was not exported, can be [exported like this](https://github.com/FanWangEcon/PkgTestR/blob/02a14365edd621d401bcaa71a95e4d8686559e3a/R/ff_rtest_add.R#L9), which would add the function to [NAMESPACE](https://github.com/FanWangEcon/PkgTestR/blob/master/NAMESPACE)
### Errors During Check
At the end of check, if the following message appears, that means we need to include *devtools* as the suggest field of DESCRIPTION. See [here](https://github.com/Bioconductor/BiocWorkshops2019/issues/32#issue-454624956).
```{bash, eval = FALSE}
checking for unstated dependencies in vignettes ... NOTE
'::' or ':::' import not declared from: 'devtools'
'library' or 'require' call not declared from: 'devtools'
```