14

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.

Jon Nagra
  • 1,538
  • 1
  • 16
  • 36
  • Good question. In general in the future you will be much better off using `htmlwidgets` than `rCharts`. There are a couple good options for pie charts. – timelyportfolio Mar 15 '16 at 15:48
  • Lately, I work more often with the javascript libraries directly for my reports at work. In any case, libraries like htmlwidgets and rmarkdown provide a lot of possibilities for r analysts (especially when you are short on time) and the development they have had in the last couple of years is amazing. I cannot lose this opportunity to thank you for your tremendous effort with buildingwidgets, I used few of them but they gave me a lot of insight. Such altruistic efforts are hard to see and I wanted to show my gratitude. – Jon Nagra Mar 18 '16 at 17:36
  • glad to hear, let me know if I can help in any way – timelyportfolio Mar 18 '16 at 20:27

1 Answers1

7

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.

NicE
  • 21,165
  • 3
  • 51
  • 68
  • That solution would never have occurred to me, thanks a lot. We have a bounty winner I believe. – Jon Nagra May 15 '15 at 15:37
  • I tried `p1$print('inline', include_assets = F, cdn = FALSE)`, `p1$show('inline', include_assets = F, cdn = FALSE)`, `p1`, `p1$show()`, `p1$print()` but no one is workable to me. http://rpubs.com/englianhu/Milestone-Report – Rγσ ξηg Lιαη Ημ 雷欧 Apr 25 '16 at 21:36
  • hello, im trying the solution you provided on a flexdashboard. the only output im getting is a the menu. everything else is empty. any idea what might be the problem? – Prometheus Jun 13 '17 at 14:49