From 6247ba8e0e9b0d3d69777147af1ba0618b9ebf63 Mon Sep 17 00:00:00 2001 From: Mauro Lepore Date: Sun, 3 Nov 2024 11:04:35 +0000 Subject: [PATCH] New 'url_template' (#56) --- DESCRIPTION | 2 +- R/reference_package.R | 12 ++++++++- README.Rmd | 6 +++-- README.md | 34 +++++++++++++++++-------- inst/WORDLIST | 19 +++----------- man/reference_concept.Rd | 13 +++++++++- man/reference_package.Rd | 13 +++++++++- tests/testthat/test-reference_package.R | 11 ++++++++ 8 files changed, 79 insertions(+), 31 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 10235f3..688fda0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,4 +31,4 @@ Encoding: UTF-8 Language: en-US LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 diff --git a/R/reference_package.R b/R/reference_package.R index c3a1ddd..31e9727 100644 --- a/R/reference_package.R +++ b/R/reference_package.R @@ -6,6 +6,10 @@ #' meta-package. #' #' @param x A character vector giving concepts or package names to match. +#' @param url_template Character. A template to generate links to documentation +#' based on the column names of the output -- typically `package` and `topic`, +#' e.g. `"https://maurolepore.github.io/{package}/reference/{topic}.html"` +#' (`glue::glue()` syntax). #' @param url Character vector of length-1 giving a base url, e.g. #' "https://maurolepore.github.io/". #' @param strip_s3class `TRUE` removes the class component of S3 methods. @@ -23,11 +27,17 @@ NULL reference_any <- function(doc) { force(doc) - function(x, url = NULL, packages = NULL, strip_s3class = TRUE) { + function(x, url_template = NULL, url = NULL, packages = NULL, strip_s3class = TRUE) { warn_unnattached(x, doc) pick <- pick_doc(packages = packages, doc = doc, x = x) result <- tidy_reference(may_add_url(pick, url), strip_s3class) + + if (!is.null(url_template)) { + result <- mutate(result, topic = paste0("", .data$topic, "") + ) + } + result } } diff --git a/README.Rmd b/README.Rmd index a57208b..0d32472 100644 --- a/README.Rmd +++ b/README.Rmd @@ -64,8 +64,10 @@ utilities You may link to documentation online. ```{r} -template <- "?" -linked <- transform(datasets, topic = sprintf(template, package, topic)) +url_template <- "https://www.rdocumentation.org/packages/{package}/versions/3.6.2/topics/{topic}.html" +linked <- reference_package("datasets", url_template = url_template) +linked["topic"] + knitr::kable(head(linked)) ``` diff --git a/README.md b/README.md index 06be984..1079d29 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,6 @@ [![R-CMD-check](https://github.com/maurolepore/pkgdoc/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/maurolepore/pkgdoc/actions/workflows/R-CMD-check.yaml) -[![Codecov test -coverage](https://codecov.io/gh/maurolepore/pkgdoc/graph/badge.svg)](https://app.codecov.io/gh/maurolepore/pkgdoc) The goal of pkgdoc is to make it easy to create a data frame of the @@ -100,16 +98,32 @@ utilities You may link to documentation online. ``` r -template <- "?" -linked <- transform(datasets, topic = sprintf(template, package, topic)) +url_template <- "https://www.rdocumentation.org/packages/{package}/versions/3.6.2/topics/{topic}.html" +linked <- reference_package("datasets", url_template = url_template) +linked["topic"] +#> # A tibble: 87 × 1 +#> topic +#> +#> 1 2 3 4 5 6 7 8 9 10 # ℹ 77 more rows + knitr::kable(head(linked)) ``` | topic | alias | title | concept | package | |:---|:---|:---|:---|:---| -| ? | Adobe_glyphs, charset_to_Unicode | Conversion Tables between Character Sets | Datasets available by data() | tools | -| ? | AirPassengers | Monthly Airline Passenger Numbers 1949-1960 | Datasets available by data() | datasets | -| ? | BJsales, BJsales.lead | Sales Data with Leading Indicator | Datasets available by data() | datasets | -| ? | BOD | Biochemical Oxygen Demand | Datasets available by data() | datasets | -| ? | CO2 | Carbon Dioxide Uptake in Grass Plants | Datasets available by data() | datasets | -| ? | ChickWeight | Weight versus age of chicks on different diets | Datasets available by data() | datasets | +| AirPassengers | AirPassengers | Monthly Airline Passenger Numbers 1949-1960 | Datasets available by data() | datasets | +| BJsales | BJsales, BJsales.lead | Sales Data with Leading Indicator | Datasets available by data() | datasets | +| BOD | BOD | Biochemical Oxygen Demand | Datasets available by data() | datasets | +| CO2 | CO2 | Carbon Dioxide Uptake in Grass Plants | Datasets available by data() | datasets | +| ChickWeight | ChickWeight | Weight versus age of chicks on different diets | Datasets available by data() | datasets | +| DNase | DNase | Elisa assay of DNase | Datasets available by data() | datasets | diff --git a/inst/WORDLIST b/inst/WORDLIST index e85c41f..518bc3d 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,25 +1,14 @@ +AirPassengers +BJsales CMD -Codecov -ForestGEO -ViewFullTable -autoplot -autoplots -autoplotting -dbh -elev’ -fgeo +ChickWeight +DNase github https io isS maurolepore pkgdown -quadrat serach -sp -sp’ -subquadrat tidymodels tidyverse -vft -’ diff --git a/man/reference_concept.Rd b/man/reference_concept.Rd index fe4a996..e72a171 100644 --- a/man/reference_concept.Rd +++ b/man/reference_concept.Rd @@ -4,11 +4,22 @@ \alias{reference_concept} \title{Pick documentation matching some concepts.} \usage{ -reference_concept(x, url = NULL, packages = NULL, strip_s3class = TRUE) +reference_concept( + x, + url_template = NULL, + url = NULL, + packages = NULL, + strip_s3class = TRUE +) } \arguments{ \item{x}{A character vector giving concepts or package names to match.} +\item{url_template}{Character. A template to generate links to documentation +based on the column names of the output -- typically \code{package} and \code{topic}, +e.g. \code{"https://maurolepore.github.io/{package}/reference/{topic}.html"} +(\code{glue::glue()} syntax).} + \item{url}{Character vector of length-1 giving a base url, e.g. "https://maurolepore.github.io/".} diff --git a/man/reference_package.Rd b/man/reference_package.Rd index 0ac6290..dbcb72f 100644 --- a/man/reference_package.Rd +++ b/man/reference_package.Rd @@ -4,11 +4,22 @@ \alias{reference_package} \title{Create a data.frame with documentation metadata of one or more packages} \usage{ -reference_package(x, url = NULL, packages = NULL, strip_s3class = TRUE) +reference_package( + x, + url_template = NULL, + url = NULL, + packages = NULL, + strip_s3class = TRUE +) } \arguments{ \item{x}{A character vector giving concepts or package names to match.} +\item{url_template}{Character. A template to generate links to documentation +based on the column names of the output -- typically \code{package} and \code{topic}, +e.g. \code{"https://maurolepore.github.io/{package}/reference/{topic}.html"} +(\code{glue::glue()} syntax).} + \item{url}{Character vector of length-1 giving a base url, e.g. "https://maurolepore.github.io/".} diff --git a/tests/testthat/test-reference_package.R b/tests/testthat/test-reference_package.R index 2a0eba7..93954d8 100644 --- a/tests/testthat/test-reference_package.R +++ b/tests/testthat/test-reference_package.R @@ -47,3 +47,14 @@ test_that("doesn't include the package-level documentation", { out <- reference_package("pkgdoc") expect_false(any(grepl("pkgdoc-package", unique(out$alias)))) }) + +test_that("takes a `url_template`", { + url_template <- "https://maurolepore.github.io/{package}/reference/{topic}.html" + out <- reference_package("pkgdoc", url_template = url_template) + expect_true(grepl("pkgdoc", out$topic[[1]])) +}) + +test_that("with bad `url_template` errors gracefully", { + bad <- "https://{bad}/{topic}.html" + expect_error(reference_package("pkgdoc", url_template = bad), "not found") +})