10 biocthis introduction

Instructor: Leo

10.2 biocthis main commands

These are the main steps you will need to know to make a Bioconductor package with biocthis:

  1. You first will need to create a package using a command from usethis.
  • For example: usethis::create_package("~/Desktop/cshl2024pkg")
  1. Now that you have a package, we can use biocthis to create 4 template R scripts that will guide you and help you make the full structure for a Bioconductor R package.
  • On your new R package (cshl2024pkg), we can now use biocthis::use_bioc_pkg_templates().

In part these commands were born out of my own self interest to make it easier to make new packages instead of copy-pasting the contents of an older one, then manually adjusting all the pieces for a new package. See https://lcolladotor.github.io/pkgs/ for the list of all the R packages I’ve been involved in.

10.3 Live demo

Here is the live demo result https://github.com/lcolladotor/cshl2024pkg/ with its companion documentation website at https://lcolladotor.github.io/cshl2024pkg/. You might also want to check the 2023 version at https://github.com/lcolladotor/cshl2024pkg/.

Check the git commit history at https://github.com/lcolladotor/cshl2024pkg/commits/devel and the GitHub Actions history at https://github.com/lcolladotor/cshl2024pkg/actions. We can see at https://app.codecov.io/gh/lcolladotor/cshl2024pkg the code coverage results for this demonstration package.

10.3.1 Example function

Let’s have a function to work with: weekday_praise().

weekday_praise <- function(date = Sys.Date()) {
    date <- as.Date(date)
    date_weekday <- weekdays(date)
    paste0(date_weekday, ": ", praise::praise())
}
weekday_praise()
#> [1] "Tuesday: You are perfect!"
weekday_praise("2024-06-09")
#> [1] "Sunday: You are excellent!"

Here’s the full code for the function and its documentation.

#' Praise a weekday
#'
#' Given a date, figure out which weekday it was, then write a positive
#' message.
#'
#' @param date A `base::Date` object or a `character()` in a format that can be
#' converted to a `base::Date` object with `base::as.Date()`.
#'
#' @importFrom praise praise
#' @export
#' @examples
#'
#' ## Praise the current weekday
#' weekday_praise()
#'
#' ## Praise the date we started teaching
#' weekday_praise("2024-06-09")
#'
#' ## Praise the current weekday in a reproducible way
#' set.seed(20240610)
#' weekday_praise()
#'
#' ## Verify that it's reproducible
#' set.seed(20240610)
#' weekday_praise()
weekday_praise <- function(date = Sys.Date()) {
    date <- as.Date(date)
    date_weekday <- weekdays(date)
    paste0(date_weekday, ": ", praise::praise())
}

Here’s a test for our function too.

library("testthat")
#> 
#> Attaching package: 'testthat'
#> The following objects are masked from 'package:rlang':
#> 
#>     is_false, is_null, is_true
#> The following object is masked from 'package:Hmisc':
#> 
#>     describe

## Verify that we get the result we wanted
set.seed(20240610)
expect_equal(weekday_praise("2024-06-09"), "Sunday: You are wondrous!")

## Verify that we get an error if the input is not correct
expect_error(weekday_praise("240609"))

## Should work for a vector input
expect_equal(length(weekday_praise(c("2024-06-09", "2024-06-10"))), 2L)

10.4 Community

For more materials on R/Bioconductor package development check http://contributions.bioconductor.org/.

biocthis is one of the reasons for my 2021 Bioconductor community award :-)

Do you want to play an active role? Join the cloud-working-group Slack channel.

© 2011-2023. All thoughts and opinions here are my own. The icon was designed by Mauricio Guzmán and is inspired by Huichol culture; it represents my community building interests.

Published with Bookdown