Search code examples
rknitrdocumentationquarto

R functions help into a quarto document


The following code gives the examples codes along their output for first six topics from library.

---
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"
    )
```

enter image description here

I want to tweak this output a little bit.

Expected Output

Wondering how to add each topic title as a section and then its corresponding code and output.

Edited

The helpExtract from SOfun package can be used to extract specified portions of help files for use in or documents.

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]
  )

However, can't figured out how to use this function to extract topic title.

Edited (2023-04-19)

@Julian's solution is effective for the package, but it does not function properly with the package. When attempting to use it with the package, the following error is generated:

Error in purrr::map():i In index: 1.Caused by error in file():! cannot open the connectionError in vctrs_vec_compat(.y, .purrr_user_env): object 'topic_title' not foundError in cat(res, sep = "\n"): object 'res' not found1


Solution

  • Is this what you want? I added a toc to see whether the chapters are as intended (code idea comes from here and here):

    ---
    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), ]
    }
    ```
    
    
    
    ```{r}
    #| 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')
    ```
    

    enter image description here