I love writing in rmarkdown. What I love most about it is the ability to have a one-stop-shop for generating text/R-code/output.

That works well and good for most of everything I do, except for when I’m trying to show users how to use JASP. I have found it extremely tedious to write about how to use the JASP interfact, make a screencapture, save the screencapture to the appropriate project folder, search how to insert an image in rmarkdown, then finally type the image path.

But, oops, I misspelled the image name, so now I have to do that weird kinda sorta double-clicky thingy in mac to highlight the name of the file, then copy, then paste. But, oops! There are 100 images in there and I accidentally selected the wrong one.

So now I have to search through all those images to find the right one.

Yeah, it’s tedious and frustrating.

For this reason, I decided that my stats book would be R, then maybe I could tackle a JASP version.

That was, of course, until I read a blog post from Andrew Heiss1 about converting plain-old text into markdown-formatted text. To do so, he used Apple’s Automator.


I’d heard of Automator, but never got around to seeing how it would benefit me.

“I’m paid for the grand prowess of my enormous brain,” I had thought. “I’m so important none of my job requires automation.”

Oh how wrong I was.

After Andrew’s post, and knowing his brain’s way more prowessy than mine, I figured I ought to dive into it. Oddly enough, I ended up learning to automate through Keyboard Maestro. (Don’t ask me why I went with that). But, I’m sure the same can be done in automator, with a few modifications.

The basic idea

Here’s what I envisioned I could do:

  1. Change the default screenshot directory to the directory where my rmarkdown file is stored.
  2. Take a screenshot
  3. Copy the file name of the screenshot I just made to my clipboard, along with the proper rmarkdown tag.
  4. Paste the proper code to my markdown document.

Care to see an example?

 

It seemed to me that I needed to come up with two macros: one that would easily allow me to specify a folder to dump my screenshots, that way I can use relative references in rmarkdown. The second macro would then search that folder for the newest screenshot

That was easy enough to do with scripting:

To make it easier to copy and paste, here’s the applescript text:

And here’s the terminal commands

Not too bad, eh?

Now the second macro needs to be able to identify the most recent screencapture, copy its relative file path, then paste it (with proper syntax) into R Studio.

Here’s the macro:

And voila! It works beautifully.

So now, all I have to do is

  1. Type ::screenshot to specify the directory (do once per project)
  2. Make a screenshot as you normally would (command-shift-4)
  3. Wait 5 seconds (it takes some time for the OS to dump the screencapture in the folder)
  4. Type option-4 to paste the rmarkdown into R studio

I have a feeling this will save me hundreds of hours.

  1. Interesting anecdote: Andrew and I met in 2007 when we were both undergraduates at Brigham Young University. We’ve been Facebook friends since, but more like casual acquaintances. Only about a year ago did I notice that (a) Andrew’s a pretty big deal in his circles, and (b) he and I share very similar academic interests.

Leave a Reply

Your email address will not be published. Required fields are marked *