Skip to content

Commit 0510815

Browse files
authored
url_template can now take explicit vignettes (#39)
* url_template can now take explicit vignettes * Polish README * Spelling
1 parent 7d8b07f commit 0510815

7 files changed

+148
-31
lines changed

R/document_universe.R

+34-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@ document_universe_impl <- function(x, url_template = NULL) {
55
out <- tidy_reference(pick, strip_s3class = TRUE)
66

77
if (!is.null(url_template)) {
8+
manual <- url_template[[1]]
9+
10+
if (identical(length(url_template), 1L)) {
11+
vignettes <- url_template[[1]]
12+
}
13+
if (identical(length(url_template), 2L)) {
14+
vignettes <- url_template[[2]]
15+
}
16+
if (length(url_template) > 2L) {
17+
longer <- length(url_template)
18+
cli::cli_abort("`url_template` must be of length 1 or 2, not {longer}.")
19+
}
20+
21+
822
out <- mutate(
923
out,
1024
topic = dplyr::case_when(
1125
.data$type == "help" ~ to_href(
1226
.data$topic,
13-
template = glue::glue(url_template)
27+
template = glue::glue(manual)
1428
),
1529
.data$type == "vignette" ~ to_href(
1630
.data$topic,
17-
template = glue::glue(vignettes_template(url_template))
31+
template = glue::glue(vignettes_template(vignettes))
1832
),
1933
.default = .data$topic
2034
)
@@ -40,10 +54,18 @@ vignettes_template <- function(template) {
4054
#' meta-package.
4155
#'
4256
#' @param x A character vector giving concepts or package names to match.
43-
#' @param url_template Character. A template to generate links to documentation
44-
#' based on the column names of the output -- typically `package` and `topic`,
45-
#' e.g. `"https://maurolepore.github.io/{package}/reference/{topic}.html"`
46-
#' (`glue::glue()` syntax).
57+
#' @param url_template Character. A template to generate links to documentation,
58+
#' using the syntax of `glue::glue()` to indicate where to insert the values
59+
#' from the columns `package` and `topic`.
60+
#' * If the vector has length 1, we assume it's for the manual (i.e. what you
61+
#' can access with `?`), e.g.:
62+
#' `"https://maurolepore.github.io/{package}/reference/{topic}.html"`. The
63+
#' template for vignettes will be automatically constructed by replacing
64+
#' /reference/ with /articles/, e.g.
65+
#' `"https://maurolepore.github.io/{package}/articles/{topic}.html"`. If this
66+
#' is invalid, then you'll need to provide a vector of length 2.
67+
#' * If the vector has length 2, we assume the first element is for the
68+
#' manual and the second element is for vignettes.
4769
#'
4870
#' @return A data frame.
4971
#'
@@ -52,10 +74,13 @@ vignettes_template <- function(template) {
5274
#' library(glue)
5375
#' library(tibble)
5476
#'
55-
#' url_template <- "https://{package}.tidyverse.org/reference/{topic}.html"
56-
#' document_universe(c("glue", "tibble"), url_template)
77+
#' manual <- "https://{package}.tidyverse.org/reference/{topic}.html"
78+
#' vignettes <- "https://{package}.tidyverse.org/articles/{topic}.html"
79+
#' document_universe(c("glue", "tibble"), url_template = c(manual, vignettes))
80+
#'
81+
#' # Assuming vignettes can be found at */articles/* rather than */reference/*
82+
#' document_universe(c("glue", "tibble"), url_template = manual)
5783
document_universe <- function(x, url_template = NULL) {
58-
# TODO Refactor to simplify. Comes from the more complex maurolepore/pkgdoc
5984
out <- document_universe_impl(x = x, url_template = url_template)
6085
tibble::as_tibble(out)
6186
}

README.Rmd

+15-5
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,27 @@ library(glue)
7979
library(tibble)
8080
8181
demoverse <- c("glue", "tibble")
82-
# For example: "https://tibble.tidyverse.org/reference/enframe.html"
83-
template <- "https://{package}.tidyverse.org/reference/{topic}.html"
84-
docs <- dverse::document_universe(demoverse, template)
85-
82+
# Example: "https://tibble.tidyverse.org/reference/as_tibble.html"
83+
manual <- "https://{package}.tidyverse.org/reference/{topic}.html"
84+
docs <- document_universe(demoverse, url_template = manual)
8685
docs
8786
```
8887

8988
`knitr::kable()` turns the URLs into clickable links.
9089

9190
```{r}
92-
knitr::kable(head(docs, 3))
91+
knitr::kable(tail(docs))
92+
```
93+
94+
`url_template` can take different templates for the manual and vignettes.
95+
96+
```{r}
97+
manual <- "https://tidyverse.r-universe.dev/{package}/doc/manual.html#{topic}"
98+
vignettes <- "https://tidyverse.r-universe.dev/articles/{package}/{topic}.html"
99+
docs_runiverse <- document_universe(demoverse, url_template = c(manual, vignettes))
100+
docs_runiverse
101+
102+
knitr::kable(tail(docs_runiverse))
93103
```
94104

95105
`DT::datatabe()` also provides a search box. See [Get started](https://maurolepore.github.io/dverse/articles/dverse.html).

README.md

+47-10
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,9 @@ library(glue)
7575
library(tibble)
7676

7777
demoverse <- c("glue", "tibble")
78-
# For example: "https://tibble.tidyverse.org/reference/enframe.html"
79-
template <- "https://{package}.tidyverse.org/reference/{topic}.html"
80-
docs <- dverse::document_universe(demoverse, template)
81-
78+
# Example: "https://tibble.tidyverse.org/reference/as_tibble.html"
79+
manual <- "https://{package}.tidyverse.org/reference/{topic}.html"
80+
docs <- document_universe(demoverse, url_template = manual)
8281
docs
8382
#> # A tibble: 46 × 7
8483
#> topic alias title concept type keyword package
@@ -99,14 +98,52 @@ docs
9998
`knitr::kable()` turns the URLs into clickable links.
10099

101100
``` r
102-
knitr::kable(head(docs, 3))
101+
knitr::kable(tail(docs))
102+
```
103+
104+
| topic | alias | title | concept | type | keyword | package |
105+
|:---|:---|:---|:---|:---|:---|:---|
106+
| <a href=https://tibble.tidyverse.org/reference/tribble.html>tribble</a> | tribble | Row-wise tibble creation | NA | help | NA | tibble |
107+
| <a href=https://glue.tidyverse.org/reference/trim.html>trim</a> | trim | Trim a character vector | NA | help | NA | glue |
108+
| <a href=https://tibble.tidyverse.org/reference/trunc_mat.html>trunc_mat</a> | trunc_mat | Legacy printing | NA | help | internal | tibble |
109+
| <a href=https://tibble.tidyverse.org/articles/types.html>types</a> | types | Column types | NA | vignette | NA | tibble |
110+
| <a href=https://tibble.tidyverse.org/reference/view.html>view</a> | view | View an object | NA | help | NA | tibble |
111+
| <a href=https://glue.tidyverse.org/articles/wrappers.html>wrappers</a> | wrappers | How to write a function that wraps glue | NA | vignette | NA | glue |
112+
113+
`url_template` can take different templates for the manual and
114+
vignettes.
115+
116+
``` r
117+
manual <- "https://tidyverse.r-universe.dev/{package}/doc/manual.html#{topic}"
118+
vignettes <- "https://tidyverse.r-universe.dev/articles/{package}/{topic}.html"
119+
docs_runiverse <- document_universe(demoverse, url_template = c(manual, vignettes))
120+
docs_runiverse
121+
#> # A tibble: 46 × 7
122+
#> topic alias title concept type keyword package
123+
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
124+
#> 1 <a href=https://tidyverse.r-univer… add_… Add … additi… help <NA> tibble
125+
#> 2 <a href=https://tidyverse.r-univer… add_… Add … additi… help <NA> tibble
126+
#> 3 <a href=https://tidyverse.r-univer… as_g… Coer… <NA> help <NA> glue
127+
#> 4 <a href=https://tidyverse.r-univer… as_t… Coer… <NA> help <NA> tibble
128+
#> 5 <a href=https://tidyverse.r-univer… char… Form… vector… help <NA> tibble
129+
#> 6 <a href=https://tidyverse.r-univer… depr… Depr… <NA> help intern… tibble
130+
#> 7 <a href=https://tidyverse.r-univer… digi… Comp… <NA> vign… <NA> tibble
131+
#> 8 <a href=https://tidyverse.r-univer… enfr… Conv… <NA> help <NA> tibble
132+
#> 9 <a href=https://tidyverse.r-univer… engi… Cust… <NA> vign… <NA> glue
133+
#> 10 <a href=https://tidyverse.r-univer… exte… Exte… <NA> vign… <NA> tibble
134+
#> # ℹ 36 more rows
135+
136+
knitr::kable(tail(docs_runiverse))
103137
```
104138

105-
| topic | alias | title | concept | type | keyword | package |
106-
| :----------------------------------------------------------------------------- | :------------------ | :-------------------------- | :------- | :--- | :------ | :------ |
107-
| <a href=https://tibble.tidyverse.org/reference/add_column.html>add\_column</a> | add\_column | Add columns to a data frame | addition | help | NA | tibble |
108-
| <a href=https://tibble.tidyverse.org/reference/add_row.html>add\_row</a> | add\_row, add\_case | Add rows to a data frame | addition | help | NA | tibble |
109-
| <a href=https://glue.tidyverse.org/reference/as_glue.html>as\_glue</a> | as\_glue | Coerce object to glue | NA | help | NA | glue |
139+
| topic | alias | title | concept | type | keyword | package |
140+
|:---|:---|:---|:---|:---|:---|:---|
141+
| <a href=https://tidyverse.r-universe.dev/tibble/doc/manual.html#tribble>tribble</a> | tribble | Row-wise tibble creation | NA | help | NA | tibble |
142+
| <a href=https://tidyverse.r-universe.dev/glue/doc/manual.html#trim>trim</a> | trim | Trim a character vector | NA | help | NA | glue |
143+
| <a href=https://tidyverse.r-universe.dev/tibble/doc/manual.html#trunc_mat>trunc_mat</a> | trunc_mat | Legacy printing | NA | help | internal | tibble |
144+
| <a href=https://tidyverse.r-universe.dev/articles/tibble/types.html>types</a> | types | Column types | NA | vignette | NA | tibble |
145+
| <a href=https://tidyverse.r-universe.dev/tibble/doc/manual.html#view>view</a> | view | View an object | NA | help | NA | tibble |
146+
| <a href=https://tidyverse.r-universe.dev/articles/glue/wrappers.html>wrappers</a> | wrappers | How to write a function that wraps glue | NA | vignette | NA | glue |
110147

111148
`DT::datatabe()` also provides a search box. See [Get
112149
started](https://maurolepore.github.io/dverse/articles/dverse.html).

inst/WORDLIST

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ tibble
1616
tidymodels
1717
tidyverse
1818
toyverse
19+
tribble
20+
trunc

man/document_universe.Rd

+20-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# with url_template longer than 2 yields an error
2+
3+
Code
4+
document_universe("base", too_long)
5+
Condition
6+
Error in `document_universe_impl()`:
7+
! `url_template` must be of length 1 or 2, not 3.
8+

tests/testthat/test-document_universe.R

+22-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test_that("with bad `url_template` errors gracefully", {
7070
})
7171

7272
test_that("vignettes have a reachable link", {
73-
# Not using dverse because on developer mode there are no vignettes
73+
skip_if_offline()
7474
withr::local_package("tibble")
7575

7676
template <- "https://{package}.tidyverse.org/reference/{topic}.html"
@@ -83,3 +83,24 @@ test_that("vignettes have a reachable link", {
8383

8484
expect_true(all(is_online(topic)))
8585
})
86+
87+
test_that("a r-universe template yields the expected vignette topic", {
88+
skip_if_offline()
89+
withr::local_package("tibble")
90+
91+
template <- c(
92+
manual = "https://tidyverse.r-universe.dev/{package}/doc/manual.html#{topic}",
93+
articles = "https://tidyverse.r-universe.dev/articles/{package}/{topic}.html"
94+
)
95+
out <- document_universe("tibble", url_template = template)
96+
url <- extract_url(out[grep("extending", out$topic), ]$topic)
97+
98+
expected <- "https://tidyverse.r-universe.dev/articles/tibble/extending.html"
99+
expect_equal(url, expected)
100+
expect_true(is_online(url))
101+
})
102+
103+
test_that("with url_template longer than 2 yields an error", {
104+
too_long <- c("a", "b", "c")
105+
expect_snapshot(error = TRUE, document_universe("base", too_long))
106+
})

0 commit comments

Comments
 (0)