Understanding snippets: processes and programmatically execute snippet?

For the past few hours I've been searching R source code, manuals and fora to no avail.

Suppose I have a snippet called mysnippet in a R.snippets file that has not yet been refreshed.

snippet mysnippet
	${1:var}("hello world")

note: snippets are not available until the save button is clicked (Tools > Global Options > Code > Edit Snippets > Save). This save button (I assume) inserts into, and reads the R.snippets file.

Question

Where can we learn about how snippets are processed internally, and in the R start-up process?
That is, where does snippets-related code live in the R source code or the R manual?

What I want to find out:

How does one programmatically execute or prepare a snippet?

One of the following would constitute a solution:

1.

Directly executing a snippet from file (without refreshing it) or snippet syntax with a call at the command line.

example: inserting snippet in the console

{code here?}  ${1:var}("hello world")

#OR

{code here?} mysnippet

2

Programmatically refreshing the list of available snippets, without exiting RStudio.
Meaning, to programmatically hit the Tools > Global Options > Code > Edit Snippets > Save button.

A clue or the solution might be the process in the R startup process that does this

not a solution:
exiting and reopening R studio.

/home/your_user/.R/snippets/r.snippets

is the default location on my Ubuntu laptop. So programmatically adding a snippet would simply involve appending a well-formed snippet to the end of the file; however, RStudio must cache them because renaming the snippet in the file has no effect without the exit hassle. As far as I can tell, snippets are a feature of RStudio, not R itself.

1 Like

Inserting a snippet into the R.snippets file is not the issue, there is a very nice package snippr by David Robinson for that. The point is finding out how R internally recognizes when a new snippet is added, or some other way to execute a snippet directly (R has to recognize it as a snippet in some way right...?)

By the way, oddly enough the default location of r snippets in Ubuntu machines is not mentioned in R snippet support page, while others are (bottom of the page)

1 Like

Thanks for the pkg. Again, I'm pretty sure this is not an R issue, but an RStudio issue. If you go to an R console from either a system terminal app or from the terminal pane, snippets recognized in the source pane are definitely not.

1 Like

You are right, it seems that RStudio Github may hold the answer, that's progress!

1 Like

People want dynamic snippet features.

After many hours, dozens of githubs, C++, JSON and TextMate crash course and a well-timed comment by @technocrat pointing in the right direction I found a (temporary) solution. Naturally, it turns out to be rather simple yet obscure and undocumented and conflicting messages all around.

Here's tidymath and dynamic snippets combined.
Function translates the text string to tidyverse pipe based on letters and "interaction terms". With some regex it is translated to a snippet with position indications, which then gets inserted into a R.snippets file.

dynamicsnippet

Currently there are still some quirks. Have to run the function twice for some reason, and it doesn't yet look pretty, but it's a proof of concept :sweat_smile:

Here's the code that ultimately refreshes R.snippet files programmatically without closing RStudio (the screens that pop up and close in the gif).

usethis::edit_rstudio_snippets(type = "r")
rstudioapi::documentSave()
Sys.sleep(.5) # for some reason this is needed
rstudioapi::documentClose()

While traversing the abyss that is snippetology, I came across a nice feature in VSCode. VSCode inline snippets seem a near 1:1 translation with the *_bindings.JSON in R.Studio install directory (AppData\Roaming\Rstudio\keybindings on my machine). With rstudioapi::insertSnippet shortcut, and JSON files, I think inline snippets (in editor or in console) should be doable too, but have not yet succeeded at that (i imagine it'd bypass the file popping up, looking much nicer).

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.