Are rCharts and DT compatible in rmarkdown?
Asked Answered
N

1

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.

Narah answered 13/5, 2015 at 10:55 Comment(3)
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.Kirit
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.Narah
glad to hear, let me know if I can help in any wayKirit
E
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.

Extravagant answered 15/5, 2015 at 14:46 Comment(3)
That solution would never have occurred to me, thanks a lot. We have a bounty winner I believe.Narah
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. rpubs.com/englianhu/Milestone-ReportTrimester
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?Vivisection

© 2022 - 2024 — McMap. All rights reserved.