packageVersion("devtools")
[1] '2.4.5'
R
PackagesSciencesPo Intro To Programming 2024
11 October, 2024
Questions
R
package?R
package?Objectives
RStudio
-powered package development workflow.We have been using R
packages all the time.
Each time we say library(xyz)
we are using external code provided in the xyz
package.
You can write your own packages.
What’s the point of Packages?
R
functionality.r-pkgs
book!RStudio
for the winWe do this in RStudio
we use the devtools
package
check you have a recent version:
Package: toypackage
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.3.2
git
.usethis
package. This is loaded by default when attaching the devtools
package (use_git
is part of usethis
…)Yes
to everything ✌️R
source code in the R/
folder..R
files as you want.use_test()
thing? 🤔 Let’s worry about this later.R/sayhello.R
:# Notice I'm using = instead of < - because
# the font of those slides prints it weirdly
hello = function(who){
paste("hello,",who)
}
R
script, we could source
the R/sayhello.R
file into global space and try this out.load_all()
:load_all()
load_all()
function simulates the process of building, installing, and attaching the toypackage
package.R
has a rigid set of rules for what a package needs to look like.DESCRIPTION
file (or type Ctrl + .
and start typing desc
)Let’s
hello
function, place the cursor inside the function body, and do Code > Insert Roxygen Skeleton
.#' Title
#'
#' @param who
#'
#' @return
#' @export
#'
#' @examples
hello <- function(who){
paste("hello,",who)
}
#'
is part of the docstring.roxygen
package can separate those blocks from our code, and produce valid R
documentation for us! 🤯document()
function.@export
tag in the docstring?document()
, roxygen changed the NAMESPACE
file based upon that tag.NAMESPACE
specify what is visible to a user who does library(toypackage)
.@export
tag, and document()
again. Look back at NAMESPACE
!check()
again!install.packages("ggplot2")
)R
installs your packages here:install()
👏
The Time Factor
If you come back to this in 2 months time you probably
The Scale Factor
As your package grows, you will find it hard to come back to all components repeatedly, making sure they all still work as intended (now that they may depend on other parts of your code)
R/
folder is covered by a corresponding test.What Is a Test?
The purpose of a test is to verify that some part of your code, a function in most cases, works as intended.
'tests/testthat/test-sayhello.R'
like sotest_that("hello function works", {
who = "James T. Kirk"
expect_equal(hello(who), paste("hello,",who))
})
library(testthat)
first)> test()
ℹ Testing toypackage
Attaching package: ‘testthat’
The following object is masked from ‘package:devtools’:
test_file
✔ | F W S OK | Context
✔ | 1 | sayhello
══ Results ══════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 1 ]
export
some of our functions, we now may want to import
some functions from elsewhere.dplyr
package:> use_package("dplyr")
✔ Adding 'dplyr' to Imports field in DESCRIPTION
• Refer to functions with `dplyr::fun()`
DESCRIPTION
file to see what happened.use_github()
usethis::use_readme_rmd()
function is perfect for this:install
in build
tab)r-pkgs
dedicated to this!
🚨 Now we are entering the seriously cool zone of R package development 😎
Wouldn’t it be 🤩 amazing if all of our package documentation, the content of our readme, and any explanatory articles we might have written as vignettes, were available on a (free to host!) website which is always up to date?
Key Points
RStudio
greatly facilitates R
package development.R
packages contain code, data and documentation in highly structured fashion.