Search code examples
rrstudiocode-snippets

Why does the part after `$` disappear when an RStudio snippet is entered?


I created the following snippet:

snippet setttwd
    setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

enter image description here

But when I type setttwd and press enter, I get the following piece of code:

enter image description here

Note that $path has disappeared.

Why?


Solution

  • Because $ is used as a special character to denote where the cursor should jump after completing each section of a snippet, in order to insert a literal $ it must be escaped as \$.

    https://rstudio.github.io/rstudio-extensions/rstudio_snippets.html#customizing-snippets