2
votes

Assume a knitr (rnw) file with a code chunk

<<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)
}
@

Now assume that you want to compile the document and in the resulting pdf have the function documentation included in a style similar to an Rd file. Would that be possible?

1
Why not place the function in a package and a the knitr file as a vignette?Thierry
I often do that. However, often when you write a small note this is to cumbersome.Relund
I think this is possible, but just not straightforward. Basically you will need to write a custom chunk hook function to process the roxygen comments (use roxygen2 to turn them into Rd), then use functions like tools::Rd2latex() to convert Rd to LaTeX, and use the chunk hook to insert LaTeX code into the LaTeX output document.Yihui Xie

1 Answers

1
votes

Based on @Yihui hints I made the solution below where you redefine the latex commands as you like and include rd2latex= as an option. The hack using roxygenize can properly be done better.

\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}