1

I am attempting to include a tkplot from the igraph package inside an HTML (knit from Rmd) document via the knitr package. I asked a very similar question about latex (which should be more difficult) here. Yihui answered but I can't transfer that hook and learning to this situation. How can I embed the following tkplot in an HTML document using an Rmd file in knitr?

```{r setup, include=FALSE}
library(igraph)
library(tcltk)
```


```{r}
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
tkplot(g)
```
Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 1
    The key is to obtain a plot that the web browser can recognize, such as a png image. I think there are multiple ways to get there, e.g. (1) do not use `tkplot()` (I suppose that is not the only plotting function in `igraph`); (2) `rglplot()` with the `hook_rgl` in knitr? (3) convert the postscript image exported from `tkplot()` to png? ... I'll leave it to other people to hack. – Yihui Xie Oct 20 '13 at 01:04

1 Answers1

0

Less attractive but following Yihui's advice:

```{r setup, include=FALSE}
library(igraph)
library(rgl)

knit_hooks$set(
    webgl = hook_webgl

)
```


```{r, webgl=TRUE, fig.width=12, fig.height=12}
edges <- structure(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "A", "B", "C", 
    "D", "E", "F", "G", "H", "I", "J", "E", "G", "G", "F", "H", "G", 
    "D", "J", "J", "D", "B", "C", "D", "I", "I", "H", "A", "B", "G", 
    "I", "F", "D", "F", "J", "D", "B", "E", "E", "A", "E"), .Dim = c(30L, 
    2L), .Dimnames = list(NULL, c("person", "choice")))

g <- graph.data.frame(edges, directed=TRUE)
rglplot(g)
```
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519