@@ -11,26 +11,24 @@ author: Alessandro Gasparini
1111
1212# Introduction
1313
14- ## What You Know Already
14+ ## What you know already
1515
16- - Packages provide a mechanism for loading optional code, data, and
17- documentation
16+ - Packages provide a mechanism for loading optional code, data, and documentation
1817- A library is a directory into which packages are installed
1918- ` install.packages() ` is used to install packages into the library
2019- ` library() ` is used to load and attach packages from the library
21- - "Attach" means that the package is put in your ` search ` list --- objects in
22- the package can be used directly
20+ - "Attach" means that the package is put in your ` search ` list --- objects in the package can be used directly
2321- Remember that ** package $\neq$ library**
2422
25- ## What We Want to Talk About Now
23+ ## What we want to talk about now
2624
2725- How to write, build, test, and check your own package ` r emoji::emoji("blush") `
2826- How to do this in a methodical and sustainable way
2927- Give tips and tricks based on practical experience
3028
31- # Contents of a Package
29+ # Contents of a package
3230
33- ## How is a Package Structured ?
31+ ## How is a package structured ?
3432
3533Package source = directory with files and subdirectories
3634
@@ -54,7 +52,7 @@ Package source = directory with files and subdirectories
5452:::
5553:::
5654
57- ## How to Get Started Quickly
55+ ## How to get started quickly
5856
5957Once upon a time, developers would set up this structure manually ` r emoji::emoji("yawning_face") `
6058
@@ -65,7 +63,7 @@ Nowadays, it's super fast with:
6563
6664![ ] ( resources/rstudio-rpackage.png ) {.r-stretch}
6765
68- ## ` DESCRIPTION ` File
66+ ## ` DESCRIPTION ` file
6967
7068- * Package* : Choose the name of your package
7169 - Not unimportant!
@@ -76,7 +74,7 @@ Nowadays, it's super fast with:
7674- * Authors@R* : Add authors and maintainer
7775- * Description* : Like an abstract, including references
7876
79- ## ` DESCRIPTION ` File (cont'd)
77+ ## ` DESCRIPTION ` file (cont'd)
8078
8179- * License* : Important for open sourcing
8280 - Consider permissive licenses such as Apache and MIT
@@ -88,7 +86,7 @@ Nowadays, it's super fast with:
8886- * Suggests* : Packages for documentation processing (` roxygen2 ` ), running
8987 examples, tests (` testthat ` ), vignettes
9088
91- ## ` R ` Folder
89+ ## ` R ` folder
9290
9391- Only contains R code files (recommended to use ` .R ` suffix)
9492 - Can create a file with ` usethis::use_r("filename") `
@@ -105,7 +103,7 @@ Nowadays, it's super fast with:
105103NULL
106104```
107105
108- ## ` NAMESPACE ` File
106+ ## ` NAMESPACE ` file
109107
110108``` {r}
111109#| echo: true
@@ -118,13 +116,12 @@ export(package_used_second)
118116export(package_used_third)
119117```
120118
121- - Defines the namespace of the package, to work with R's namespace management
122- system
119+ - Defines the namespace of the package, to work with R's namespace management system
123120- Namespace directives in this file allow to specify:
124121 - Which objects are exported to users and other packages
125122 - Which are imported from other packages
126123
127- ## ` NAMESPACE ` File (cont'd)
124+ ## ` NAMESPACE ` file (cont'd)
128125
129126- Controls the search strategy for variables:
130127 1 . Local (in the function body etc.)
@@ -133,34 +130,28 @@ export(package_used_third)
133130 4 . Base namespace
134131 5 . Normal ` search() ` path
135132
136- ## ` man ` Folder
133+ ## ` man ` folder
137134
138- - Contains documentation files for the objects in the package in the ` .Rd `
139- format
135+ - Contains documentation files for the objects in the package in the ` .Rd ` format
140136 - The syntax is a bit similar to ` LaTeX `
141137- All user level objects should be documented
142138- Internal objects don't need to be documented --- but I recommend it!
143- - Once upon a time, developers would set up these ` .Rd ` files and the
144- ` NAMESPACE ` manually ` r emoji::emoji("yawning_face") `
139+ - Once upon a time, developers would set up these ` .Rd ` files and the ` NAMESPACE ` manually ` r emoji::emoji("yawning_face") `
145140- Fortunately, nowadays we have ` roxygen2 ` ! ` r emoji::emoji("rocket") `
146141
147- ## ` roxygen2 ` to the Rescue !
142+ ## ` roxygen2 ` to the rescue !
148143
149- - We can include the documentation source directly in the R script on top of
150- the objects we are documenting
151- - Syntax is composed of special comments ` #' ` and special macros preceded with
152- ` @ `
153- - In RStudio, running Build > More > Document will render the ` .Rd ` files and
154- the ` NAMESPACE ` file for you
144+ - We can include the documentation source directly in the R script on top of the objects we are documenting
145+ - Syntax is composed of special comments ` #' ` and special macros preceded with ` @ `
146+ - In RStudio, running Build > More > Document will render the ` .Rd ` files and the ` NAMESPACE ` file for you
155147- Get started with ` usethis::use_roxygen_md() `
156- - Placing your cursor inside a function in RStudio, create a ` roxygen2 ` skeleton
157- with Code > Insert Roxygen Skeleton
148+ - Placing your cursor inside a function in RStudio, create a ` roxygen2 ` skeleton with Code > Insert Roxygen Skeleton
158149
159150## Setting up ` roxygen2 ` in your project
160151
161152![ ] ( resources/roxygen2-setting.png ) {.r-stretch}
162153
163- ## ` roxygen2 ` Source
154+ ## ` roxygen2 ` source
164155
165156` R/my_sum.R ` :
166157
@@ -187,7 +178,7 @@ my_sum <- function(x, y) {
187178}
188179```
189180
190- ## ` roxygen2 ` Output
181+ ## ` roxygen2 ` output
191182
192183` man/my_sum.Rd ` :
193184
@@ -222,7 +213,7 @@ my_sum(1, 2)
222213}
223214```
224215
225- ## ` roxygen2 ` Output (cont'd)
216+ ## ` roxygen2 ` output (cont'd)
226217
227218` NAMESPACE ` :
228219
@@ -232,7 +223,7 @@ my_sum(1, 2)
232223export(my_sum )
233224```
234225
235- ## ` tests ` Folder
226+ ## ` tests ` folder
236227
237228- Where store the unit tests covering the functionality of the package
238229- Get started with ` usethis::use_testthat() ` and ` usethis::use_test() ` and
@@ -241,7 +232,7 @@ export(my_sum)
241232 into R scripts directly in ` tests ` directory
242233- We will look at unit tests in detail later
243234
244- ## ` data ` Folder
235+ ## ` data ` folder
245236
246237- For (example) data that you ship in your package to the user
247238 - Get started with ` usethis::use_data() `
@@ -250,7 +241,7 @@ export(my_sum)
250241- If you generate the example data, save the R script, too
251242 - Put that into ` data-raw ` folder, start with ` usethis::use_data_raw() `
252243
253- ## ` inst ` Folder
244+ ## ` inst ` folder
254245
255246- Contents will be copied recursively to installation directory
256247 - Be careful not to interfere with standard folder names
@@ -261,7 +252,7 @@ export(my_sum)
261252 - Create it with ` usethis::use_citation() `
262253- ` inst/doc ` can contain documentation files (typically ` pdf ` )
263254
264- ## ` src ` Folder
255+ ## ` src ` folder
265256
266257- Contains sources and headers for any code that needs compilation
267258- Should only contain a single language here
@@ -272,7 +263,7 @@ export(my_sum)
272263 - Wrapping existing libraries for use in R
273264 - Speeding up complex or long computations
274265
275- ## ` vignettes ` Folder
266+ ## ` vignettes ` folder
276267
277268- Special case of documentation files (` pdf ` or ` html ` ) created by compiling
278269 source files
@@ -283,7 +274,7 @@ export(my_sum)
283274- Important for the user to understand the high-level ideas
284275- Complements function-level documentation from our ` roxygen2 ` chunks
285276
286- ## ` NEWS ` File
277+ ## ` NEWS ` file
287278
288279- Lists user-visible changes worth mentioning
289280- In each new release, add items at the top under the version they refer to
@@ -297,7 +288,7 @@ export(my_sum)
297288- We mentioned before that licensing information is usually included in the ` DESCRIPTION ` file
298289- In fact, the ` License ` field (in standardized form) is mandatory
299290- Licensing for a package which might be distributed is an important but potentially complex subject
300- - It is very important to include license information, as otherwise:
291+ - It is very important to include licensing information, as otherwise:
301292 - It may not be possible to use it
302293 - It may not be possible to distribute copies of it
303294- We are going to talk more about licensing in Chapter 5 later today
@@ -323,9 +314,10 @@ License: Artistic-2.0 | AGPL-3 + file LICENSE
323314- The optional file ` LICENSE ` /` LICENCE ` contains a copy of the license
324315 - Only include such a file if it is referred to in the ` License ` field
325316
326- ## Adding a license to your package
317+ ## Adding a license to your package {.scrollable}
327318
328319- Once again, functions from the usethis package simplify this process
320+
329321- Possible options:
330322 - ` usethis::use_mit_license(copyright_holder = NULL) `
331323 - ` usethis::use_gpl_license(version = 3, include_future = TRUE) `
@@ -336,18 +328,17 @@ License: Artistic-2.0 | AGPL-3 + file LICENSE
336328 - ` usethis::use_ccby_license() `
337329 - ` usethis::use_proprietary_license(copyright_holder) `
338330
339- # Building the Package
331+ # Building the package
340332
341- ## Documenting the Package
333+ ## Documenting the package
342334
343335- The first step is to produce the documentation files and ` NAMESPACE `
344336- In RStudio: Build > More > Document
345337- In the console: ` devtools::document() `
346338
347- ## Checking the Package
339+ ## Checking the package
348340
349- - R comes with pre-defined check command for packages:
350- "the R package checker" aka ` R CMD check `
341+ - R comes with pre-defined check command for packages: "the R package checker" aka ` R CMD check `
351342- About 22 checks are run (so quite a lot), including things like:
352343 - Can the package be installed?
353344 - Is the code syntax ok?
@@ -357,28 +348,26 @@ License: Artistic-2.0 | AGPL-3 + file LICENSE
357348- In RStudio: Build > Check
358349- In the console: ` devtools::check() `
359350
360- ## Building the Package
351+ ## Building the package
361352
362353- The R package folder can be compressed into a single package file
363354- Typically we manually only build "source" package
364355 - In RStudio: Build > More > Build Source Package
365356 - In the console: ` devtools::build() `
366357- Makes it easy to share the package with others and submit to CRAN
367358
368- ## Installing the Package
359+ ## Installing the package
369360
370- - R comes with pre-defined install command for packages:
371- ` R CMD INSTALL `
361+ - R comes with pre-defined install command for packages: ` R CMD INSTALL `
372362- In RStudio: Build > Install
373363- In the console: ` devtools::install() `
374- - Note: During development it's usually sufficient to use Build > More > Load
375- All
364+ - Note: During development it's usually sufficient to use Build > More > Load All
376365 - Runs ` devtools::load_all() `
377366 - Roughly simulates what happens when package would be installed and loaded
378367 - ** Unexported** objects and helpers under ` tests ` will also be available
379368 - Key: much faster!
380369
381- ## Keyboard Shortcuts
370+ ## Keyboard shortcuts
382371
383372- Learning a few keyboard shortcuts can speed up many of the tasks we introduced in this lecture:
384373
@@ -389,7 +378,7 @@ readr::read_csv(here::here("slides/resources/keyboard-shortcuts.csv"), col_types
389378 knitr::kable(align = "ccc")
390379```
391380
392- - There are many more – check the [ RStudio IDE Documentation] ( https://docs.posit.co/ide/user/ide/reference/shortcuts.html )
381+ - There are many more – check for instance the [ RStudio IDE Documentation] ( https://docs.posit.co/ide/user/ide/reference/shortcuts.html )
393382
394383# Exercise
395384
@@ -405,7 +394,7 @@ readr::read_csv(here::here("slides/resources/keyboard-shortcuts.csv"), col_types
4053941 . Run checks
4063951 . Build the package
407396
408- > We will be using this package throughout the day!
397+ $\leadsto$ We will be using this package throughout the day!
409398
410399# References
411400
0 commit comments