Saving/exporting SunburstR (package) plot

Viewed 30

I am looking for help with saving/exporting this HTML widget so that I share it with others. The following is my code:

sun <- sunburst(df, 
         percent=TRUE,count=TRUE,   legend = list(w=150), withD3 = T, width = "100%", height = 400)

htmlwidgets::onRender(
    sun,
    "
    function(el, x) {
    d3.selectAll('.sunburst-legend text').attr('font-size', '10px');
    d3.select(el).select('.sunburst-togglelegend').property('checked',true); // force show the legend, check legend
    d3.select(el).select('.sunburst-togglelegend').on('click')(); // simulate click
    d3.select(el).select('.sunburst-togglelegend').remove() // remove the legend toggle
    }
    ")

Example dataset:

df <- read.csv(system.file("examples/visit-sequences.csv",package="sunburstR"),header = FALSE,stringsAsFactors = FALSE)[1:100,]

Thank you.

1 Answers

With the following code, you can save your graph to HTML and PDF :

library(rmarkdown)
library(pagedown)

vector_Text_RMD <- c('---',
'title: ""',
'output: html_document',
'---',
'```{r setup, include=FALSE}',
'knitr::opts_chunk$set(echo = TRUE)',
'```',
'```{r cars, echo=FALSE}',
'library(sunburstR)',
'df <- read.csv(system.file("examples/visit-sequences.csv",package="sunburstR"),header = FALSE,stringsAsFactors = FALSE)[1:100,]',
'sun <- sunburst(df, percent = TRUE, count = TRUE, legend = list(w = 150), withD3 = T, width = "100%", height = 400)',
'htmlwidgets::onRender(sun,',
"    'function(el, x) {",
'    d3.selectAll(".sunburst-legend text").attr("font-size", "10px");',
'    d3.select(el).select(".sunburst-togglelegend").property("checked",true); // force show the legend, check legend',
'    d3.select(el).select(".sunburst-togglelegend").on("click")(); // simulate click',
'    d3.select(el).select(".sunburst-togglelegend").remove() // remove the legend toggle',
'    }',
"    ')",
'```')


zzfil <- tempfile(fileext = ".Rmd")
writeLines(text = vector_Text_RMD, con = zzfil)

render(input = zzfil,
       output_file = "C:/stackoverflow100.html")

chrome_print("C:/stackoverflow100.html", 
             output = "C:/testpdf100.pdf")
Related