Learn R Programming

whisker (version 0.4.1)

whisker-package: Mustache for R

Description

Whisker is a templating engine for R conforming to the Mustache specification. Mustache is a logicless templating language, meaning that no programming source code can be used in your templates. This may seem very limited, but Mustache is nonetheless powerful and has the advantage of being able to be used unaltered in many programming languages. For example it make it very easy to write a web application in R using Mustache templates and where the browser can template using javascript's "Mustache.js"

Arguments

Details

Mustache (and therefore whisker) takes a simple but different approach to templating compared to most templating engines. Most templating libraries for example Sweave and brew allow the user to mix programming code and text throughout the template. This is powerful, but ties a template directly to a programming language. Furthermore that approach makes it difficult to seperate programming code from templating code.

Whisker on the other hand, takes a Mustache template and uses the variables of the current environment (or the supplied list) to fill in the variables.

Examples

Run this code
template <- 
'Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}'

data <- list( name = "Chris"
            , value = 10000
            , taxed_value = 10000 - (10000 * 0.4)
            , in_ca = TRUE
            )

whisker.render(template, data)




base <-
'Names
{{#names}}
  {{> user}}
{{/names}}'

user <- '{{name}}'

names <- list(list(name="Alice"), list(name="Bob"))

whisker.render(base, partials=list(user=user))

Run the code above in your browser using DataLab