Assigning a keyboard shortcut for a RMarkdown template

I've created a basic RMarkdown template in an internal custom themes package. The template shows up as expected in RStudio (currently running build 1.2.907). I'd love to assign a keyboard shortcut to open a new file based on this template, akin to the custom keyboard shortcut that can be assigned to the "New R Notebook" command. There doesn't seem to be a way to access any of the template's for shortcuts, short of creating a custom add in and assigning a shortcut to the add in.

That's doable, but is more work than I really wanted to take on. :stuck_out_tongue: Is there a more direct way of accomplishing this end (having a keyboard shortcut that creates a new notebook based on a package supplied template)?

You could create a snippet to insert your template in a script. Snippets are wonderful :slight_smile: I use them everywhere for everything :stuck_out_tongue:

That's an interesting idea!

Reading through, I'm not sure this gets me where I need to go - which is to allow R users across my org to install our mycothemes package and have access to our ggplot theme, fonts, color palette, and RMarkdown templates. Specifically, how would a snippet call a package installed Markdown template? If that was overcome, there would still be the issue of distributing files into every user's .R/snippets, right?

Appreciate the thought! Please let me know if I'm overlooking features in snippets for this use case!

you can also use rsam to create adhoc RStudio shortcuts, that can be session persistent too.

This part sounds like a regular package that you could distribute in a few different ways, see e.g.

The second part, of creating a template and a shortcut could be accomplished by making an RStudio addin, which could be part of your package or separate, it's just a matter of preference. You can assign keyboard shortcuts to addins, which would, in effect, allow you to have a shortcut for your template (I think).

The closest thing that comes to mind is the New Post option in blogdown, which you can select from the addins dropdown (and, if so desired, add a keyboard shortcut to open it).

It looks like the code for that is here: blogdown/inst/scripts/new_post.R at main · rstudio/blogdown · GitHub

It opens a little shiny gadget in which you enter the post title, etc., and then (once you've pressed ok) opens up the appropriate document:

Hopefully some of these resources can get you started! I've never made an addin, but there are lots of good examples in this thread, as well as a webinar which I've also linked to, below.


Thanks, Mara!

I already have the internal package created and distribution squared away via the nifty ghenter ( package. The details on how to write the function for the addin were what I was missing. These lines ( from blogdown look to be the magic I need. Thanks for the pointer!

A limitation of this method is that it forces a different user experience than the standard RStudio new R Script/R Markdown workflows by requiring a file to be created on disk with either an add-in or user specified name. The native RStudio New... experience creates an unsaved Untitled file and leaves the location and name of the file up to the user. One may argue that not permitting unsaved Untitled files is a feature and not a bug (:stuck_out_tongue_winking_eye:) but given the threads that keep occurring on changing the current RMarkdown cars based template, I'm surprised allowing organizations to roll out their own standard look and feel for analysis and reports is so difficult.

Really, all I needed was the ability to (manually) specify a shortcut to a specific RMarkdown template that already appears in the RStudio window. :wink: This works though!


Yeah, I hear you. By making a template, you can select it when you go File >> New R Markdown, but (to my knowledge) there isn't a shortcut for that. You can feel free to submit this as a feature request (FR) for the IDE

I'd recommend keeping the FR to the more general use-case of creating a new RMarkdown file, since it's apt to be helpful to more people.

It's also possible that you could write an addin such that it would create an unsaved unnamed file. (Again, I have no experience there). Yihui's add-in was just a possible starting point.

Definitely keep us updated if you figure something out, I for one would love to know if this was possible!

3 Likes is the feature request issue, should anyone want to emoji +1 that item!