diff --git a/NEWS.md b/NEWS.md
index 6909d6dd..56d03913 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -3,6 +3,7 @@
* `@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 usage now shows `ClassName$new(args)` for constructors and `obj$method(args)` for other methods, making it clearer how each method is actually called (#1026).
* `@returns` now works as a method-level tag in R6 classes, just like `@return` (#1148).
* The "Super classes" section now omits the `pkg::` prefix for parent classes from the same package, making the inheritance chain easier to read (#1567).
* R6 classes with only active bindings and `cloneable = FALSE` no longer error during documentation (#1610).
diff --git a/R/rd-r6-methods-self.R b/R/rd-r6-methods-self.R
index e654c02a..d3f29ffb 100644
--- a/R/rd-r6-methods-self.R
+++ b/R/rd-r6-methods-self.R
@@ -1,7 +1,6 @@
rd_r6_method <- function(
name,
class,
- alias,
formals,
description = character(),
details = character(),
@@ -13,7 +12,6 @@ rd_r6_method <- function(
list(
name = name,
class = class,
- alias = alias,
formals = formals,
description = description,
details = details,
@@ -27,7 +25,6 @@ rd_r6_method <- function(
#' @export
format.rd_r6_method <- function(x, ...) {
- nm <- r6_show_name(x$name)
lines <- character()
push <- function(...) lines <<- c(lines, ...)
push_subsection <- function(title, ...) {
@@ -39,11 +36,14 @@ format.rd_r6_method <- function(x, ...) {
}
# Anchor and heading
- id <- paste0("method-", x$class, "-", nm)
+ call <- r6_method_name(x$class, x$name)
+
+ id <- paste0("method-", x$class, "-", x$name)
push(rd_if_html("
"))
push(rd_if_html(''))
push(rd_if_latex("\\hypertarget{", id, "}{}"))
- push(paste0("\\subsection{Method \\code{", nm, "()}}{"))
+
+ push(paste0("\\subsection{\\code{", call, "()}}{"))
# Description
if (length(x$description) > 0) {
@@ -54,13 +54,12 @@ format.rd_r6_method <- function(x, ...) {
}
# Usage
- usage_name <- paste0(x$alias, "$", nm)
- fake <- paste(rep("X", nchar(usage_name)), collapse = "")
+ fake <- paste(rep("X", nchar(call)), collapse = "")
usage <- format(function_usage(fake, x$formals))
push_subsection(
"Usage",
rd_if_html(''),
- paste0("\\preformatted{", sub(paste0("^", fake), usage_name, usage), "}"),
+ paste0("\\preformatted{", sub(paste0("^", fake), call, usage), "}"),
rd_if_html("
")
)
@@ -108,7 +107,7 @@ format.rd_r6_method <- function(x, ...) {
lines
}
-r6_method_from_row <- function(method, alias, block) {
+r6_method_from_row <- function(method, block) {
tags <- method$tags[[1]]
desc_tags <- keep(tags, \(t) t$tag == "description")
@@ -131,7 +130,6 @@ r6_method_from_row <- function(method, alias, block) {
rd_r6_method(
name = method$name,
class = method$class,
- alias = alias,
formals = method$formals[[1]],
description = description,
details = details,
@@ -222,9 +220,8 @@ r6_resolve_params <- function(method, block) {
})
}
-# vectorized
-r6_show_name <- function(names) {
- ifelse(names == "initialize", "new", names)
+r6_method_name <- function(class, method) {
+ paste0(class, "$", ifelse(method == "initialize", "new", method))
}
rd_if_html <- function(...) {
diff --git a/R/rd-r6-methods.R b/R/rd-r6-methods.R
index 89e45e70..b5c65714 100644
--- a/R/rd-r6-methods.R
+++ b/R/rd-r6-methods.R
@@ -14,10 +14,10 @@ format.rd_r6_methods <- function(x, ...) {
lines <- character()
push <- function(...) lines <<- c(lines, ...)
- nms <- r6_show_name(map_chr(x$self, \(m) m$name))
+ nms <- map_chr(x$self, \(m) m$name)
classes <- map_chr(x$self, \(m) m$class)
dest <- sprintf("method-%s-%s", classes, nms)
- code <- sprintf("\\code{%s$%s()}", x$alias, nms)
+ code <- sprintf("\\code{%s()}", r6_method_name(classes, nms))
push("\\section{Methods}{")
push(
@@ -74,7 +74,7 @@ r6_extract_methods <- function(r6data, alias, block) {
self_methods <- lapply(
seq_len(nrow(methods_df)),
- function(i) r6_method_from_row(methods_df[i, ], alias, block)
+ function(i) r6_method_from_row(methods_df[i, ], block)
)
inherited <- r6_extract_inherited_methods(r6data)
rd_r6_methods(alias, self = self_methods, inherited = inherited)
@@ -144,10 +144,9 @@ r6_all_examples <- function(methods) {
if (length(method$examples) == 0) {
return()
}
- name <- paste0(methods$alias, "$", r6_show_name(method$name))
c(
"\n## ------------------------------------------------",
- paste0("## Method `", name, "`"),
+ paste0("## Method `", r6_method_name(method$class, method$name), "()`"),
"## ------------------------------------------------\n",
paste(method$examples, collapse = "\n")
)
diff --git a/man/RoxyTopic.Rd b/man/RoxyTopic.Rd
index 8b86a84b..a4322ce8 100644
--- a/man/RoxyTopic.Rd
+++ b/man/RoxyTopic.Rd
@@ -37,7 +37,7 @@ A \code{RoxyTopic} object corresponds to a generated \code{.Rd} file.
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-format}{}}}
-\subsection{Method \code{format()}}{
+\subsection{\code{RoxyTopic$format()}}{
Format the \code{.Rd} file. It considers the sections in
particular order, even though Rd tools will reorder them again.
\subsection{Usage}{
@@ -63,7 +63,7 @@ Character string.
\if{html}{\out{
}}
\if{html}{\out{}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-is_valid}{}}}
-\subsection{Method \code{is_valid()}}{
+\subsection{\code{RoxyTopic$is_valid()}}{
Check if an \code{.Rd} file is valid
\subsection{Usage}{
\if{html}{\out{}}
@@ -79,7 +79,7 @@ Logical flag, \code{TRUE} for valid \code{.Rd} files
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-has_section}{}}}
-\subsection{Method \code{has_section()}}{
+\subsection{\code{RoxyTopic$has_section()}}{
Check if an \code{.Rd} file has a certain section.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -103,7 +103,7 @@ Logical flag.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-get_section}{}}}
-\subsection{Method \code{get_section()}}{
+\subsection{\code{RoxyTopic$get_section()}}{
Query a section.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -128,7 +128,7 @@ if the topic has no such section.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-get_value}{}}}
-\subsection{Method \code{get_value()}}{
+\subsection{\code{RoxyTopic$get_value()}}{
Query the value of a section. This is the value of
the \link{rd_section} object.
\subsection{Usage}{
@@ -153,7 +153,7 @@ Value.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-get_rd}{}}}
-\subsection{Method \code{get_rd()}}{
+\subsection{\code{RoxyTopic$get_rd()}}{
Get the Rd code of a section.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -177,7 +177,7 @@ Character vector, one element per line.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-get_name}{}}}
-\subsection{Method \code{get_name()}}{
+\subsection{\code{RoxyTopic$get_name()}}{
Get the value of the \code{name} section. This is the name
of the Rd topic.
\subsection{Usage}{
@@ -194,7 +194,7 @@ Character scalar.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-inherits_from}{}}}
-\subsection{Method \code{inherits_from()}}{
+\subsection{\code{RoxyTopic$inherits_from()}}{
Query the topics this topic inherits \code{type} from.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -218,7 +218,7 @@ A character vector of topic names.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-inherits_section_from}{}}}
-\subsection{Method \code{inherits_section_from()}}{
+\subsection{\code{RoxyTopic$inherits_section_from()}}{
Query the topics this topic inherits sections from.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -234,7 +234,7 @@ A character vector of topic names.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-add}{}}}
-\subsection{Method \code{add()}}{
+\subsection{\code{RoxyTopic$add()}}{
Add one or more sections to the topic.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -260,7 +260,7 @@ then the two sections will be merged.}
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-add_section}{}}}
-\subsection{Method \code{add_section()}}{
+\subsection{\code{RoxyTopic$add_section()}}{
Add a section.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -288,7 +288,7 @@ once in \code{self$sections}. This method if for internal use only.
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-RoxyTopic-clone}{}}}
-\subsection{Method \code{clone()}}{
+\subsection{\code{RoxyTopic$clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{
}}
diff --git a/tests/testthat/_snaps/rd-r6-class.md b/tests/testthat/_snaps/rd-r6-class.md
index 0472d9a7..4f571632 100644
--- a/tests/testthat/_snaps/rd-r6-class.md
+++ b/tests/testthat/_snaps/rd-r6-class.md
@@ -50,7 +50,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-C2-meth1}{}}}
- \subsection{Method \code{meth1()}}{
+ \subsection{\code{C2$meth1()}}{
method1
\subsection{Usage}{
\if{html}{\out{
}}
@@ -84,7 +84,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-B-shared}{}}}
- \subsection{Method \code{shared()}}{
+ \subsection{\code{B$shared()}}{
Method from B.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -96,7 +96,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-B-clone}{}}}
- \subsection{Method \code{clone()}}{
+ \subsection{\code{B$clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -129,7 +129,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-C-meth}{}}}
- \subsection{Method \code{meth()}}{
+ \subsection{\code{C$meth()}}{
Method description.
\subsection{Description section}{
diff --git a/tests/testthat/_snaps/rd-r6-methods-self.md b/tests/testthat/_snaps/rd-r6-methods-self.md
index 9da54ab4..0db38480 100644
--- a/tests/testthat/_snaps/rd-r6-methods-self.md
+++ b/tests/testthat/_snaps/rd-r6-methods-self.md
@@ -31,7 +31,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-Person-greet}{}}}
- \subsection{Method \code{greet()}}{
+ \subsection{\code{Person$greet()}}{
Say hello.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -56,9 +56,9 @@
cat(format(method), sep = "\n")
Output
\if{html}{\out{
}}
- \if{html}{\out{
}}
- \if{latex}{\out{\hypertarget{method-Foo-new}{}}}
- \subsection{Method \code{new()}}{
+ \if{html}{\out{
}}
+ \if{latex}{\out{\hypertarget{method-Foo-initialize}{}}}
+ \subsection{\code{Foo$new()}}{
Create object.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -76,7 +76,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-Job-run}{}}}
- \subsection{Method \code{run()}}{
+ \subsection{\code{Job$run()}}{
Run the job.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -109,7 +109,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-Job-run}{}}}
- \subsection{Method \code{run()}}{
+ \subsection{\code{Job$run()}}{
Run.
\subsection{Usage}{
\if{html}{\out{
}}
diff --git a/tests/testthat/_snaps/rd-r6-methods.md b/tests/testthat/_snaps/rd-r6-methods.md
index 6865e819..a8b654db 100644
--- a/tests/testthat/_snaps/rd-r6-methods.md
+++ b/tests/testthat/_snaps/rd-r6-methods.md
@@ -12,7 +12,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-Foo-run}{}}}
- \subsection{Method \code{run()}}{
+ \subsection{\code{Foo$run()}}{
Run it.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -30,13 +30,13 @@
Output
## ------------------------------------------------
- ## Method `C$greet`
+ ## Method `C$greet()`
## ------------------------------------------------
c$greet()
## ------------------------------------------------
- ## Method `C$stop`
+ ## Method `C$stop()`
## ------------------------------------------------
c$stop()
diff --git a/tests/testthat/_snaps/rd-r6.md b/tests/testthat/_snaps/rd-r6.md
index 64586579..d17950e0 100644
--- a/tests/testthat/_snaps/rd-r6.md
+++ b/tests/testthat/_snaps/rd-r6.md
@@ -18,13 +18,13 @@
a <- A$new()
## ------------------------------------------------
- ## Method `A$meth1`
+ ## Method `A$meth1()`
## ------------------------------------------------
## Example for meth1
## ------------------------------------------------
- ## Method `A$meth2`
+ ## Method `A$meth2()`
## ------------------------------------------------
## Example for meth2
@@ -63,7 +63,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-A-meth1}{}}}
- \subsection{Method \code{meth1()}}{
+ \subsection{\code{A$meth1()}}{
A method 1.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -90,7 +90,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-A-meth2}{}}}
- \subsection{Method \code{meth2()}}{
+ \subsection{\code{A$meth2()}}{
Method 2 description.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -126,7 +126,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-A-meth3}{}}}
- \subsection{Method \code{meth3()}}{
+ \subsection{\code{A$meth3()}}{
Method 3.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -146,7 +146,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-A-clone}{}}}
- \subsection{Method \code{clone()}}{
+ \subsection{\code{A$clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -221,7 +221,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-B-meth1}{}}}
- \subsection{Method \code{meth1()}}{
+ \subsection{\code{B$meth1()}}{
B method 1.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -241,7 +241,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-B-meth4}{}}}
- \subsection{Method \code{meth4()}}{
+ \subsection{\code{B$meth4()}}{
B method 4.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -253,7 +253,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-B-clone}{}}}
- \subsection{Method \code{clone()}}{
+ \subsection{\code{B$clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -328,7 +328,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-C-meth2}{}}}
- \subsection{Method \code{meth2()}}{
+ \subsection{\code{C$meth2()}}{
C method 2.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -349,7 +349,7 @@
\if{html}{\out{
}}
\if{html}{\out{
}}
\if{latex}{\out{\hypertarget{method-C-meth5}{}}}
- \subsection{Method \code{meth5()}}{
+ \subsection{\code{C$meth5()}}{
C method 5.
\subsection{Usage}{
\if{html}{\out{
}}
@@ -359,3 +359,4 @@
}
}
+
diff --git a/tests/testthat/test-rd-r6-methods-self.R b/tests/testthat/test-rd-r6-methods-self.R
index f58df339..82822049 100644
--- a/tests/testthat/test-rd-r6-methods-self.R
+++ b/tests/testthat/test-rd-r6-methods-self.R
@@ -16,7 +16,6 @@ test_that("r6_method_from_row extracts all components", {
expect_equal(method$name, "greet")
expect_equal(method$class, "C")
- expect_equal(method$alias, "C")
expect_equal(method$description, "Say hello.")
expect_equal(method$details, "Be polite.")
expect_equal(method$params, list(list(name = "who", description = "Name.")))
@@ -132,7 +131,6 @@ test_that("format.rd_r6_method produces method subsection", {
method <- rd_r6_method(
name = "greet",
class = "Person",
- alias = "Person",
formals = as.pairlist(alist(who = , how = "nicely")),
description = "Say hello.",
params = list(
@@ -147,7 +145,6 @@ test_that("format.rd_r6_method renames initialize to new", {
method <- rd_r6_method(
name = "initialize",
class = "Foo",
- alias = "Foo",
formals = NULL,
description = "Create object."
)
@@ -158,7 +155,6 @@ test_that("format.rd_r6_method includes optional sections", {
method <- rd_r6_method(
name = "run",
class = "Job",
- alias = "Job",
formals = NULL,
description = "Run the job.",
details = "Some details.",
@@ -172,7 +168,6 @@ test_that("format.rd_r6_method omits empty optional sections", {
method <- rd_r6_method(
name = "run",
class = "Job",
- alias = "Job",
formals = NULL,
description = "Run."
)
diff --git a/tests/testthat/test-rd-r6-methods.R b/tests/testthat/test-rd-r6-methods.R
index a79afa01..542db5b5 100644
--- a/tests/testthat/test-rd-r6-methods.R
+++ b/tests/testthat/test-rd-r6-methods.R
@@ -5,7 +5,6 @@ test_that("format.rd_r6_methods with one method", {
rd_r6_method(
name = "run",
class = "Foo",
- alias = "Foo",
formals = NULL,
description = "Run it."
)