[Rd] inject html code into Rd file

Duncan Murdoch murdoch at stats.uwo.ca
Sat Apr 3 02:04:32 CEST 2010


On 02/04/2010 8:06 AM, Duncan Murdoch wrote:
> On 02/04/2010 7:13 AM, Romain Francois wrote:
>> Le 02/04/10 13:07, Duncan Murdoch a écrit :
>>> On 02/04/2010 6:17 AM, Romain Francois wrote:
>>>> Hello,
>>>>
>>>> I'm trying to inject html code into an Rd file. For example :
>>>>
>>>> \name{test}
>>>> \alias{test}
>>>> \title{test}
>>>> \description{
>>>> \if{html}{
>>>> \Sexpr[stage=render,results=text,echo=FALSE]{
>>>> "<b>hello</b>"
>>>> }
>>>> }
>>>> }
>>>>
>>>> when this file is rendered, instead of having "hello" in bold, I get
>>>> <b>hello</b>, i.e. characters < and > are replaced with html entities
>>>> : < and >
>>>>
>>>> Is there a way to turn this off ?
>>> Yes, if you wrap it in \out{}. The example in the manual is
>>>
>>> \if{latex}{\out{\alpha}}\ifelse{html}{\out{α}}{alpha}
>>>
>>> Duncan Murdoch
>> yes, I saw that in WRE, I should have been more specific.
>>
>>
>> what if instead of a trivial string like "<b>hello</b>" the text is to 
>> be computed by some function. For example:
>>
>> print( xtable( iris), type = "html" )
> 
> I think this should do it:
> 
> \Sexpr[stage=render,results=rd,echo=FALSE]{
> paste("\\out{", "<b>hello</b>, "}", sep="")}
> 
> but this stuff hasn't been tested much, so there might be problems...

One problem is that the backslashes need to be escaped twice, so you'd want

  \Sexpr[stage=render,results=rd,echo=FALSE]{
  paste("\\\\out{", "<b>hello</b>, "}", sep="")}

and you'd probably want it wrapped in \if or \ifelse so that it doesn't 
show up in text or latex output:

  \Sexpr[stage=render,results=rd, 
echo=FALSE]{"\\\\if{html}{\\\\out{<b>hello</b>}}"}

Duncan Murdoch



More information about the R-devel mailing list