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:

  1. It takes input text and passes it into the parse() function

  2. It takes the parsed text and passes it into eval()

  3. 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:

  1. 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 šŸ˜)

  2. Generate Insights in Minutes, not Hours: This comprehensive course teaches you to handle data faster, smarter, and more efficiently.

  3. 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

or to participate.