简体   繁体   English

R markdown文件:包含帮助信息

[英]R markdown file: include help information

I would like to include at the end of the R markdown documention the help page about the mtcars dataset. 我想在R markdown文档的末尾包含有关mtcars数据集的帮助页面。

In my file I included the following: 在我的文件中,我包括以下内容:

```{r}
?mtcars
```

When I compile the markdown (output is to PDF - knitr), upon processing this instruction the help page comes up in my browser but the resulting pdf lacks this section. 当我编译markdown(输出是PDF - knitr)时,在处理此指令时,帮助页面会出现在我的浏览器中,但生成的pdf缺少此部分。

Is there a way I could acheive this other then copying from one place to the other? 有没有办法可以实现这一点,然后从一个地方复制到另一个地方?

Thank you. 谢谢。

We can adapt Yihui Xie's static_help function to get the html source for a given help file 我们可以调整Yihui Xie的static_help函数来获取给定帮助文件的html源代码

static_help <- function(pkg, topic, out, links = tools::findHTMLlinks()) {
  pkgRdDB = tools:::fetchRdDB(file.path(find.package(pkg), 'help', pkg))
  force(links)
  tools::Rd2HTML(pkgRdDB[[topic]], out, package = pkg,
                 Links = links, no_links = is.null(links))
}

If we write the source to a temporary file we can then read it back in and strip off the header and footer, giving you the body of the help file to include in your markdown document 如果我们将源写入临时文件,我们可以将其读回并删除页眉和页脚,为您提供帮助文件的正文以包含在您的降价文档中

```{r, echo = FALSE, results = "asis"}
static_help <- function(pkg, topic, out, links = tools::findHTMLlinks()) {
  pkgRdDB = tools:::fetchRdDB(file.path(find.package(pkg), 'help', pkg))
  force(links)
  tools::Rd2HTML(pkgRdDB[[topic]], out, package = pkg,
                 Links = links, no_links = is.null(links))
}
tmp <- tempfile()
static_help("datasets", "mtcars", tmp)
out <- readLines(tmp)
headfoot <- grep("body", out)
cat(out[(headfoot[1] + 1):(headfoot[2] - 1)], sep = "\n")
```

EDIT 编辑

The above solution produced HTML output, whereas the question actually asked for PDF output. 上面的解决方案产生了HTML输出,而实际上问题是PDF输出。 We can adapt the above to return latex output instead; 我们可以改编以上来代替乳胶输出; this time the only-post-editing required is to switch % for \\n 这次唯一需要的后期编辑是切换\\n %

```{r, echo = FALSE, results = "asis"}
static_help <- function(pkg, topic, out, links = tools::findHTMLlinks()) {
  pkgRdDB = tools:::fetchRdDB(file.path(find.package(pkg), 'help', pkg))
  force(links)
  tools::Rd2latex(pkgRdDB[[topic]], out, package = pkg,
                  Links = links, no_links = is.null(links))
}
tmp <- tempfile()
static_help("datasets", "mtcars", tmp)
out <- readLines(tmp)
out <- gsub("%", "\n", out, fixed = TRUE)
cat(out, sep = "\n")
```

However the .Rd files depend on Rd.sty . 但是.Rd文件依赖于Rd.sty The simplest way to get LaTeX to find Rd.sty is to put a copy in the same directory as your .Rmd file. 让LaTeX找到Rd.sty的最简单方法是将副本放在与.Rmd文件相同的目录中。 Then you need to define a custom template to replace the default pandoc LaTeX template . 然后,您需要定义自定义模板以替换默认的pandoc LaTeX模板 Again, the simplest solution is to put a copy of the default template in the same directory as your .Rmd file, then modify it by replacing everything between the \\documentclass command and the \\begin{document} command (lines 2 - 145) with the command 同样,最简单的解决方案是将默认模板的副本放在与.Rmd文件相同的目录中,然后通过替换\\documentclass命令和\\begin{document}命令(第2 - 145行)之间的所有内容来修改它。命令

\usepackage{Rd}

Finally modify the metadata of your .Rmd file to use the new template 最后修改.Rmd文件的元数据以使用新模板

---
output:
    pdf_document:
        template: template.tex
---

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

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