ramnathv / rCharts

Interactive JS Charts from R
http://rcharts.io
Other
1.19k stars 654 forks source link

Sankey Diagram does not show up in the Rmarkdown but in the local Rstudio #726

Open MengyingLi opened 7 years ago

MengyingLi commented 7 years ago

Hi I am using Sankey Diagram and it works perfectly in my local. However, when I tried to print it to Rmarkdown, it did not work at all. I have followed @ramnathv 's other post in including the options in the chunk and print in the end. This trick works well for all other RCharts but not for Sankey Diagram. The related piece of code is showing as below and in this example, only p2 showed up not p1

````{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(rCharts)
    library(knitr)
```{r set-options, echo=FALSE, cache=FALSE}
options(RCHART_WIDTH = 1000, RCHART_HEIGHT = 400)
```

```{r, echo = FALSE, results = 'asis'}
load("/home/user/R/df_sankey_example.rda")
sankeyPlot_fct=function(df){
  sankeyPlot <- rCharts$new()

  sankeyPlot$setLib('/home/user/rcharts_d3_sankey/')

  sankeyPlot$setTemplate(
    afterScript = "
<script>
// to be specific in case you have more than one chart
d3.selectAll('#{{ chartId }} svg path.link')
  .style('stroke', function(d){
    //here we will use the source color
    //if you want target then sub target for source
    //or if you want something other than gray
    //supply a constant
    //or use a categorical scale or gradient
    return d.source.color;
  })
 //note no changes were made to opacity
 //to do uncomment below but will affect mouseover
 //so will need to define mouseover and mouseout
 //happy to show how to do this also
 .style('stroke-opacity', .7) 
</script>
")
sankeyPlot$set(
    data = df,
    nodeWidth = 25,
    nodePadding = 10,
    layout = 32,
    width = 1800,
    height = 900
  )

  sankeyPlot
}
```{r, echo=FALSE, cache=T, results='asis', comment=NA}
p1= sankeyPlot_fct(df.sankey.plot)
p1$print('chart1', include_assets=T)
````
```{r, echo=FALSE, cache=T, results='asis', comment=NA}

p2 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart') p2$print('chart2', include_assets=T)

henclewood commented 6 years ago

@MengyingLi, did you manage to generate an rCharts Sankey Diagram ("p1") in RMarkdown, html, file?