Skip to content

Instantly share code, notes, and snippets.

@moodymudskipper
Last active April 2, 2023 14:25
Show Gist options
  • Save moodymudskipper/5f227be37c5d9a6b66fe8fa6a2608d32 to your computer and use it in GitHub Desktop.
Save moodymudskipper/5f227be37c5d9a6b66fe8fa6a2608d32 to your computer and use it in GitHub Desktop.
Custom roxygen2 tags : "tip" example
# You'll also need to add roxygen2 to Suggests in the DESCRIPTION file to satisfy checks
# or just call `usethis::use_package("roxygen2", "Suggests")`
# first we define the tag used in examples in the doc ----------------------------------------------
#' @export
#' @importFrom roxygen2 roxy_tag_parse
roxy_tag_parse.roxy_tag_tip <- function(x) {
roxygen2::tag_markdown(x)
}
#' @export
#' @importFrom roxygen2 roxy_tag_rd
roxy_tag_rd.roxy_tag_tip <- function(x, base_path, env) {
roxygen2::rd_section("tip", x$val)
}
#' @export
format.rd_section_tip <- function(x, ...) {
paste0(
"\\section{Tips and tricks}{\n",
"\\itemize{\n",
paste0(" \\item ", x$value, "\n", collapse = ""),
"}\n",
"}\n"
)
}
# now we figure out a way to have tags without parameters -------------------------------------
#' @export
#' @importFrom roxygen2 roxy_tag_parse
roxy_tag_parse.roxy_tag_thanks <- function(x) {
# not sure if this is the idiomatic way but that's how I made zero parameter tags work
# they need `x$raw` not to be empty, so just set a value that we won't use
x$raw <- "."
roxygen2::tag_markdown(x)
}
#' @export
#' @importFrom roxygen2 roxy_tag_rd
roxy_tag_rd.roxy_tag_thanks <- function(x, base_path, env) {
roxygen2::rd_section("thanks", x$val)
}
#' @export
format.rd_section_thanks <- function(x, ...) {
items <- c("Mom", "Dad", "Friends")
paste0(
"\\section{Thanks}{\n",
"\\itemize{\n",
paste0(" \\item ", items, "\n", collapse = ""),
"}\n",
"}\n"
)
}
# A function that uses those tags -------------------------------------------------
#' fun
#'
#' @tip The mean of a logical vector is the proportion of `TRUE` values.
#' @tip You can compute means of dates and date-times!
#' @thanks
#' @export
fun <- function() {
NULL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment