Translate: 将R函数帮助添加到Quarto文档中

huangapple go评论107阅读模式
英文:

R functions help into a quarto document

问题

The following code gives the examples [tag:R] codes along their output for first six topics from [tag:stats] [tag:R] library.


title: "Help"
format: pdf

  1. #| echo: false
  2. #| class-output: r
  3. library(purrr)
  4. map(
  5. .x = ls("package:stats", all = FALSE)[1:6]
  6. , .f = example
  7. , package = "stats"
  8. , character.only = TRUE
  9. , type = "html"
  10. )

Translate: 将R函数帮助添加到Quarto文档中

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 [tag:R] package can be used to extract specified portions of [tag:R] help files for use in [tag:Sweave] or [tag:R-markdown] 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 figure out how to use this function to extract topic title.

Edited (2023-04-19)

@Julian's solution is effective for the [tag:stats] [tag:R] package, but it does not function properly with the [tag:meta] [tag:R] package. When attempting to use it with the [tag:meta] [tag:R] 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

英文:

The following code gives the examples [tag:R] codes along their output for first six topics from [tag:stats] [tag:R] library.

  1. ---
  2. title: "Help"
  3. format: pdf
  4. ---
  5. ```{r}
  6. #| echo: false
  7. #| class-output: r
  8. library(purrr)
  9. map(
  10. .x = ls("package:stats", all = FALSE)[1:6]
  11. , .f = example
  12. , package = "stats"
  13. , character.only = TRUE
  14. , type = "html"
  15. )
  16. ```

Translate: 将R函数帮助添加到Quarto文档中

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 [tag:R] package can be used to extract specified portions of [tag:R] help files for use in [tag:Sweave] or [tag:R-markdown] documents.

  1. remotes::install_github("mrdwab/SOfun")
  2. library(SOfun)
  3. helpExtract(
  4. Function = acf
  5. , section = c("Description", "Usage", "Arguments", "Details", "Value", "Examples")[1]
  6. , type = c("m_code", "m_text", "s_code", "s_text")[2]
  7. )

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 [tag:stats] [tag:R] package, but it does not function properly with the [tag:meta] [tag:R] package. When attempting to use it with the [tag:meta] [tag:R] 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

答案1

得分: 2

这是你想要的吗?我添加了一个目录来查看章节是否符合预期(代码思路来自这里这里):

  1. ---
  2. title: "帮助"
  3. format: pdf
  4. toc: true
  5. number-sections: true
  6. ---
  1. #| echo: false
  2. # https://stackoverflow.com/questions/8379570/get-functions-title-from-documentation
  3. pkg_topic <- function(package, topic, file = NULL) {
  4. # 找到主题名称/别名对应的“file”名称
  5. if (is.null(file)) {
  6. topics <- pkg_topics_index(package)
  7. topic_page <- subset(topics, alias == topic, select = file)$file
  8. if(length(topic_page) < 1)
  9. topic_page <- subset(topics, file == topic, select = file)$file
  10. stopifnot(length(topic_page) >= 1)
  11. file <- topic_page[1]
  12. }
  13. rdb_path <- file.path(system.file("help", package = package), package)
  14. tools:::fetchRdDB(rdb_path, file)
  15. }
  16. pkg_topics_index <- function(package) {
  17. help_path <- system.file("help", package = package)
  18. file_path <- file.path(help_path, "AnIndex")
  19. if (length(readLines(file_path, n = 1)) < 1) {
  20. return(NULL)
  21. }
  22. topics <- read.table(file_path, sep = "\t",
  23. stringsAsFactors = FALSE, comment.char = "", quote = "", header = FALSE)
  24. names(topics) <- c("alias", "file")
  25. topics[complete.cases(topics), ]
  26. }
  1. #| output: asis
  2. #| echo: false
  3. topic <- ls("package:stats", all = FALSE)[1:6]
  4. topic_title <- topic |>
  5. purrr::map( \(function_name) {
  6. target <- gsub("^.+/library/(.+)/help.+$", "\", utils:::index.search("stats",
  7. find.package()))
  8. doc.txt <- pkg_topic(target, function_name) # 假设Hadley的两个函数都在这里
  9. return(doc.txt[[1]][[1]][1])
  10. })
  11. res <- purrr::map2_chr(topic, topic_title, \(topic, topic_title) {
  12. knitr::knit_child(text = c(
  13. "# `r topic_title`",
  14. "",
  15. "```{r}",
  16. "#| echo: false",
  17. "#| class-output: r",
  18. "example(topic, package = 'stats', character.only = TRUE, type = 'html')",
  19. "```",
  20. "",
  21. ""
  22. ), envir = environment(), quiet = TRUE)
  23. })
  24. cat(res, sep = '\n')

Translate: 将R函数帮助添加到Quarto文档中

  1. <details>
  2. <summary>英文:</summary>
  3. Is this what you want? I added a toc to see whether the chapters are as intended (code idea comes from [here](https://github.com/quarto-dev/quarto-cli/issues/2370) and [here](https://stackoverflow.com/questions/8379570/get-functions-title-from-documentation)):
  4. ---
  5. title: &quot;Help&quot;
  6. format: pdf
  7. toc: true
  8. number-sections: true
  9. ---
  10. ```{r}
  11. #| echo: false
  12. # https://stackoverflow.com/questions/8379570/get-functions-title-from-documentation
  13. pkg_topic &lt;- function(package, topic, file = NULL) {
  14. # Find &quot;file&quot; name given topic name/alias
  15. if (is.null(file)) {
  16. topics &lt;- pkg_topics_index(package)
  17. topic_page &lt;- subset(topics, alias == topic, select = file)$file
  18. if(length(topic_page) &lt; 1)
  19. topic_page &lt;- subset(topics, file == topic, select = file)$file
  20. stopifnot(length(topic_page) &gt;= 1)
  21. file &lt;- topic_page[1]
  22. }
  23. rdb_path &lt;- file.path(system.file(&quot;help&quot;, package = package), package)
  24. tools:::fetchRdDB(rdb_path, file)
  25. }
  26. pkg_topics_index &lt;- function(package) {
  27. help_path &lt;- system.file(&quot;help&quot;, package = package)
  28. file_path &lt;- file.path(help_path, &quot;AnIndex&quot;)
  29. if (length(readLines(file_path, n = 1)) &lt; 1) {
  30. return(NULL)
  31. }
  32. topics &lt;- read.table(file_path, sep = &quot;\t&quot;,
  33. stringsAsFactors = FALSE, comment.char = &quot;&quot;, quote = &quot;&quot;, header = FALSE)
  34. names(topics) &lt;- c(&quot;alias&quot;, &quot;file&quot;)
  35. topics[complete.cases(topics), ]
  36. }
  37. ```
  38. ```{r}
  39. #| output: asis
  40. #| echo: false
  41. topic &lt;- ls(&quot;package:stats&quot;, all = FALSE)[1:6]
  42. topic_title &lt;- topic |&gt;
  43. purrr::map( \(function_name) {
  44. target &lt;- gsub(&quot;^.+/library/(.+)/help.+$&quot;, &quot;\&quot;, utils:::index.search(&quot;stats&quot;,
  45. find.package()))
  46. doc.txt &lt;- pkg_topic(target, function_name) # assuming both of Hadley&#39;s functions are here
  47. return(doc.txt[[1]][[1]][1])
  48. })
  49. res &lt;- purrr::map2_chr(topic, topic_title, \(topic, topic_title) {
  50. knitr::knit_child(text = c(
  51. &quot;# `r topic_title`&quot;,
  52. &quot;&quot;,
  53. &quot;```{r}&quot;,
  54. &quot;#| echo: false&quot;,
  55. &quot;#| class-output: r&quot;,
  56. &quot;example(topic, package = &#39;stats&#39;, character.only = TRUE, type = &#39;html&#39;)&quot;,
  57. &quot;```&quot;,
  58. &quot;&quot;,
  59. &quot;&quot;
  60. ), envir = environment(), quiet = TRUE)
  61. })
  62. cat(res, sep = &#39;\n&#39;)
  63. ```
  64. [![enter image description here][1]][1]
  65. [1]: https://i.stack.imgur.com/Sh6Wa.png
  66. </details>

huangapple
  • 本文由 发表于 2023年4月17日 17:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定