Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are rCharts and DT compatible in rmarkdown?

I am trying to create a document with rmarkdown that includes both plots from the rCharts package and a datatable using the DT library included in htmlwidgets. For some reason I cannot display both of them together.

---
title: "Untitled"
output: html_document
---

```{r, echo=FALSE}
library(DT)
library(rCharts)

df<-data.frame(Name=c("a","Z","h","k","j"),Value=(sample(10^7,5)))

datatable(df, filter = 'top', options = list(
  pageLength = 10,iDisplaylength=10, autoWidth = TRUE
))
```

```{r, message=FALSE, echo=FALSE, results='asis'}
df<-data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),
               othera=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')

#Different options I tried

p1$print('inline', include_assets = TRUE, cdn = FALSE)
#p1$show('inline', include_assets = TRUE, cdn = FALSE)

#p1$print('inline', include_assets = TRUE)
#p1$show('inline', include_assets = TRUE)

#These provide an error
#p1$print('inline', include_assets = TRUE, cdn = TRUE)
#p1$show('inline', include_assets = TRUE, cdn = TRUE)

```

The commented lines are the things I have tried.

Note I: if p1$print('inline', include_assets = TRUE, cdn = FALSE) is commented the datatable is displayed properly.

Note II: I am aware of p1$save() function combined with an iframe, however, I would like to use the chart inline.

like image 374
Jon Nagra Avatar asked May 13 '15 10:05

Jon Nagra


1 Answers

The jQuery library is included at the top of the page and when you include_assets in the print, the it is included again which causes issues.

To fix this, you can try setting include_assets to false and adding the required libraries except jQuery "by hand".

 p1 <- nPlot(valuea~ label, data = df, type = 'pieChart')
    cat("<link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/nv.d3.css>
    <link rel='stylesheet' href=.../R/3.1/library/rCharts/libraries/nvd3/css/rNVD3.css>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/d3.v3.min.js></script>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/nv.d3.min-new.js></script>
    <script type='text/javascript' src=.../R/3.1/library/rCharts/libraries/nvd3/js/fisheye.js></script> ")
    p1$print('inline', include_assets = F, cdn = FALSE)

You can find the required libraries and links by running p1$print('inline', include_assets = T, cdn = FALSE) in R, they will be the first lines of output. The src paths are absolute so I replaced some of it by ... in the code above.

like image 116
NicE Avatar answered Oct 08 '22 11:10

NicE