- 3 Minutes Wednesdays
- Posts
- Building our own glue with R
Building our own glue with R
Remember how I bamboozled Excel users with my cool PDF automation?
If you want to pull that off too, check out my freshly released "Report Automation With Typst" course.
Guten Tag! š
Many greetings from Munich, Germany. Last week, I left you with a shocking cliffhanger. I promised to give you a function that could take arbitrary inputs and assemble that into something we can send to an LLM. And all of that was supposed to work with a glue-like function like this:

And the mean part was that I left you hanging when all we could do was this:

Thatās of course only half of what we wanted. This lets us turn lists, data.frames and other variables into texts but how do we mix that with other text using a function that works like glue(). Well, letās find out today.
How does glue() actually work?
If youāre anything like me, then you really prefer glue() from the {glue} package over built-in functions like paste() or sprintf():

And if you want to go even further, then the {epoxy} package (see old newsletter or YT video) has even more tricks up its sleeve.
Both glue() and epoxy() use so-called transformers under the hood. You see, if you look at the glue() documentation, then youāll see that thereās an argument called .transformer that is set to glue::identity_transformer().

As you can see, it does three things:
It takes input
textand passes it into theparse()functionIt takes the parsed text and passes it into
eval()It does both steps using some error handling that is performed via the
with_glue_error()function.
So basically it does nothing but this:

That is some dark magic that you usually want to avoid. But in this particular scenario the eval-parse-combo cannot be helped.
Piggy-backing off glueās transformer
So as the identity_transformer() function is so nice that performs some error-handling letās use it but also inject our generate_prompt() function into the transformer.

And now we can use this transformer manually:

But what we really want to do is to use it with glue():

Make a function out of this
To round things off we can create a new function that just pre-fills the .transformer argument but exposes all the other arguments from glue():

Prefill with args exposed
So this works pretty nicely, but the function will only have one single argument, namely dot-dot-dot. This means that anything we stick into this function will be passed of to glue(). To see that, you can inspect the argument of a function via the formals() function:

But what if we wanted to expose all the regular glue::glue() arguments and just pre-fill the .transformer argument. Well, you can just create a function that overwrites the formals of the original glue() function:

This function works exactly like purrr::partial():

But this time all the arguments are still there:

From a perspective of functionality, this doesnāt change anything. But from a usability perspective the new function now has better auto-complete:

Excellent! Weāve finished our project for this week. š„³ Hope you enjoyed this weekās newsletter. As always, feel free to hit the reply button to share your thoughts.
Happy Coding and see you next week,
Albert
Whenever youāre ready, there are three I can help you:
Automate Your Data Reports: This course helps data analysts eliminate manual copy-paste reporting by automating PDF reports end-to-end, saving hours every cycle and preventing costly mistakes. (Using the lovely Typst language š)
Generate Insights in Minutes, not Hours: This comprehensive course teaches you to handle data faster, smarter, and more efficiently.
Bespoke Data Science Solutions: Iāve helped clients build their own data science solutions. Whether building custom web apps, PDF reports, AI automations or teaching workshops, Iāve got you covered. You can reach out to me via this form (or simply hit reply to this email)
Reply