From b7c4243a00ac1cd38e3516dd7f4eb1de3014fee5 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 17 Mar 2026 09:40:58 -0500 Subject: [PATCH 1/5] Tweak R6 usage * Use `obj$method()` not `{classname}$method()` * Use `{classname}()` not `{classname}$new()` * `rd_r6_method()` no longer needs alias Fixes #1026 --- NEWS.md | 1 + R/rd-r6-methods-self.R | 25 ++++++++++++--------- R/rd-r6-methods.R | 6 ++--- tests/testthat/_snaps/rd-r6-class.md | 9 ++++---- tests/testthat/_snaps/rd-r6-methods-self.md | 15 ++++++------- tests/testthat/_snaps/rd-r6-methods.md | 7 +++--- tests/testthat/_snaps/rd-r6.md | 25 ++++++++++----------- tests/testthat/test-rd-r6-methods-self.R | 5 ----- tests/testthat/test-rd-r6-methods.R | 1 - 9 files changed, 44 insertions(+), 50 deletions(-) diff --git a/NEWS.md b/NEWS.md index dda68fbb..b58c1dcb 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(args)` for constructors and `obj$method(args)` for other methods, making it clearer how each method is actually called (#1026). * `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). diff --git a/R/rd-r6-methods-self.R b/R/rd-r6-methods-self.R index fde2f1ab..04b7cab0 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,10 +36,13 @@ format.rd_r6_method <- function(x, ...) { } # Anchor and heading - id <- paste0("method-", x$class, "-", nm) + + id <- paste0("method-", x$class, "-", x$name) push(rd_if_html("
")) push(rd_if_html('')) push(rd_if_latex("\\hypertarget{", id, "}{}")) + + nm <- if (x$name == "initialize") x$class else x$name push(paste0("\\subsection{Method \\code{", nm, "()}}{")) # Description @@ -54,13 +54,13 @@ format.rd_r6_method <- function(x, ...) { } # Usage - usage_name <- paste0(x$alias, "$", nm) - fake <- paste(rep("X", nchar(usage_name)), collapse = "") + usage_nm <- r6_method_name(x$class, x$name) + fake <- paste(rep("X", nchar(usage_nm)), 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), usage_nm, usage), "}"), rd_if_html("
") ) @@ -108,7 +108,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 +131,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, @@ -223,8 +222,12 @@ r6_resolve_params <- function(method, block) { } # vectorized -r6_show_name <- function(names) { - ifelse(names == "initialize", "new", names) +r6_method_name <- function(class, method) { + if (method == "initialize") { + class + } else { + paste0("obj$", method) + } } rd_if_html <- function(...) { diff --git a/R/rd-r6-methods.R b/R/rd-r6-methods.R index a8622650..8df458fc 100644 --- a/R/rd-r6-methods.R +++ b/R/rd-r6-methods.R @@ -14,7 +14,7 @@ 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) @@ -78,7 +78,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) @@ -145,7 +145,7 @@ r6_all_examples <- function(methods) { if (length(method$examples) == 0) { return() } - name <- paste0(methods$alias, "$", r6_show_name(method$name)) + name <- paste0(r6_method_name(methods$class, method$name), "()") c( "\n## ------------------------------------------------", paste0("## Method `", name, "`"), diff --git a/tests/testthat/_snaps/rd-r6-class.md b/tests/testthat/_snaps/rd-r6-class.md index a8c7bf37..7f9c310c 100644 --- a/tests/testthat/_snaps/rd-r6-class.md +++ b/tests/testthat/_snaps/rd-r6-class.md @@ -54,7 +54,7 @@ method1 \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{C2$meth1()} + \preformatted{obj$meth1()} \if{html}{\out{
}} } @@ -88,7 +88,7 @@ Method from B. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{B$shared()} + \preformatted{obj$shared()} \if{html}{\out{
}} } @@ -100,7 +100,7 @@ The objects of this class are cloneable with this method. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{B$clone(deep = FALSE)} + \preformatted{obj$clone(deep = FALSE)} \if{html}{\out{
}} } @@ -137,7 +137,7 @@ } \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{C$meth()} + \preformatted{obj$meth()} \if{html}{\out{
}} } @@ -149,4 +149,3 @@ } } - diff --git a/tests/testthat/_snaps/rd-r6-methods-self.md b/tests/testthat/_snaps/rd-r6-methods-self.md index 029aca46..cf742cce 100644 --- a/tests/testthat/_snaps/rd-r6-methods-self.md +++ b/tests/testthat/_snaps/rd-r6-methods-self.md @@ -28,7 +28,7 @@ Say hello. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Person$greet(who, how = "nicely")} + \preformatted{obj$greet(who, how = "nicely")} \if{html}{\out{
}} } @@ -49,13 +49,13 @@ 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{Method \code{Foo()}}{ Create object. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Foo$new()} + \preformatted{Foo()} \if{html}{\out{
}} } @@ -73,7 +73,7 @@ Run the job. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Job$run()} + \preformatted{obj$run()} \if{html}{\out{
}} } @@ -106,9 +106,8 @@ Run. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Job$run()} + \preformatted{obj$run()} \if{html}{\out{
}} } } - diff --git a/tests/testthat/_snaps/rd-r6-methods.md b/tests/testthat/_snaps/rd-r6-methods.md index 6865e819..1d933709 100644 --- a/tests/testthat/_snaps/rd-r6-methods.md +++ b/tests/testthat/_snaps/rd-r6-methods.md @@ -16,7 +16,7 @@ Run it. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Foo$run()} + \preformatted{obj$run()} \if{html}{\out{
}} } @@ -30,15 +30,14 @@ Output ## ------------------------------------------------ - ## Method `C$greet` + ## Method `obj$greet()` ## ------------------------------------------------ c$greet() ## ------------------------------------------------ - ## Method `C$stop` + ## Method `obj$stop()` ## ------------------------------------------------ c$stop() c$stop(force = TRUE) - diff --git a/tests/testthat/_snaps/rd-r6.md b/tests/testthat/_snaps/rd-r6.md index 1e217df8..c7ba8122 100644 --- a/tests/testthat/_snaps/rd-r6.md +++ b/tests/testthat/_snaps/rd-r6.md @@ -50,13 +50,13 @@ ## Example for meth2 ## ------------------------------------------------ - ## Method `A$meth1` + ## Method `obj$meth1()` ## ------------------------------------------------ ## Example for meth1 ## ------------------------------------------------ - ## Method `A$meth2` + ## Method `obj$meth2()` ## ------------------------------------------------ ## Example for meth2 @@ -99,7 +99,7 @@ A method 1. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{A$meth1(Z)} + \preformatted{obj$meth1(Z)} \if{html}{\out{
}} } @@ -126,7 +126,7 @@ Method 2 description. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{A$meth2(Z = 10, ...)} + \preformatted{obj$meth2(Z = 10, ...)} \if{html}{\out{
}} } @@ -157,7 +157,7 @@ \subsection{Method \code{meth3()}}{ \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{A$meth3(duplicate, missing)} + \preformatted{obj$meth3(duplicate, missing)} \if{html}{\out{
}} } @@ -186,7 +186,7 @@ The objects of this class are cloneable with this method. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{A$clone(deep = FALSE)} + \preformatted{obj$clone(deep = FALSE)} \if{html}{\out{
}} } @@ -260,7 +260,7 @@ B method 1. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{B$meth1(Z)} + \preformatted{obj$meth1(Z)} \if{html}{\out{
}} } @@ -280,7 +280,7 @@ A method 4. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{B$meth4()} + \preformatted{obj$meth4()} \if{html}{\out{
}} } @@ -292,7 +292,7 @@ The objects of this class are cloneable with this method. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{B$clone(deep = FALSE)} + \preformatted{obj$clone(deep = FALSE)} \if{html}{\out{
}} } @@ -377,7 +377,7 @@ C method 2. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{C$meth2(Z = 10, ...)} + \preformatted{obj$meth2(Z = 10, ...)} \if{html}{\out{
}} } @@ -398,7 +398,7 @@ C method 5. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{C$meth5()} + \preformatted{obj$meth5()} \if{html}{\out{
}} } @@ -409,10 +409,9 @@ \subsection{Method \code{undocumented_method()}}{ \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{C$undocumented_method()} + \preformatted{obj$undocumented_method()} \if{html}{\out{
}} } } } - diff --git a/tests/testthat/test-rd-r6-methods-self.R b/tests/testthat/test-rd-r6-methods-self.R index 359059aa..fcd6af6d 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."))) @@ -103,7 +102,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( @@ -118,7 +116,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." ) @@ -129,7 +126,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.", @@ -143,7 +139,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." ) From ea62a1a10691ef055feb5c72e7bf02dfeb41e382 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 17 Mar 2026 14:52:18 -0500 Subject: [PATCH 2/5] Fix constructor call --- R/rd-r6-methods-self.R | 2 +- tests/testthat/_snaps/rd-r6-class.md | 1 + tests/testthat/_snaps/rd-r6-methods-self.md | 3 ++- tests/testthat/_snaps/rd-r6-methods.md | 1 + tests/testthat/_snaps/rd-r6.md | 1 + 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/rd-r6-methods-self.R b/R/rd-r6-methods-self.R index 04b7cab0..a783fe14 100644 --- a/R/rd-r6-methods-self.R +++ b/R/rd-r6-methods-self.R @@ -224,7 +224,7 @@ r6_resolve_params <- function(method, block) { # vectorized r6_method_name <- function(class, method) { if (method == "initialize") { - class + paste0(class, "$new") } else { paste0("obj$", method) } diff --git a/tests/testthat/_snaps/rd-r6-class.md b/tests/testthat/_snaps/rd-r6-class.md index 7f9c310c..3d96b428 100644 --- a/tests/testthat/_snaps/rd-r6-class.md +++ b/tests/testthat/_snaps/rd-r6-class.md @@ -149,3 +149,4 @@ } } + diff --git a/tests/testthat/_snaps/rd-r6-methods-self.md b/tests/testthat/_snaps/rd-r6-methods-self.md index cf742cce..c5b80115 100644 --- a/tests/testthat/_snaps/rd-r6-methods-self.md +++ b/tests/testthat/_snaps/rd-r6-methods-self.md @@ -55,7 +55,7 @@ Create object. \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{Foo()} + \preformatted{Foo$new()} \if{html}{\out{
}} } @@ -111,3 +111,4 @@ } } + diff --git a/tests/testthat/_snaps/rd-r6-methods.md b/tests/testthat/_snaps/rd-r6-methods.md index 1d933709..7a05d5ae 100644 --- a/tests/testthat/_snaps/rd-r6-methods.md +++ b/tests/testthat/_snaps/rd-r6-methods.md @@ -41,3 +41,4 @@ c$stop() c$stop(force = TRUE) + diff --git a/tests/testthat/_snaps/rd-r6.md b/tests/testthat/_snaps/rd-r6.md index c7ba8122..4e9c4c3a 100644 --- a/tests/testthat/_snaps/rd-r6.md +++ b/tests/testthat/_snaps/rd-r6.md @@ -415,3 +415,4 @@ } } + From a24d6b8901d65e9843bfd3ec8fb26b772e7b1265 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 17 Mar 2026 14:53:05 -0500 Subject: [PATCH 3/5] Update news --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index b8c22ba7..71e69187 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +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(args)` for constructors and `obj$method(args)` for other methods, making it clearer how each method is actually called (#1026). + * 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). * 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). * `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). From 78be8a1d888ea9875d3397723483335b896e1756 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 18 Mar 2026 12:41:58 -0500 Subject: [PATCH 4/5] Re-document --- man/RoxyTopic.Rd | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/man/RoxyTopic.Rd b/man/RoxyTopic.Rd index 8b86a84b..0235445d 100644 --- a/man/RoxyTopic.Rd +++ b/man/RoxyTopic.Rd @@ -42,7 +42,7 @@ Format the \code{.Rd} file. It considers the sections in particular order, even though Rd tools will reorder them again. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$format(...)} +\preformatted{obj$format(...)} \if{html}{\out{
}} } @@ -67,7 +67,7 @@ Character string. Check if an \code{.Rd} file is valid \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$is_valid()} +\preformatted{obj$is_valid()} \if{html}{\out{
}} } @@ -83,7 +83,7 @@ Logical flag, \code{TRUE} for valid \code{.Rd} files Check if an \code{.Rd} file has a certain section. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$has_section(type)} +\preformatted{obj$has_section(type)} \if{html}{\out{
}} } @@ -107,7 +107,7 @@ Logical flag. Query a section. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$get_section(type)} +\preformatted{obj$get_section(type)} \if{html}{\out{
}} } @@ -133,7 +133,7 @@ Query the value of a section. This is the value of the \link{rd_section} object. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$get_value(type)} +\preformatted{obj$get_value(type)} \if{html}{\out{
}} } @@ -157,7 +157,7 @@ Value. Get the Rd code of a section. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$get_rd(type)} +\preformatted{obj$get_rd(type)} \if{html}{\out{
}} } @@ -182,7 +182,7 @@ Get the value of the \code{name} section. This is the name of the Rd topic. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$get_name()} +\preformatted{obj$get_name()} \if{html}{\out{
}} } @@ -198,7 +198,7 @@ Character scalar. Query the topics this topic inherits \code{type} from. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$inherits_from(type)} +\preformatted{obj$inherits_from(type)} \if{html}{\out{
}} } @@ -222,7 +222,7 @@ A character vector of topic names. Query the topics this topic inherits sections from. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$inherits_section_from()} +\preformatted{obj$inherits_section_from()} \if{html}{\out{
}} } @@ -238,7 +238,7 @@ A character vector of topic names. Add one or more sections to the topic. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$add(x, block = "???", overwrite = FALSE)} +\preformatted{obj$add(x, block = "???", overwrite = FALSE)} \if{html}{\out{
}} } @@ -264,7 +264,7 @@ then the two sections will be merged.} Add a section. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$add_section(section, block = "???", overwrite = FALSE)} +\preformatted{obj$add_section(section, block = "???", overwrite = FALSE)} \if{html}{\out{
}} } @@ -292,7 +292,7 @@ once in \code{self$sections}. This method if for internal use only. The objects of this class are cloneable with this method. \subsection{Usage}{ \if{html}{\out{
}} -\preformatted{RoxyTopic$clone(deep = FALSE)} +\preformatted{obj$clone(deep = FALSE)} \if{html}{\out{
}} } From acac31d1fc5e4b234ad872feab46065c9ba540f3 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 18 Mar 2026 12:50:07 -0500 Subject: [PATCH 5/5] Use the same method formatting everywhere --- R/rd-r6-methods-self.R | 16 +++---- R/rd-r6-methods.R | 5 +-- man/RoxyTopic.Rd | 48 ++++++++++----------- tests/testthat/_snaps/rd-r6-class.md | 16 +++---- tests/testthat/_snaps/rd-r6-methods-self.md | 14 +++--- tests/testthat/_snaps/rd-r6-methods.md | 8 ++-- tests/testthat/_snaps/rd-r6.md | 40 ++++++++--------- 7 files changed, 70 insertions(+), 77 deletions(-) diff --git a/R/rd-r6-methods-self.R b/R/rd-r6-methods-self.R index 862eb471..d3f29ffb 100644 --- a/R/rd-r6-methods-self.R +++ b/R/rd-r6-methods-self.R @@ -36,14 +36,14 @@ format.rd_r6_method <- function(x, ...) { } # Anchor and heading + 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, "}{}")) - nm <- if (x$name == "initialize") x$class else x$name - 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_nm <- r6_method_name(x$class, x$name) - fake <- paste(rep("X", nchar(usage_nm)), 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_nm, usage), "}"), + paste0("\\preformatted{", sub(paste0("^", fake), call, usage), "}"), rd_if_html("
") ) @@ -221,13 +220,8 @@ r6_resolve_params <- function(method, block) { }) } -# vectorized r6_method_name <- function(class, method) { - if (method == "initialize") { - paste0(class, "$new") - } else { - paste0("obj$", 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 eadc716b..b5c65714 100644 --- a/R/rd-r6-methods.R +++ b/R/rd-r6-methods.R @@ -17,7 +17,7 @@ format.rd_r6_methods <- function(x, ...) { 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( @@ -144,10 +144,9 @@ r6_all_examples <- function(methods) { if (length(method$examples) == 0) { return() } - name <- paste0(r6_method_name(methods$class, 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 0235445d..a4322ce8 100644 --- a/man/RoxyTopic.Rd +++ b/man/RoxyTopic.Rd @@ -37,12 +37,12 @@ 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}{ \if{html}{\out{
}} -\preformatted{obj$format(...)} +\preformatted{RoxyTopic$format(...)} \if{html}{\out{
}} } @@ -63,11 +63,11 @@ 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{
}} -\preformatted{obj$is_valid()} +\preformatted{RoxyTopic$is_valid()} \if{html}{\out{
}} } @@ -79,11 +79,11 @@ 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{
}} -\preformatted{obj$has_section(type)} +\preformatted{RoxyTopic$has_section(type)} \if{html}{\out{
}} } @@ -103,11 +103,11 @@ 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{
}} -\preformatted{obj$get_section(type)} +\preformatted{RoxyTopic$get_section(type)} \if{html}{\out{
}} } @@ -128,12 +128,12 @@ 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}{ \if{html}{\out{
}} -\preformatted{obj$get_value(type)} +\preformatted{RoxyTopic$get_value(type)} \if{html}{\out{
}} } @@ -153,11 +153,11 @@ 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{
}} -\preformatted{obj$get_rd(type)} +\preformatted{RoxyTopic$get_rd(type)} \if{html}{\out{
}} } @@ -177,12 +177,12 @@ 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}{ \if{html}{\out{
}} -\preformatted{obj$get_name()} +\preformatted{RoxyTopic$get_name()} \if{html}{\out{
}} } @@ -194,11 +194,11 @@ 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{
}} -\preformatted{obj$inherits_from(type)} +\preformatted{RoxyTopic$inherits_from(type)} \if{html}{\out{
}} } @@ -218,11 +218,11 @@ 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{
}} -\preformatted{obj$inherits_section_from()} +\preformatted{RoxyTopic$inherits_section_from()} \if{html}{\out{
}} } @@ -234,11 +234,11 @@ 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{
}} -\preformatted{obj$add(x, block = "???", overwrite = FALSE)} +\preformatted{RoxyTopic$add(x, block = "???", overwrite = FALSE)} \if{html}{\out{
}} } @@ -260,11 +260,11 @@ 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{
}} -\preformatted{obj$add_section(section, block = "???", overwrite = FALSE)} +\preformatted{RoxyTopic$add_section(section, block = "???", overwrite = FALSE)} \if{html}{\out{
}} } @@ -288,11 +288,11 @@ 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{
}} -\preformatted{obj$clone(deep = FALSE)} +\preformatted{RoxyTopic$clone(deep = FALSE)} \if{html}{\out{
}} } diff --git a/tests/testthat/_snaps/rd-r6-class.md b/tests/testthat/_snaps/rd-r6-class.md index 472db23b..4f571632 100644 --- a/tests/testthat/_snaps/rd-r6-class.md +++ b/tests/testthat/_snaps/rd-r6-class.md @@ -50,11 +50,11 @@ \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{
}} - \preformatted{obj$meth1()} + \preformatted{C2$meth1()} \if{html}{\out{
}} } @@ -84,11 +84,11 @@ \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{
}} - \preformatted{obj$shared()} + \preformatted{B$shared()} \if{html}{\out{
}} } @@ -96,11 +96,11 @@ \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{
}} - \preformatted{obj$clone(deep = FALSE)} + \preformatted{B$clone(deep = FALSE)} \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}{ @@ -137,7 +137,7 @@ } \subsection{Usage}{ \if{html}{\out{
}} - \preformatted{obj$meth()} + \preformatted{C$meth()} \if{html}{\out{
}} } diff --git a/tests/testthat/_snaps/rd-r6-methods-self.md b/tests/testthat/_snaps/rd-r6-methods-self.md index 8a3eab46..0db38480 100644 --- a/tests/testthat/_snaps/rd-r6-methods-self.md +++ b/tests/testthat/_snaps/rd-r6-methods-self.md @@ -31,11 +31,11 @@ \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{
}} - \preformatted{obj$greet(who, how = "nicely")} + \preformatted{Person$greet(who, how = "nicely")} \if{html}{\out{
}} } @@ -58,7 +58,7 @@ \if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-Foo-initialize}{}}} - \subsection{Method \code{Foo()}}{ + \subsection{\code{Foo$new()}}{ Create object. \subsection{Usage}{ \if{html}{\out{
}} @@ -76,11 +76,11 @@ \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{
}} - \preformatted{obj$run()} + \preformatted{Job$run()} \if{html}{\out{
}} } @@ -109,11 +109,11 @@ \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{
}} - \preformatted{obj$run()} + \preformatted{Job$run()} \if{html}{\out{
}} } diff --git a/tests/testthat/_snaps/rd-r6-methods.md b/tests/testthat/_snaps/rd-r6-methods.md index 7a05d5ae..a8b654db 100644 --- a/tests/testthat/_snaps/rd-r6-methods.md +++ b/tests/testthat/_snaps/rd-r6-methods.md @@ -12,11 +12,11 @@ \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{
}} - \preformatted{obj$run()} + \preformatted{Foo$run()} \if{html}{\out{
}} } @@ -30,13 +30,13 @@ Output ## ------------------------------------------------ - ## Method `obj$greet()` + ## Method `C$greet()` ## ------------------------------------------------ c$greet() ## ------------------------------------------------ - ## Method `obj$stop()` + ## Method `C$stop()` ## ------------------------------------------------ c$stop() diff --git a/tests/testthat/_snaps/rd-r6.md b/tests/testthat/_snaps/rd-r6.md index 10c00ae0..d17950e0 100644 --- a/tests/testthat/_snaps/rd-r6.md +++ b/tests/testthat/_snaps/rd-r6.md @@ -18,13 +18,13 @@ a <- A$new() ## ------------------------------------------------ - ## Method `obj$meth1()` + ## Method `A$meth1()` ## ------------------------------------------------ ## Example for meth1 ## ------------------------------------------------ - ## Method `obj$meth2()` + ## Method `A$meth2()` ## ------------------------------------------------ ## Example for meth2 @@ -63,11 +63,11 @@ \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{
}} - \preformatted{obj$meth1(Z)} + \preformatted{A$meth1(Z)} \if{html}{\out{
}} } @@ -90,11 +90,11 @@ \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{
}} - \preformatted{obj$meth2(Z = 10, ...)} + \preformatted{A$meth2(Z = 10, ...)} \if{html}{\out{
}} } @@ -126,11 +126,11 @@ \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{
}} - \preformatted{obj$meth3(x)} + \preformatted{A$meth3(x)} \if{html}{\out{
}} } @@ -146,11 +146,11 @@ \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{
}} - \preformatted{obj$clone(deep = FALSE)} + \preformatted{A$clone(deep = FALSE)} \if{html}{\out{
}} } @@ -221,11 +221,11 @@ \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{
}} - \preformatted{obj$meth1(Z)} + \preformatted{B$meth1(Z)} \if{html}{\out{
}} } @@ -241,11 +241,11 @@ \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{
}} - \preformatted{obj$meth4()} + \preformatted{B$meth4()} \if{html}{\out{
}} } @@ -253,11 +253,11 @@ \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{
}} - \preformatted{obj$clone(deep = FALSE)} + \preformatted{B$clone(deep = FALSE)} \if{html}{\out{
}} } @@ -328,11 +328,11 @@ \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{
}} - \preformatted{obj$meth2(Z = 10, ...)} + \preformatted{C$meth2(Z = 10, ...)} \if{html}{\out{
}} } @@ -349,11 +349,11 @@ \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{
}} - \preformatted{obj$meth5()} + \preformatted{C$meth5()} \if{html}{\out{
}} }