Skip to content

Instantly share code, notes, and snippets.

@kbroman
Created February 23, 2015 15:07
Show Gist options
  • Save kbroman/a4b70d6eb582b9dc5141 to your computer and use it in GitHub Desktop.
Save kbroman/a4b70d6eb582b9dc5141 to your computer and use it in GitHub Desktop.
Problem with multiple htmlwidgets within an R Markdown document
---
title: "test qtlcharts"
author: "Karl Broman"
date: "February 23, 2015"
output: html_document
---
```{r load_lib}
library(devtools)
install_github("kbroman/qtlcharts", ref="htmlwidgets")
library(qtlcharts)
```
If I include multiple htmlwidgets charts within an R Markdown document,
only the resources for the first of them gets included.
For example, in the following, the two `iplot` examples will be shown,
but the `iplotPXG` example gives an error, `Uncaught ReferenceError: iplotPXG is not defined`.
If I move the `iplotPXG` chunk to before the `iplot` chunks, then
the `iplotPXG` example works, but the `iplot` examples give an error.
```{r iplot}
x <- rnorm(100)
grp <- sample(1:3, 100, replace=TRUE)
y <- x*grp + rnorm(100)
iplot(x, y, grp)
```
```{r iplot2}
iplot(y, x, grp)
```
```{r iplotPXG}
library(qtl)
data(hyper)
iplotPXG(hyper, "D4Mit164")
```
---
title: "test qtlcharts"
author: "Karl Broman"
date: "February 23, 2015"
output: html_document
---
```{r load_lib}
library(devtools)
install_github("kbroman/qtlcharts", ref="htmlwidgets")
library(qtlcharts)
```
If I include multiple htmlwidgets charts within an R Markdown document,
only the resources for the first of them gets included.
For example, in the following, the `iplotPXG` example will be shown,
but the `iplot` examples give an error.
If I move the `iplot` chunks to before the `iplotPXG` chunk, then
the `iplot` examples work, but the `iplotPXG` example give an error.
```{r iplotPXG}
library(qtl)
data(hyper)
iplotPXG(hyper, "D4Mit164")
```
```{r iplot}
x <- rnorm(100)
grp <- sample(1:3, 100, replace=TRUE)
y <- x*grp + rnorm(100)
iplot(x, y, grp)
```
```{r iplot2}
iplot(y, x, grp)
```
@timelyportfolio
Copy link

yaml reads name to determine diff, so the best way to handle would be to change the name, or if qtlcharts dependencies load all scripts with an array for script.

 - name: qtlcharts
    src: htmlwidgets/lib/qtlcharts
    version: 0.1.1
    script:
      - iplot.js

is treated the same as

  - name: qtlcharts
    src: htmlwidgets/lib/qtlcharts
    version: 0.1.1
    script:
      - iplotPXG.js

If you do not want an array of scripts so just load necessary dependencies, then this should make that happen.

 - name: qtlcharts_core
    src: htmlwidgets/lib/qtlcharts
    version: 0.1.1
    script:
      - iplot.js
  - name: qtlcharts_pxg
    src: htmlwidgets/lib/qtlcharts
    version: 0.1.1
    script:
      - iplotPXG.js

@timelyportfolio
Copy link

@kbroman
Copy link
Author

kbroman commented Feb 23, 2015

@timelyportfolio Thanks! That's understandable.

I guess the thing for me to do is to have a separate chunk of yaml for each of the basic bits, so it loads just what's necessary, and only once.

@timelyportfolio
Copy link

Yes, I think that is the best option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment