R markdown: enable help information

I would like to include at the end of the R markdown document a man page for the mtcars dataset.

In my file, I included the following:

```{r} ?mtcars ``` 

When I compile markdown (the output is PDF-knitr), after processing this instruction, a help page appears in my browser, but as a result of this pdf of this section this section is missing.

Is there a way that I could do this differently and then copy from one place to another?

Thanks.

+5
source share
1 answer

We can adapt the Yihui Xie static_help function to get the html source for this help file

 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 in a temporary file, we can read it and delete the header and footer, providing you the body of the help file for inclusion in your document with a tag.

 ```{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 created HTML output, while the question really asked about PDF output. We can adapt the above to get latex output instead; this time only post-editing is required to switch % to \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, .Rd files are dependent on Rd.sty . The easiest way to get LaTeX to find Rd.sty is to place a copy in the same directory as your .Rmd file. Then you need to define a custom template to replace the default pandoc LaTeX template. Again, the easiest solution is to put a copy of the default template in the same directory as your .Rmd file, and then change it, replacing everything between the \documentclass command and the \begin{document} command (lines 2 - 145) with the command

 \usepackage{Rd} 

Finally, change the metadata of your .Rmd file to use the new template

 --- output: pdf_document: template: template.tex --- 
+3
source

Source: https://habr.com/ru/post/1205152/


All Articles