rknitrigraphtkplot

tkplot in html with knitr


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

Solution

  • 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)
    ```