I got really, really bored this week, so I decided to go back to the R package, tinieR, that I released last year (if you haven’t seen it before, tinieR shrinks image file sizes using the TinyPNG.com API). My idle tinkering ended up with a few cool little additions to the package, so I figured it was time for a new release: v0.4.1!
Why 0.4.1? Because I set it all up to be 0.4.0, but then I wasn’t entirely happy with some things and kept tinkering, and didn’t realise that it wasn’t exactly straightforward to change git release/tag options. So .1 it is…
New things
Here are the main additions to the package:
- Added ability to set global defaults for
tinify()
arguments withtinify_defaults()
. Changes made are printed to the console, and if called without any arguments, prints all current default settings. - Added new
suffix
argument intinify()
to change the “_tiny” suffix applied to tinified file names. - Added new
return_path = "proj"
option toreturn_path
argument intinify()
to return the path to the newly tinified file relative to the project directory, no matter the current working directory.
You can read more in the new package vignette here, and here’s an example of the new defaults options in action:
```{r setup, include=FALSE}
library(tinieR)
tinify_defaults(
suffix = 'resized',
quiet = 'true',
return_path = 'rel',
resize = list(method = 'scale', width = 100)
)
```
Here is my image:
```{r}
knitr::include_graphics(tinify("imgs/example.png"))
```
Useful things I learnt
Aside from being slightly useful, the main reason I work on tinieR is to give me an excuse to poke around in R and learn new things about the language. So what did this latest round of tinkering show me?
tinify_defaults()
really taught me about how to set and retrieve options in R, and in particular, how useful it is that they have adefault
argument to fall back on when not set.- It also taught me more about returning
messages
andwarnings
in R, how to suppress warnings when required, and how to usetryCatch
to collect errors and supply my own or perform a new action. - …And how to deal with storing, setting, and retrieving these default options for use in package tests (using
on.exit()
mostly). - I didn’t end up actually using it (yet), but I also found out about the crayon package for printing coloured text to the R console - fun!
- The new
"proj"
setting forreturn_path
got me familiar with the rprojroot package. I’d used it before for… something that I forget, but it was great to really spend some time poking around in it. It’s a very useful package when you need it! - I wrote my first vignette, and it was interesting to see how the usethis package and my prior experience with Rmarkdown made it exceptionally easy.
- Not directly for the release of 0.4.1, but I also spent some time tinkering with the pkgdown configuration for the package documentation site. Nothing drastic, just a new theme and some moving around of contents, but good to know!
I really recommend developing a package for getting to know R better. It doesn’t have to do much fancy - tinieR certainly doesn’t - but having problems to solve really gives purpose to your learning. If you want to try it, check out the R Packages book by Hadley Wickham and Jenny Bryan for a complete guide!
I hope you’ll try out tinieR for yourself - and if you encounter any issues, please let me know!