- 3 Minutes Wednesdays
- Posts
- No more messed up ggplots, no more duplicated Shiny code
No more messed up ggplots, no more duplicated Shiny code
Guten Tag!
Many greetings from Ulm, Germany. It's time for another round of tidbits about R, statistics, dataviz and Shiny. Today's content falls into the latter two categories.
Export your ggplots without creating a mess
There was a time when exporting ggplots was hard for me. What happened every time was this.
I've put in a lot of effort to create a new viz. Took care of the colors, added some annotations and fixed little things here and there. I worked on my viz until it looked "good enough" in RStudio's plots window.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/9ed3a2ab-1d78-4218-a9d8-29a7bd2aefa4/my_plot_rescaled.png)
But then I called ggsave() and things turned into a mess.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/44292975-340a-44d3-af57-4931d9544f8f/my_plot.png)
Well, shit. This doesn't look right. Soon, I found myself repeatedly calling ggsave() using different height and width arguments. This turned into a stupid cycle of
Calling ggsave()
Opening the file and checking the result
Being pissed that it still doesn't look right
Luckily, I've discovered the {camcorder} package. This package automatically exports all plots as image files and displays them in RStudio's viewer window. That doesn't sound like much but it's huge! This way, I could escape the above cycle from Hell.
Now, I have no problems with exporting plots anymore. And to celebrate that achievement, I've put together a super short video. It shows you exactly how {camcorder} works. Check it out.
Avoid code duplication with Shiny modules
Generally, code duplication is bad. It's tempting to just copy a code chunk, do some small changes and then go your merry way. But more often than not, this "quick" hack will end up costing you more time. And don't let me get started on how error-prone this is.
And when you're writing Shiny code, code duplication is a special kind of torture. That's because all IDs (like for UI elements) need to be unique in Shiny. So when you duplicate code, you often end up appending some tag or number to your initial IDs.
All of a sudden you have IDs like "select_var_penguin", "select_var_iris", "select_var_mpg". From painful experience I know that this is bad.
Thankfully, Shiny modules avoid this mess. And two weeks ago I promised you a demo video of how they work. Now, the deed is done and you can watch the results on YouTube.
Recommended Resources
Slidecraft 101 by Emil Hvitfeldt: Emil continues his excellent series about Quarto slides. This time he talks about styling code and its output. The blog post is really detailed and it's also a fun way to learn some (S)CSS.
Slide annotation by Emil Hvitfeldt: Emil has created a super flashy and totally awesome Quarto extension. It lets you use animations in slides. Check it out here.
Best practices for functions by Cosima Meyer: Cosima has taken over the "We are R-Ladies" Twitter account this week. She shared a great list of tips to keep in mind when creating functions. Definitely worth a read.
That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.
Enjoy the rest of your day!Albert
Reply