roxygen2
roxygen2 copied to clipboard
Link for R6 method
For normal functions in my package fun, I can add a link to its doc using markdown syntax [fun()].
But I did not find a way to add a link to the doc of an R6 method. Neither [MyClass$Method] and [MyClass$Method()] worked.
This feature will be useful when MyClass is an R6 class which contains lots of methods and it may be better to explicitly link to a specific method in other doc, instead of linking to the whole class doc.
Unfortunately there’s no reliable way to link to specific methods because you can’t link within a Rd file.
I see. That's sad. Shall I keep it open or close it since it is hard to implement?
FWIW, you can create links like this and they'll work in the HTML help, like this:
\href{../../<package>/html/<helppage>.html#<anchor>}{linktext}
and in the other package you need anchors like this:
\if{html}{\out}{<a id="<anchor>"></a>}
You's probably want to put the \href into \if{html} as well.
You can look at https://github.com/r-lib/roxygen2/pull/936/files to see the anchors and the links roxygen currently generates. The additional data-* tags are for (future) pkgdown.
All this being a hack, I am not sure if we want to support it better in roxygen2.
@gaborcsardi That is super useful! It is a good workaround for my package site generated by pkgdown.
I think I'll close this now. We'll see if we'll get any issues with the R6 method links. If not, then maybe we can consider adding support for this is roxygen.
Another vote to support linking to specific methods of an R6 class. Documentation becomes unclear when you have to say "To do x, call the foo() method of the [bar] object" instead of "To do x, call the bar$foo() function`
You can already say the latter, this is how:
#' To do x call the [`bar$foo()`][bar] function.
(Of course it will not go to the position of the method within the page, as mentioned above, there is no supported way to do that. There is a hack-y way, but even that does not work in RStudio.)
As reported in #388, I have the same problem.
How do I link to one particular methods of an R6 class?
#' Test R6 class
#' @export
TestR6 = R6Class(
"TestR6",
public = list(
#' @description Example method
myMethodA = function() {1:10}
)
);
#' Test function
#'
#' This function is a wrapper for [TestR6$myMethodA()] or [TestR6#method-myMethodA]
#' or [TestR6#method-myMethodA()], but neither of these links work....
#' @export
myMethodB = function(r) { r$myMethodA() }
Neither of the links in the documentation of myMethodB appear to correctly link to the TestR6$myMethodA() method, just to the general TestR6 class.... For classes with potentially dozens of methods, this is not very user-friendly.
What is the correct way to link to one particular method?
@gaborcsardi meant to open a new issue, but this issue seems to be exactly what I'd need. Can you please re-open this issue?
@kainhofer , the workaround @gaborcsardi proposed still works for my package, at least in RStudio.
Whenever linking to a specific R6 class Cls method named Fun in your package Pkg, you can do the following:
\href{../../Pkg/html/Cls.html#method-Fun}{\code{XXXX}}
So, for your case, let's say your package name is Pkg, the code blow should work:
\href{../../Pkg/html/TestR6.html#method-myMethodA}{\code{TestR6$myMethodA()}}
@hongyuanjia I'm also using that workaround in my package. However, it only appears to work at first glance. While it jumps to the page documenting the R6-class, it does NOT jump to the particular method (at least not in RStudio's help pane). I find this strange, because the method list in the R6 class documentation also uses the link with # to jump to the section. However, it seems that RStudio only jumps to the correct section within the same .Rd-file, but it does not jump to the correct section when linking to a different .Rd-file.
I have an R6 class with >20 methods and when clicking in a hyperlink I cannot expect my users to remember the name of the method that was displayed on the link and then search that method in the long help page describing all R6 methods...
However, it seems that RStudio only jumps to the correct section within the same .Rd-file, but it does not jump to the correct section when linking to a different .Rd-file.
That's an RStudio bug.
Another issue I have is with links to R6 fields. It seems that roxygen currently does not create any anchors for R6 fields in the html output, so AFAICS there is no way to link to the documentation of a particular R6 field, not even a workaround. Methods create the "method-METHODNAME" anchor that one can use and directly link, but fields do not create anything...
hit this in 2024