Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using knitr (from .Rhtml to html): how to embed a link in an R Figure?

I am using knit to convert my .Rhtml file to an .html file. I am calling the output of a chunk called Q1:

<!--begin.rcode Q1,echo=FALSE,fig.show="all",fig.align="center",warning=FALSE 
end.rcode--> 

Here comes the chunk, it is basically a ggplot2 figure in a 2x2 layout.

library(ggplot2)
myplot = list()
   for (i in 1:4){
          x = 1:100
          y = sample(100,100)
          data = data.frame(x=x,y=y)
          myplot[[i]] = ggplot(data,aes(x=x,y=y))+geom_point()+labs(title="bla")}

do.call(grid.arrange,c(myplot,list(nrow=2,ncol =2)))

Now, when looking at the resulting html file, I would like to incorporate the following feature: I would like to have a link (e.g. to a database) when clicking on the title of each plot. Is this somehow possible?

Thx

like image 757
steffi Avatar asked Dec 21 '12 16:12

steffi


1 Answers

This doesn't completely answer your question, but it might get you or someone else started on a full answer.

Paul Murrel's gridSVG package (see also this useful pdf doc) allows one to add hyperlinks to grid-based SVG graphics. (In theory it should thus work with ggplot2; in practice I've just got it working with lattice). The current issue of the R Journal includes a couple of articles ("What's in a name?" and "Debugging grid graphics." -- Warning: pdfs) that might help you to best design dynamic searches for name of the grob to which you'd like to add a link (as in my second line of code).

library(gridSVG)
library(lattice)

xyplot(mpg~wt, data=mtcars, main = "Link to R-project home")
mainGrobName <- grep("main", grid.ls()[[1]], value=TRUE)
grid.hyperlink(mainGrobName, "http://www.r-project.org")
gridToSVG("HyperlinkExample.svg")
like image 168
Josh O'Brien Avatar answered Sep 20 '22 18:09

Josh O'Brien