R函数帮助转换成夸托文档

velaa5lx  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(141)

下面的代码给出了r代码示例沿着statsr库中前六个主题的输出。

---
title: "Help"
format: pdf
---

```{r}
#| echo: false
#| class-output: r
library(purrr)
map(
      .x             = ls("package:stats", all = FALSE)[1:6] 
    , .f             = example
    , package        = "stats"
    , character.only = TRUE
    , type           = "html"
    )

![](https://i.stack.imgur.com/DJTix.png)

我想稍微调整一下这个输出。

**预期产出**

想知道如何将每个主题标题添加为一个部分,然后添加其相应的代码和输出。

**已编辑**

`SOfun` [r](https://stackoverflow.com/questions/tagged/r)软件包中的`helpExtract`可用于提取[r](https://stackoverflow.com/questions/tagged/r)帮助文件的指定部分,以在[sweave](https://stackoverflow.com/questions/tagged/sweave)或[r-markdown](https://stackoverflow.com/questions/tagged/r-markdown)文档中使用。

remotes::install_github("mrdwab/SOfun")

library(SOfun)

helpExtract(
Function = acf
, section = c("Description", "Usage", "Arguments", "Details", "Value", "Examples")[1]
, type = c("m_code", "m_text", "s_code", "s_text")[2]
)


但是,不知道如何使用此功能来提取主题标题。
hfwmuf9z

hfwmuf9z1#

这是你想要的吗?我添加了一个toc来查看章节是否如预期的那样(代码想法来自herehere):

---
title: "Help"
format: pdf
toc: true
number-sections: true
---

```{r}
#| echo: false

# https://stackoverflow.com/questions/8379570/get-functions-title-from-documentation
pkg_topic <- function(package, topic, file = NULL) {
  # Find "file" name given topic name/alias
  if (is.null(file)) {
    topics <- pkg_topics_index(package)
    topic_page <- subset(topics, alias == topic, select = file)$file

    if(length(topic_page) < 1)
      topic_page <- subset(topics, file == topic, select = file)$file

    stopifnot(length(topic_page) >= 1)
    file <- topic_page[1]    
  }

  rdb_path <- file.path(system.file("help", package = package), package)
  tools:::fetchRdDB(rdb_path, file)
}

pkg_topics_index <- function(package) {
  help_path <- system.file("help", package = package)

  file_path <- file.path(help_path, "AnIndex")
  if (length(readLines(file_path, n = 1)) < 1) {
    return(NULL)
  }

  topics <- read.table(file_path, sep = "\t", 
    stringsAsFactors = FALSE, comment.char = "", quote = "", header = FALSE)

  names(topics) <- c("alias", "file") 
  topics[complete.cases(topics), ]
}
#| output: asis
#| echo: false

topic <- ls("package:stats", all = FALSE)[1:6]
topic_title <- topic |> 
  purrr::map( \(function_name) {
    target <- gsub("^.+/library/(.+)/help.+$", "\\1", utils:::index.search("stats", 
                                                                  find.package()))
    doc.txt <- pkg_topic(target, function_name)  # assuming both of Hadley's functions are here

    return(doc.txt[[1]][[1]][1])
  })
  

res <- purrr::map2_chr(topic, topic_title, \(topic, topic_title) {
    knitr::knit_child(text = c(
      "# `r topic_title`",
      "", 
      "```{r}",
      "#| echo: false",
      "#| class-output: r",
      "example(topic, package = 'stats', character.only = TRUE, type = 'html')",
      "```",
      "",
      ""
    ), envir = environment(), quiet = TRUE)
  })

cat(res, sep = '\n')

![](https://i.stack.imgur.com/Sh6Wa.png)

相关问题