This article wants to provide a brief overview on How to export charts with dw_export_chart() via the Datawrapper-API.
In general there is a bunch of arguments that can be added to a call to Datawrapper’s Export-API. You can check out the documentation here or the reference here.
dw_export_chart() does not yet support all of these options. They are added step by step. Feel free to contribute the argument you’re missing with a pull request on Github.
For the moment, the most important arguments are supported (check the documentation dw_export_chart() for more):
type
unit
mode
width and height in pixelsplain to FALSE
scale of the chart.border_width argument, measured in pixelsborder_color
transparent to TRUE.Use the type = "png"-argument and download the result into an object.
To save the image we are using {magick} which is a requirement for {DatawRappr}. Simply run image_write():
png_chart <- dw_export_chart("CHART_ID", type = "png")
magick::image_write(png_chart, "OUTPUT.png")When loading a PDF with type = "pdf", the data is transfered as a binary. Fir this you can use the builtin writeBin-function:
pdf_chart <- dw_export_chart("CHART_ID", type = "pdf")
writeBin(pdf_chart, con = "OUTPUT.pdf")When using type = "svg" you will retrieve the code for a svg. So saving is as simple as writing text to a file:
svg_chart <- dw_export_chart("CHART_ID", type = "svg")
fileConn <- file("OUTPUT.svg")
writeLines(svg_chart, fileConn)
close(fileConn)