From 71e54b3f1f96d86405401b8b0bdcd287e8109330 Mon Sep 17 00:00:00 2001 From: Rob Hyndman Date: Mon, 11 Mar 2024 15:38:38 +1100 Subject: [PATCH] Updated week 3 --- week3/doubler.R | 20 +++--- week3/index.qmd | 1 + week3/slides.qmd | 160 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 158 insertions(+), 23 deletions(-) diff --git a/week3/doubler.R b/week3/doubler.R index 1e61e9c..f4c599c 100644 --- a/week3/doubler.R +++ b/week3/doubler.R @@ -1,3 +1,5 @@ +devtools::dev_sitrep() + library(devtools) # Create package with one function that doubles numbers create_package("/tmp/doubler") @@ -5,21 +7,26 @@ setwd("/tmp/doubler") use_git() use_gpl3_license() -load_all() # Or Ctrl-L use_r("dblr") +document() +build() +install() +library(doubler) dblr(5) dblr(3 + 2i) dblr("A") dblr(TRUE) +load_all() # Or Ctrl-L + check() -document() -build() -install() -library(doubler) +use_readme_rmd() +build_readme() + +usethis::use_vignette("introduction") use_testthat() use_test("dblr") @@ -27,6 +34,3 @@ test() use_package() use_github() -use_readme_rmd() -check() -install() diff --git a/week3/index.qmd b/week3/index.qmd index 2b1eb39..4c5206a 100644 --- a/week3/index.qmd +++ b/week3/index.qmd @@ -19,6 +19,7 @@ schedule |> ## What you will learn this week +* Conditions * Package metadata * Package documentation * Package checks and tests diff --git a/week3/slides.qmd b/week3/slides.qmd index 22ad500..43c3e59 100644 --- a/week3/slides.qmd +++ b/week3/slides.qmd @@ -29,6 +29,133 @@ source(here::here("course_info.R")) \vspace*{0.4cm} \tableofcontents +# Conditions + +## Conditions + +```r +message("This is what a message looks like") +#> This is what a message looks like + +warning("This is what a warning looks like") +#> Warning: This is what a warning looks like + +stop("This is what an error looks like") +#> Error in eval(expr, envir, enclos): This is what an error looks like +``` + +\pause + +* Ignore messages with `suppressMessages()`. +* Ignore warnings with `suppressWarnings()`. +* Ignore errors with `try()`. + +## try() + +* Allows execution to continue even if an error occurs. +* Returns a special object that captures the error. + +\fontsize{10}{10}\sf + +:::: {.columns} +::: {.column width="50%"} + +```{r, error = TRUE} +f1 <- function(x) { + log(x) + 10 +} +f1("x") +``` + +::: +::: {.column width="50%"} + +```{r, error = TRUE} +f2 <- function(x) { + try(log(x)) + 10 +} +f2("a") +``` + +::: +:::: + +## Handling conditions +\fontsize{13}{15}\sf + +Allow you to specify what should happen when a condition occurs. + +```{r, eval = FALSE} +tryCatch( + error = function(cnd) { + # code to run when error is thrown + }, + code_to_run_while_handlers_are_active +) +withCallingHandlers( + warning = function(cnd) { + # code to run when warning is signalled + }, + message = function(cnd) { + # code to run when message is signalled + }, + code_to_run_while_handlers_are_active +) +``` + +## tryCatch() +\fontsize{10}{10}\sf + +```{r} +f3 <- function(x) { + tryCatch( + error = function(cnd) NA, + log(x) + ) +} + +f3("x") +``` + + +## withCallingHandlers() +\fontsize{10}{10}\sf + +```{r} +f4 <- function(x) { + withCallingHandlers( + warning = function(cnd) cat("How did this happen?\n"), + log(x) + ) +} + +f4(-1) +``` + + +## Exercise + +Explain the results of running the following code + +```{r, eval = FALSE} +show_condition <- function(code) { + tryCatch( + error = function(cnd) "error", + warning = function(cnd) "warning", + message = function(cnd) "message", + { + code + 5 + } + ) +} +show_condition(stop("!")) +show_condition(10) +show_condition(warning("?!")) +``` + # Getting started ## System setup @@ -81,7 +208,7 @@ install.packages(c("devtools", "roxygen2", "testthat", "knitr")) * The `DESCRIPTION` file is the principal way to declare dependencies; we don’t do this via `library(somepackage)`. * Be explicit about which functions are user-facing and which are internal helpers. By default, functions are not exported. -## Getting started +## Exercise: Start on your package ```{r} #| eval: false @@ -134,7 +261,7 @@ Config/testthat/edition: 3 * **Depends**: packages that are attached with your package. (Not needed for most packages.) * **Imports**: packages that are used in your package. (Refer to functions using `pkg::fun()`.) * **Suggests**: packages that are used in your package, but not required. (E.g., in tests or examples.) -* **LazyData**: `true` prevents users using `data()`. +* **LazyData**: `true` prevents users having to use `data()`. ## DESCRIPTION file @@ -215,10 +342,23 @@ use_package_doc() "object" ``` +## README.Rmd + +1. Describe the high-level purpose of the package. +2. A simple example illustrating package. +3. Installation instructions +4. An overview of the main components of the package. + +* Like a short vignette +* Displayed on the Github repository and the front page of the pkgdown site. +* Create with `usethis::use_readme_rmd()` +* Build with `devtools::build_readme()` + ## Vignettes * A long-form guide to your package, or an extended example. -* `usethis::use_vignette("my-vignette")` + + * `usethis::use_vignette("my-vignette")` * Creates a `vignettes/` directory. * Adds the necessary dependencies to `DESCRIPTION` * Drafts a vignette, `vignettes/my-vignette.Rmd`. @@ -258,17 +398,7 @@ library(yourpackage) * Any package used in a vignette must be included in Suggests if not already in Imports. -## README.Rmd - -1. Describe the high-level purpose of the package. -2. A simple example illustrating package. -3. Installation instructions -4. An overview of the main components of the package. -* Like a short vignette -* Displayed on the Github repository and the front page of the pkgdown site. -* Create with `usethis::use_readme_rmd()` -* Build with `devtools::build_readme()` ## NEWS @@ -299,7 +429,7 @@ library(yourpackage) ## testthat v3 -* `usethis::use_testthat(3)` +* `usethis::use_testthat()` * Create a `tests/testthat/` directory. * Add `testthat` to the Suggests field in DESCRIPTION and specify `testthat 3e` in the `Config/testthat/edition` field. * Create a file `tests/testthat.R` that runs all your tests when `check()` runs. @@ -334,7 +464,7 @@ library(yourpackage) * Updates `.Rbuildignore` * Adds `docs` to `.gitignore` * `pkgdown::build_site()` to build the site. -* `usethis::use_pkgdown_github_pages()` to publish the site via Gituhb Actiosn and GitHub Pages. +* `usethis::use_pkgdown_github_pages()` to publish the site via GitHub Actions and GitHub Pages. * Make a hex sticker with the `hexSticker` package. * Add it using `usethis::use_logo()`.