3

I built a web page using the amazing rCharts and knitr. My page combines rickshaw time series charts with sliders and a data table. When I add the datatable, all the sliders disappear from my rickshaw charts. I tried modifying the config.yml so that I didn't duplicate a call to the same resources, but that didn't resolve the issue. Any ideas would be a big help. Below is a reproducible r-markdown file that can be knit into an html page with knitr.

```{r echo = F, message = T, cache = F}
require(rCharts)
opts_knit$set(self.contained=T)
knitr::opts_chunk$set(results = 'asis', tidy = F, message = T, echo=F, fig.width=700)
```

### Chart
```{r chart}
# add dummy date column to iris to make a time series chart
x <- cbind(iris, date=seq.Date(as.Date("2010-01-01"), length.out=nrow(iris), by=1))
x$date <- as.double(as.POSIXct(x$date,origin="1970-01-01"))
r2 <- Rickshaw$new()
r2$layer(
  Sepal.Length ~ date,
  data = x,
  type = "line",
  min = "auto"
)
r2$set(
  slider = TRUE,
  title = "IRIS",
  width = 700
)
r2$print('chart', include_assets=T, cdn=T)
```

### Table
```{r table}
r2 <- dTable(iris)
r2$print('table', include_assets=T, cdn=T)
```
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jdl
  • 31
  • 1

1 Answers1

5

The problem occurs because jQuery is being included twice. A quick fix would be to add a document hook to knitr that will remove the line that duplicates jQuery. Make sure to set cache = F for this chunk, so that it is run every time you invoke knitr. In a future version, I will add a jQuery argument to print so that you can turn it off, if you have already loaded jQuery on your page.

```{r cache = F}
library(knitr)
knit_hooks$set(document = function(doc){
  pat = "<script type='text/javascript' src=http://code.jquery.com/jquery-1.10.2.min.js></script>"
  gsub(pat, "", doc)
})
```
Ramnath
  • 54,439
  • 16
  • 125
  • 152