【问题标题】:Roxygen2 documentation in a knitr documentknitr 文档中的 Roxygen2 文档
【发布时间】:2015-12-30 15:46:23
【问题描述】:

假设一个带有代码块的 knitr (rnw) 文件

<<function, include=FALSE>>=
#' A simple function
#'
#' @param foo Variable foo.
#' @param bar Variable bar. 
#'
#' @return The product of foo and bar
product<-function(foo, bar) {
   return(foo*bar)
}
@

现在假设您要编译该文档,并在生成的 pdf 中将函数文档包含在类似于 Rd 文件的样式中。这可能吗?

【问题讨论】:

  • 为什么不将函数放在一个包中,并将 knitr 文件作为小插图?
  • 我经常这样做。然而,当你写一个小笔记时,这通常很麻烦。
  • 我认为这是可能的,但并不简单。基本上你需要编写一个自定义的块挂钩函数来处理 roxygen cmets(使用 roxygen2 将它们变成 Rd),然后使用像 tools::Rd2latex() 这样的函数将 Rd 转换为 LaTeX,并使用块将 LaTeX 代码插入 LaTeX 输出文档的钩子。

标签: r latex knitr roxygen2


【解决方案1】:

根据@Yihui 的提示,我制作了下面的解决方案,您可以根据需要重新定义latex 命令并包含 rd2latex= 作为选项。使用 roxygenize 的 hack 可以做得更好。

\documentclass{article}
\usepackage{framed}
% Redefine latex commands for Rd output
\usepackage{ifthen}
\usepackage{verbatim}
\newcommand{\HeaderA}[3]{}
\newcommand{\code}[1]{\texttt{#1}:}
%\newenvironment{Description}{\iffalse}{\fi}
\newenvironment{Description}%
        {%
            \ifthenelse{\isundefined{\showtodos}}%
                    {\expandafter\comment}%
                    {}%
                    }%
         {%
            \ifthenelse{\isundefined{\showtodos}}%
                    {\expandafter\endcomment}%
                    {}%
          }
\newenvironment{Usage}{}{}
\newenvironment{Arguments}{}{}
\newenvironment{Value}{Returns:}{}
\newenvironment{ldescription}{\begin{itemize}}{\end{itemize}}

\parindent0pt
\begin{document}

<<ini, include=FALSE>>=
library(tools)
library(roxygen2)
library(knitr)
knit_hooks$set(rd2latex = function(before, options, envir) {
    if (before) {
      # hack: to run roxygenise() folders man, R and a DESC file must be created
      basePath <- normalizePath(".")
      manPath <- file.path(basePath, "man")
      rPath <- file.path(basePath, "R")
      fileConn<-file("DESCRIPTION")
      writeLines("Package: tmp", fileConn)
      close(fileConn)
      dir.create(rPath, recursive = TRUE, showWarnings = FALSE)
      # save code to an R file
      fName<-paste0("chunk_",options$label)
      fileConn<-file(paste0("R/",fName,".R") )
      writeLines(options$code, fileConn)
      close(fileConn)
      # generate Rd file
      roxygenise()
      rdFiles <- list.files("man",full.names = TRUE)
      # convert to a latex file which can be included using \input{}
      if (options$rd2latex==TRUE) Rd2latex(rdFiles,out=paste0(fName,".tex"))
      else Rd2latex(rdFiles,out=paste0(options$rd2latex,".tex"))
      # remove working files
      unlink(c(manPath,rPath,"DESCRIPTION"), recursive = TRUE, force = TRUE)
    }
})
@

\subsection*{Testing}

<<function, include=FALSE, rd2latex='product'>>=
#' A simple function
#'
#' @param foo Variable foo.
#' @param bar Variable bar.
#'
#' @return The product of foo and bar
product<-function(foo, bar) {
   return(foo*bar)
}
@

A simple product function is:

\begin{framed}
\input{product.tex}
\end{framed}

\end{document}

【讨论】:

    猜你喜欢
    • 2013-09-24
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2018-10-13
    • 2017-09-15
    相关资源
    最近更新 更多