Are R snippets simple text templates?

Viewed 60

something unexpected happened when I used a code snippet in R Studio. I was editing a .rmd file as part of a bookdown site.

I created the Snippet:

snippet tweet
    `r blogdown::shortcode('tweet', 'tweet code')`

but when I used that snippet I got:

{{% tweet "tweet code" %}}

This is actually pretty cool, since it does work as intended and it's a cleaner syntax. I just want to know what caused this to happen. I had assumed snippets were a simple copy and paste type operation.

1 Answers

The answer seems to be:

No, snippets in RStudio are not simply text macros they can also execute R code.

For example the default snippet ts:

snippet ts
  `r paste("#", date(), "------------------------------\n")`

executes the included paste function to insert a timestamp comment.

Related