library(knitr) # eval is false so that we're not messing with the installer's snippets opts_chunk$set(eval = FALSE)
While most users will probably need snippr
only to install published snippets, it provides tools for working directly with .snippets
files and text.
Suppose we write a snippet for creating an S3 generic:
snip <- '${1:funcname} <- function(${2:args}, ...) UseMethod("${1:funcname}")'
We can now save that function to our .snippets
file:
library(snippr) snippet_add("S3", snip)
If you restart RStudio you'll reload the snippets, after which you can access the snippet by typing S3
then hitting Tab. You can replace an existing snippet simply by re-running snippet_add
. For example, if you want to add curly brackets to the above snippet, do:
snip <- '${1:funcname} <- function(${2:args}, ...) { UseMethod("${1:funcname}") }' snippet_add("S3", snip)
You can remove the snippet with:
snippet_remove("S3")
To get a list of the current snippets, do:
snippets <- snippets_get() head(snippets)
Or to retrieve a single one:
snippet_apply <- snippets_get("apply")
You can retrieve snippets for other languages with the language
argument:
html_snippets <- snippets_get(language = "html") head(html_snippets)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.