Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* `@examplesIf` now warns when there is no example code after the condition (#1695).
* `tag_words_line()` is deprecated in favour of `tag_words()`, which now checks for single-line content by default. Use `tag_words(x, multiline = TRUE)` or `tag_value(x, multiline = TRUE)` if your tag legitimately spans multiple lines.
* R6 improvements:
* R6 method documentation now uses less boilerplate: the "Method" prefix is removed from subsection headings, and the "Usage" and "Arguments" sub-subsection wrappers are removed (#1153).
* R6 method headings now get a `$` prefix. This should prevent downlit from linking them inappropriately (#1258).
* `initialize()` method parameters now automatically inherit documentation from `@field` tags with the same name, reducing the need to duplicate descriptions. Explicit `@param` tags still take precedence (#1004).
* New `needs_roxygenize()` provides a lightweight check that man pages are up-to-date by comparing modification times of `.Rd` files with their source files (#1411).
* All generated links now use the same code path. This will lead to some minor differences when you re-document, but overall the links will now be more consistent (#1792).
Expand Down
8 changes: 3 additions & 5 deletions R/rd-r6-methods-self.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ format.rd_r6_method <- function(x, ...) {
push(rd_if_html("<hr>"))
push(rd_if_html('<a id="', id, '"></a>'))
push(rd_if_latex("\\hypertarget{", id, "}{}"))
push(paste0("\\subsection{Method \\code{", nm, "()}}{"))
push(paste0("\\subsection{\\code{$", nm, "()}}{"))

# Description
if (length(x$description) > 0) {
Expand All @@ -57,8 +57,7 @@ format.rd_r6_method <- function(x, ...) {
usage_name <- paste0(x$alias, "$", nm)
fake <- paste(rep("X", nchar(usage_name)), collapse = "")
usage <- format(function_usage(fake, x$formals))
push_subsection(
"Usage",
push(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these sections because R users are familiar with them already, and they introduce more structure. The same with the 'Method' prefix. I think it is ok to make the docs more verbose if it helps discovery and reduces ambiguity. I agree that the boilerplate can be very repetitive, but I think for a reference manual that is ok. (Not for a tutorial otoh.)

But this is somewhat subjective, and I don't have a very strong opinion about these changes, so feel free to go ahead with them.

rd_if_html('<div class="r">'),
paste0("\\preformatted{", sub(paste0("^", fake), usage_name, usage), "}"),
rd_if_html("</div>")
Expand All @@ -68,8 +67,7 @@ format.rd_r6_method <- function(x, ...) {
if (length(x$params) > 0) {
nms <- map_chr(x$params, \(p) p$name)
vals <- map_chr(x$params, \(p) p$description)
push_subsection(
"Arguments",
push(
rd_if_html('<div class="arguments">'),
"\\describe{",
paste0("\\item{\\code{", nms, "}}{", vals, "}"),
Expand Down
23 changes: 4 additions & 19 deletions tests/testthat/_snaps/rd-r6-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,11 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-C2-meth1"></a>}}
\if{latex}{\out{\hypertarget{method-C2-meth1}{}}}
\subsection{Method \code{meth1()}}{
\subsection{\code{$meth1()}}{
method1
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{C2$meth1()}
\if{html}{\out{</div>}}
}

}
}

Expand All @@ -84,34 +81,25 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-B-shared"></a>}}
\if{latex}{\out{\hypertarget{method-B-shared}{}}}
\subsection{Method \code{shared()}}{
\subsection{\code{$shared()}}{
Method from B.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{B$shared()}
\if{html}{\out{</div>}}
}

}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-B-clone"></a>}}
\if{latex}{\out{\hypertarget{method-B-clone}{}}}
\subsection{Method \code{clone()}}{
\subsection{\code{$clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{B$clone(deep = FALSE)}
\if{html}{\out{</div>}}
}

\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{</div>}}
}

}
}

Expand All @@ -129,18 +117,15 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-C-meth"></a>}}
\if{latex}{\out{\hypertarget{method-C-meth}{}}}
\subsection{Method \code{meth()}}{
\subsection{\code{$meth()}}{
Method description.

\subsection{Description section}{
Description section body.
}
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{C$meth()}
\if{html}{\out{</div>}}
}

\subsection{Details}{
\subsection{Details section}{
Details section body.
Expand Down
23 changes: 4 additions & 19 deletions tests/testthat/_snaps/rd-r6-methods-self.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-Person-greet"></a>}}
\if{latex}{\out{\hypertarget{method-Person-greet}{}}}
\subsection{Method \code{greet()}}{
\subsection{\code{$greet()}}{
Say hello.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{Person$greet(who, how = "nicely")}
\if{html}{\out{</div>}}
}

\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{who}}{Name to greet.}
\item{\code{how}}{Greeting style.}
}
\if{html}{\out{</div>}}
}

}

# format.rd_r6_method renames initialize to new

Expand All @@ -51,15 +45,12 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-Foo-new"></a>}}
\if{latex}{\out{\hypertarget{method-Foo-new}{}}}
\subsection{Method \code{new()}}{
\subsection{\code{$new()}}{
Create object.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{Foo$new()}
\if{html}{\out{</div>}}
}

}

# format.rd_r6_method includes optional sections

Expand All @@ -69,14 +60,11 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-Job-run"></a>}}
\if{latex}{\out{\hypertarget{method-Job-run}{}}}
\subsection{Method \code{run()}}{
\subsection{\code{$run()}}{
Run the job.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{Job$run()}
\if{html}{\out{</div>}}
}

\subsection{Details}{
Some details.
}
Expand All @@ -102,13 +90,10 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-Job-run"></a>}}
\if{latex}{\out{\hypertarget{method-Job-run}{}}}
\subsection{Method \code{run()}}{
\subsection{\code{$run()}}{
Run.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{Job$run()}
\if{html}{\out{</div>}}
}

}

5 changes: 1 addition & 4 deletions tests/testthat/_snaps/rd-r6-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-Foo-run"></a>}}
\if{latex}{\out{\hypertarget{method-Foo-run}{}}}
\subsection{Method \code{run()}}{
\subsection{\code{$run()}}{
Run it.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}
\preformatted{Foo$run()}
\if{html}{\out{</div>}}
}

}
}

Expand Down
Loading
Loading