[Rd] inject html code into Rd file

Yihui Xie xie at yihui.name
Sat Apr 3 21:46:37 CEST 2010


Sounds like a good idea. The RCurl package can also do the base64
encoding (depends on libcurl), e.g.

library(RCurl)
img <- function() {
    tf <- tempfile()
    tf.out <- tempfile()
    png(tf, width = 500, height = 500)
    plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2)
    dev.off()
    img <- readBin(tf, "raw", file.info(tf)[1, "size"])
    b64 <- base64Encode(img, "character")
    sprintf("<img src=\"data:image/png;base64,%s\" />", b64)
}
writeLines(img(), "test.html")

I saw your blog post today about your base64 package. My concern is IE
(<=7) does not support data uri...

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-6609 Web: http://yihui.name
Department of Statistics, Iowa State University
3211 Snedecor Hall, Ames, IA



On Sat, Apr 3, 2010 at 3:11 AM, Romain Francois
<romain at r-enthusiasts.com> wrote:
> Le 03/04/10 02:04, Duncan Murdoch a écrit :
>>
>> 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
>
> Thanks.
> This gives one way to include images in a Rd file with data uri, here is a
> proof of concept (that depends on openssl to do the base 64 encoding):
>
> img <- function(){
>        tf <- tempfile()
>        tf.out <- tempfile()
>        png( tf, width = 500, height = 500)
>        plot( 1:100, rnorm(100), pch = 21, bg = "red", cex =2 )
>        dev.off()
>        system( sprintf( 'openssl base64 -in "%s" -out "%s" ', tf, tf.out ) )
>        sprintf( '\\out{<img src="data:image/png;base64,%s" />}',
>                paste( readLines( tf.out), collapse = "\n" ) )
> }
>
> and the Rd file:
>
> \name{test}
> \alias{test}
> \title{test}
> \description{
> \if{html}{
> \Sexpr[stage=render,results=rd,echo=FALSE]{
>        source( "test.R" )
>        img()
> }
> }
> }
>
>
>
> It might be interesting to have something like results=asis or something.
>
> Romain
>
> --
> Romain Francois
> Professional R Enthusiast
> +33(0) 6 28 91 30 30
> http://romainfrancois.blog.free.fr
> |- http://tr.im/OIXN : raster images and RImageJ
> |- http://tr.im/OcQe : Rcpp 0.7.7
> `- http://tr.im/O1wO : highlight 0.1-5
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list