[Rd] R CMD check and error in an \Sexpr in an Rd file

Renaud Gaujoux renaud at mancala.cbio.uct.ac.za
Fri Nov 4 11:12:44 CET 2011


Thank you Duncan for pointing this out. I did have tried using the 
command \out with no success, but I finally manage to do what I want with:

\if{html}{\Sexpr[results=rd, 
stage=render]{"\\\\out{<pre>line\nnewline\n\nother line</pre>}"}}
\if{text}{\Sexpr[results=rd, 
stage=render]{"\\\\out{line\nnewline\n\nother line}"}}
\if{latex}{\Sexpr[results=rd, 
stage=render]{"\\\\out{\\\\begin{verbatim}line\nnewline\n\nother 
line\\\\end{verbatim}}"}}

The only remaining issue is with format 'text', that does not show 
single break lines. Double break lines are shown correctly with an empty 
line.
This can be put in a macro:

\newcommand{\rdVerb}{\if{html}{\Sexpr[results=rd, 
stage=render]{paste("\\\\\\out{<pre>",#1,"</pre>}", 
sep='')}}\if{text}{\Sexpr[results=rd, 
stage=render]{paste("\\\\\\out{",#1,"}", 
sep='')}}\if{latex}{\Sexpr[results=rd,stage=render]{paste("\\\\\\out{\\\\\\\\begin{verbatim}",#1,"\\\\\\\\end{verbatim}}", 
sep='')}}
}


which can be used as:

\rdVerb{"line\nnew line\n\nother paragraph"}

Dynamic code works as well:

\rdVerb{paste("This is the result of tools:::Rd_expr_PR(1234):", 
tools:::Rd_expr_PR(1234), sep="")}
\rdVerb{paste("This is the result of tools:::Rd_expr_PR(1234):", 
tools:::Rd_expr_PR(1234), sep="\n")}

But backslashes must be escaped twice:

\rdVerb{"\\\\\begin{section}\ntest\nsd\n\n\\\\\end{section}"}

Note that on the text version generated by the second \VERBATIM actually 
correctly shows the new line. Not sure why (?).

Renaud

-- 
Renaud Gaujoux
Computational Biology - University of Cape Town
South Africa


On 03/11/2011 18:28, Duncan Murdoch wrote:
> On 03/11/2011 10:49 AM, Renaud Gaujoux wrote:
>> On 03/11/2011 16:28, Duncan Murdoch wrote:
>> >  On 03/11/2011 10:21 AM, Renaud Gaujoux wrote:
>> >>  Thank you Georgi.
>> >>  With the fix \long the output is indeed consistent with the
>> >>  documentation.
>> >>  I think my use of cat() worked by luck as its output should not have
>> >>  been rendered.
>> >>
>> >>  Would a 'results=tex' (html, text) be possible? or 
>> 'results=source' that
>> >>  could be combined with \if{format}{text}?
>> >>  This would allow to generate custom Latex, or HTML code, but it 
>> might
>> >>  also be is hazardous...
>> >
>> >  Yes, that's possible.  See the manual, especially the section "2.12
>> >  conditional text".
>> >
>> >  Duncan Murdoch
>>
>> Conditional text is possible, but latex or html code seem to be
>> preprocessed and escaped, or maybe I am not doing the right things.
>
> Not if you ask it not to do that.  See the example in that section of 
> the manual.
>
> Duncan Murdoch
>
>> To test:
>>
>> %%%%%%%%
>> \name{Sexpr}
>> \alias{Sexpr}
>> \title{Error and verbatim in Sexpr}
>> \description{
>>       Testing Sexpr in Rd files
>>
>>       \if{html}{\Sexpr[results=text, stage=render]{"<bold>text in
>> bold</bold>"}}
>>       \if{text}{\Sexpr[results=text, stage=render]{"_text in bold_"}}
>>       \if{latex}{\Sexpr[results=text, stage=render]{"\\\\textbf{text in
>> bold}"}}
>>
>>
>>
>> }
>> %%%%%%%
>



More information about the R-devel mailing list