[R] How to include the documentation of a function in a Sweave document?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Feb 27 02:18:57 CET 2008


Thibaut Jombart wrote:
> Duncan Murdoch wrote:
>   
>>>>   
>>>>         
>>> Maybe a clue: we can use
>>> cat(readLines(as.character(?plot)),sep="\n")
>>>
>>> to display the help (here, of plot) directly to the screen. So we could
>>> use something like:
>>>
>>> <<echo=TRUE,print=FALSE,eval=FALSE>>=
>>> ?plot
>>> @
>>>
>>>
>>> <<echo=FALSE,print=TRUE,eval=TRUE>>=
>>> cat(readLines(as.character(?plot)),sep="\n")
>>> @
>>>
>>> But this doesn't work (latex compilation error) as weird characters 
>>> appear in the produced tex, at some places (tabulations?), like:
>>>
>>> _T_h_e _D_e_f_a_
>>>
>>> (not sure what it will look like in this email, but emacs reads things
>>> like _^HT_^HH_^He...).
>>>
>>> Maybe an encoding problem? I tried specifying different encoding to 
>>> readLines, with no luck (latin1, UTF-8). Otherwise, the help appears 
>>> in the .tex.
>>>       
>> Those are backspaces:  it's trying to underline the title.  You'd get 
>> a better display if you read the latex version instead.  I think you 
>> need to construct the path to it yourself (using system.file() etc.)
>>
>> Duncan Murdoch
>>
>>
>>
>>     
> Thanks for the hint !
> So, the code below roughly works:
>
> #######
> \documentclass{article}
> \usepackage{verbatim}
> \begin{document}
> <<echo=TRUE,print=FALSE,eval=FALSE>>=
> ?plot
> @
>
>
> <<echo=FALSE,print=FALSE,results=tex>>=
> path <- sub("help","latex",as.character(?plot))
> path <- paste(path,'tex',sep=".")
> cat(readLines(path),sep="\n")
> @
>
> \end{document}
> #######
>
> The document compiles with pdflatex but still complains about a bunch of 
> unknown latex instructions (like \HeaderA, used at the begining of the 
> included (plot.tex) tex file. The resulting pdf indeed looks a bit nasty.
>
> What command shall we include in the header to have it work?
>   

The refman.tex file starts like this:

\documentclass[a4paper]{book}
\usepackage[times,hyper]{Rd}
\usepackage[latin1]{inputenc}
\usepackage{makeidx}

Those packages should do it.  (Rd.sty is in R_HOME/share/texmf).

Duncan Murdoch



More information about the R-help mailing list