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

自闭症网瘾萝莉.ら 提交于 2019-12-12 10:47:35

问题


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


回答1:


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")


来源:https://stackoverflow.com/questions/13993725/using-knitr-from-rhtml-to-html-how-to-embed-a-link-in-an-r-figure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!