简体   繁体   English

为不在包中的 R6 类创建 Rd 文档文件

[英]creating Rd documentation files for R6 classes not in a package

I am trying to create the documentation of a few scripts that have some R6 classes in it.我正在尝试创建一些包含一些 R6 类的脚本的文档。 As an example I use the R6Class named "Person" from here: https://www.tidyverse.org/blog/2019/11/roxygen2-7-0-0/作为一个例子,我从这里使用名为“人”的 R6Class: https ://www.tidyverse.org/blog/2019/11/roxygen2-7-0-0/

I am using this function https://stackoverflow.com/a/59073260/12166017 to create the documentation without the need of creating a package.我正在使用此功能https://stackoverflow.com/a/59073260/12166017来创建文档,而无需创建包。 Is it also possible to use this for R6Classes or is there maybe another way to achieve this?是否也可以将它用于 R6Classes 或者是否有另一种方法来实现这一目标? With the mentioned function I always get an Error R6 Class (Person) without source references.使用提到的函数,我总是得到一个R6 Class (Person) without source references.的错误R6 Class (Person) without source references.

The solution of the official tidyverse blog https://www.tidyverse.org/blog/2019/11/roxygen2-7-0-0/ to write Roxygen: list(r6 = FALSE) in the DESCRIPTION file does not work because I don't have a package and so no DESCRIPTION file. tidyverse官方博客https://www.tidyverse.org/blog/2019/11/roxygen2-7-0-0/Roxygen: list(r6 = FALSE)文件中写Roxygen: list(r6 = FALSE)的解决方法不行,因为我没有包,所以没有描述文件。

One way to do this is to hijack some of roxygen2's unexported functions to create the block object for the documented R6 class and write an Rd file.一种方法是劫持 roxygen2 的一些未导出的函数,为记录的 R6 类创建块对象并写入Rd文件。 This can then be parsed and written to html using the tools package.然后可以使用tools包将其解析并写入 html。

This is a very rough proof-of-concept, requiring your R6 definition to be in a standalone file, and not taking any arguments to allow saving to specific locations, etc, but it could be adapted and expanded to suit:这是一个非常粗略的概念验证,要求您的 R6 定义在一个独立文件中,并且不接受任何参数以允许保存到特定位置等,但可以对其进行调整和扩展以适应:

document_R6 <- function(R_file)
{
  blocks  <- lapply(roxygen2:::tokenize_file(R_file), roxygen2:::block_set_env,
                    env = .GlobalEnv)
  blocks  <- roxygen2:::order_blocks(blocks)
  roclet  <- roxygen2:::roclet("rd")
  my_rd   <- suppressWarnings(roxygen2:::roclet_process(roclet, blocks))
  my_rd   <- my_rd[[1]]$format()
  rd_file <- tempfile()
  writeLines(my_rd, rd_file)
  tools::Rd2HTML(tools::parse_Rd(rd_file), 
                 gsub("\\.R$", ".html", R_file))
}

So if we have the following file, taken from your link:因此,如果我们有以下文件,取自您的链接:

Person.R人物R

#' R6 Class representing a person
#'
#' A person has a name and a hair color.
Person <- R6::R6Class("Person",
  public = list(
    #' @field name First or full name of the person.
    name = NULL,

    #' @field hair Hair color of the person.
    hair = NULL,

    #' @description
    #' Create a new person object.
    #' @param name Name.
    #' @param hair Hair color.
    #' @return A new `Person` object.
    initialize = function(name = NA, hair = NA) {
      self$name <- name
      self$hair <- hair
      self$greet()
    },

    #' @description
    #' Change hair color.
    #' @param val New hair color.
    #' @examples
    #' P <- Person("Ann", "black")
    #' P$hair
    #' P$set_hair("red")
    #' P$hair
    set_hair = function(val) {
      self$hair <- val
    },

    #' @description
    #' Say hi.
    greet = function() {
      cat(paste0("Hello, my name is ", self$name, ".\n"))
    }
  )
)

Then we can do:然后我们可以这样做:

document_R6("Person.R")

The following is a screenshot of the resulting rendered "Person.html" file, found in the same directory as "Person.R":以下是生成的“Person.html”文件的屏幕截图,该文件与“Person.R”位于同一目录中:


在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM