Skip to content

Commit 8f0678e

Browse files
topepojuliasilge
andauthored
changes for #647 (#666)
* changes for #647 * Clarify docs in engine-docs * Fix quotes * Clarify README-DOCS Co-authored-by: Julia Silge <julia.silge@gmail.com>
1 parent f8310e5 commit 8f0678e

File tree

5 files changed

+117
-83
lines changed

5 files changed

+117
-83
lines changed

R/engine_docs.R

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,24 @@ update_model_info_file <- function(path = "inst/models.tsv") {
9797
# ------------------------------------------------------------------------------
9898

9999

100-
#' Tools for documenting packages
100+
#' Tools for documenting engines
101101
#'
102102
#' @description
103-
#' These are functions used to create dynamic documentation in Rd files
104-
#' based on which parsnip-related packages are loaded by the user.
103+
#' parsnip has a fairly complex documentation system where the engines for
104+
#' each model have detailed documentation about the syntax, tuning parameters,
105+
#' preprocessing needs, and so on.
105106
#'
106-
#' These functions can be used to make dynamic lists of documentation help
107-
#' files. \pkg{parsnip} uses these along with files in `man/rmd` which
108-
#' contain expanded documentation for specific model/engine combinations.
109-
#' [find_engine_files()] looks for files that have the pattern
110-
#' `details_{model}_{engine}.Rd` to link to. These files are generated by files
111-
#' named `man/rmd/{model}_{engine}.Rmd`. `make_engine_list()` creates a
112-
#' list seen at the top of the model Rd files while `make_seealso_list()`
113-
#' populates the list seen in "See Also" below. See the details section.
107+
#' The functions below are called from `.R` files to programmatically
108+
#' generate content in the help files for a model.
109+
#'
110+
#' * [find_engine_files()] identifies engines for a model and creates a
111+
#' bulleted list of links to those specific help files.
112+
#'
113+
#' * [make_seealso_list()] creates a set of links for the "See Also" list at
114+
#' the bottom of the help pages.
115+
#'
116+
#' * [find_engine_files()] is a function, used by the above, to find the
117+
#' engines for each model function.
114118
#'
115119
#' @param mod A character string for the model file (e.g. "linear_reg")
116120
#' @param pkg A character string for the package where the function is invoked.
@@ -122,38 +126,23 @@ update_model_info_file <- function(path = "inst/models.tsv") {
122126
#'
123127
#' `find_engine_files()` returns a tibble.
124128
#' @details
125-
#' The \pkg{parsnip} documentation is generated _dynamically_. Part of the Rd
126-
#' file populates a list of engines that depends on what packages are loaded
127-
#' *at the time that the man file is loaded*. For example, if
128-
#' another package has a new engine for `linear_reg()`, the
129-
#' `parsnip::linear_reg()` help can show a link to a detailed help page in the
130-
#' other package.
131-
#'
132-
#' The process for a package developer to create \pkg{parsnip} documentation is:
133-
#'
134-
#' 1. Create an engine-specific R file in the `R` directory with the name
135-
#' `{model}_{engine}.R` (e.g. `boost_tree_C5.0.R`). This has a small amount of
136-
#' documentation, as well as the directives "`@name details_{model}_{engine}`"
137-
#' and "`@includeRmd man/rmd/{model}_{engine}.md details`".
129+
#' parsnip includes a document (`README-DOCS.md`) with step-by-step instructions
130+
#' and details. See the code below to determine where it is installed (or see
131+
#' the References section).
138132
#'
139-
#' 2. Copy the file in \pkg{parsnip} that is in `man/rmd/aaa.Rmd` and put
140-
#' it in the same place in your package.
141-
#'
142-
#' 3. Write your own `man/rmd/{model}_{engine}.Rmd` file. This can include
143-
#' packages that are not listed in the DESCRIPTION file. Those are only
144-
#' required when the documentation file is created locally (probably using
145-
#' [devtools::document()]).
146-
#'
147-
#' 4. Run [devtools::document()] so that the `.md` content is included in the
148-
#' Rd file.
149-
#'
150-
#' The examples in \pkg{parsnip} can provide guidance for how to organize
151-
#' technical information about the models.
133+
#' Most parsnip users will not need to use these functions or documentation.
134+
#' @references
135+
#' \url{https://github.com/tidymodels/parsnip/blob/main/inst/README-DOCS.md}
152136
#' @name doc-tools
153137
#' @keywords internal
154138
#' @export
155139
#' @examples
156-
#' find_engine_files("linear_reg")
140+
#' # See this file for step-by-step instructions.
141+
#' system.file("README-DOCS.md", package = "parsnip")
142+
#'
143+
#' # Code examples:
144+
#' make_engine_list("linear_reg")
145+
#'
157146
#' cat(make_engine_list("linear_reg"))
158147
find_engine_files <- function(mod) {
159148
# Get available topics

inst/README-DOCS.md

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# About the parsnip documentation system
22

3+
## Background
4+
35
parsnip uses three concepts to describe models:
46

57
- The _model type_ specifies its mathematical structure.
@@ -21,9 +23,23 @@ Each modeling function defined in parsnip has a documentation file (with extensi
2123

2224
Also, each combination of engine and model type has a corresponding `.Rd` file (a.k.a the "engine-specific" documentation files). The list of known engines is also shown in the `.Rd` file for the main function.
2325

26+
If you are creating a new engine documentation file, the steps are:
27+
28+
1. Use roxygen comments to generate the main `.Rd` file that R's help system uses.
29+
2. Create an `.Rmd` file that has the details of the engine.
30+
3. Knit the `.Rmd` file to `.md` so the `.Rd` file from step 1 can link it.
31+
4. Once you are done, run `devtools::document()` (or use the RStudio IDE) to build the help files.
32+
33+
**Before you read the rest**, you probably don't have to generate the help files in this way yourself:
34+
35+
* This process only applies for engines whose model function is in parsnip. If your R package defines a new model, then you don't have to do all of this (unless you want to).
36+
37+
* If you make a PR with a new engine, you can work on the `.Rmd` file and the tidymodels folks can go through the process of merging it in (but let us know if you want us to do that in the PR).
2438

2539
## Creating the engine-specific `.Rd` files
2640

41+
Once create by `document()`, the `.Rd` files live in the `man` directory. They are not handwritten but are automatically generated from `.R` files in the `R` directory.
42+
2743
How do we generate these `.Rd` files? We'll use an example with `poisson_reg()` and the `"zeroinfl"` engine.
2844

2945
Each model/engine combination has its own `.R` file with a naming convention reflecting the contents (`poisson_reg_zeroinfl.R`). This file has a description of the type of model and the underlying function that is used for that engine:
@@ -40,15 +56,46 @@ as well as a directive for the `.Rd` file name to be created:
4056
4157
The engine markdown file (`poisson_reg_zeroinfl.md`) is made by the developer offline.
4258

43-
## Creating the engine-specific `.md` files
59+
Take a look at the [actual file](https://github.com/tidymodels/parsnip/blob/main/R/poisson_reg_zeroinfl.R) if you want to see it all.
60+
61+
To summarize: the relationship between these `.R` files and the `.md` files that they include is:
62+
63+
```
64+
:
65+
├── R
66+
│   ├── :
67+
│   ├── :
68+
│   ├── bag_tree.R. <- model definition code that has the Sexprs
69+
│   ├── bag_tree_C5.0.R <- wrapper around bag_tree_C5.0.md (below)
70+
│   ├── bag_tree_rpart.R <- wrapper around bag_tree_rpart.md (below)
71+
: :
72+
: :
73+
├── man
74+
│   ├── rmd
75+
│   │   ├──:
76+
│   │   ├── :
77+
│   │   ├── aaa.Rmd <- sourced by all other Rmd files
78+
│   │   ├── :
79+
│   │   ├── :
80+
│   │   ├── bag_tree_C5.0.Rmd <- engine details for the C5.0 engine only
81+
│   │   ├── bag_tree_C5.0.md
82+
│   │   ├── bag_tree_rpart.Rmd
83+
│   │   ├── bag_tree_rpart.md
84+
: : :
85+
```
86+
87+
88+
## Creating the engine-specific `.md` files for `.Rmd` files
4489

4590
How do we make these markdown files? These are created by corresponding `.Rmd` files contained in `parsnip/man/rmd/`. There are `.Rmd` files for the engines defined in parsnip as well as the extension packages listed by `parsnip:::extensions()`.
4691

4792
Each `.Rmd` file uses `parsnip/man/rmd/aaa.Rmd` as a child document. This file defines helper functions for the engine-specific documentation and loads some specific packages.
4893

4994
The `.Rmd` files use packages that are not formally parsnip dependencies (these are listed in `aaa.Rmd`). It also requires the parsnip extension packages defined in `parsnip:::extensions()`.
5095

51-
The `.Rmd` files have a consistent structure and there are numerous examples of these files in the package. The main sections are:
96+
There is an internal helper function (`parsnip:::install_engine_packages()`) that will install the extension packages as well as a few others that are required to build all of the documentation files.
97+
98+
The `.Rmd` files have a consistent structure and there are [numerous examples](https://github.com/tidymodels/parsnip/tree/main/man/rmd) of these files in the package. The main sections are:
5299

53100
- The list of possible engines.
54101
- The list of tuning parameters, if any, and other arguments of interest.
@@ -57,11 +104,15 @@ The `.Rmd` files have a consistent structure and there are numerous examples of
57104

58105
To convert the `.Rmd` files to `.md`, use the function `knit_engine_docs()`. After this, use `devtools::document()` to create the engine specific `.Rd` files.
59106

107+
**Please** look at the output of the knitting; if there are errors, they will show up there.
108+
60109
To test the results, do a hard restart of the R session (i.e., do not use `load_all()`).
61110

62111
## The main function `.Rd` files
63112

64-
This type of file determines the engine-specific `.Rd` files for the model function and enumerates their values in a bulleted list. For example, `poisson_reg.R` has the line:
113+
_This section has information that is probably only needed by the core developers._
114+
115+
Recall that the `.Rd` files are created from the `.R` files. There are some fancy things that we do in the `.R` files to list the engines. For example, `poisson_reg.R` has the line:
65116

66117
```r
67118
#' \Sexpr[stage=render,results=rd]{parsnip:::make_engine_list("poisson_reg")}
@@ -70,9 +121,10 @@ This type of file determines the engine-specific `.Rd` files for the model funct
70121
This finds the relevant engine `.Rd` files and creates the corresponding `.Rd` markup:
71122

72123
```
73-
There are different ways to fit this model. The method of estimation is
74-
chosen by setting the model \emph{engine}. The engine-specific pages
75-
for this model are listed below.
124+
There are different ways to fit this model, and the method of estimation is
125+
chosen by setting the model \emph{engine}. The engine-specific pages for this
126+
model are listed below.
127+
76128
77129
\itemize{
78130
\item \code{\link[parsnip:details_poisson_reg_glm]{glm}¹²}
@@ -85,7 +137,7 @@ for this model are listed below.
85137
\item \code{\link[parsnip:details_poisson_reg_zeroinfl]{zeroinfl}²}
86138
}
87139
88-
¹ The default engine. ² May require a parsnip extension package.
140+
¹ The default engine. ² Requires a parsnip extension package.
89141
```
90142

91143
There is a similar line at the bottom of the files that creates the _See Also_ list:
@@ -96,6 +148,8 @@ There is a similar line at the bottom of the files that creates the _See Also_ l
96148

97149
## Generating the model flat file
98150

151+
_This is also for core developers_.
152+
99153
As previously mentioned, the parsnip package contains a file `models.tsv`. To create this file:
100154

101155
1. Load the packages listed in `parsnip:::extensions()`.

man/details_C5_rules_C5.0.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/details_cubist_rules_Cubist.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/doc-tools.Rd

Lines changed: 27 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)