fmmattioni / downloadthis

Implement Download Buttons in rmarkdown 💾
https://fmmattioni.github.io/downloadthis/
Other
147 stars 5 forks source link

Use methods: add ggplot #9

Closed JohnCoene closed 4 years ago

JohnCoene commented 4 years ago

So here's my interpretation of things, use methods to provide the user a simple interface: a single function that somewhat intelligently produces the ideal download output given the input data structure.

library(ggplot2)
library(downloadthis)

plot <- ggplot(cars, aes(speed, dist)) +
  geom_point()

# ggplot2 default to png
download_this(plot)

# data.frame default csv
download_this(cars)

# list default to rds
lst <- apply(cars, 1, as.list)
download_this(lst)

Most of the checks (if statement) you make in download_this is on the combination of .data and output_extension: methods are just great for that, the entire expression is based on .data so not more tests needed.

Moreover it opens the door to supporting many more data structures. One could think of adding a method for spatial features that downloads GeoJSON or ts time series downlaoded as CSV.

fmmattioni commented 4 years ago

Hi John!

This looks truly fantastic!

Thank you very much for helping to improve this!