cicero | R Documentation |
Cicero: some similarities with moustache's idea but designed to be LaTeX friendly, tiny and to allow mixing a bit of R code with templates (if x is a function)
cicero(x, ...)
x |
template with |
... |
data (vectors) passed with the same name as the variable to be filled in tmpl. If missing lookup is made in the calling environment |
strict |
data provided must have all the same length (otherwise recycling will act) |
## Example with character template
tmp <- "Hi my name's <<name>> \textbf{<<surname>>}.
Born on <<birthdate>>. Heigth <<heigth>>, weight <<weight>>."
cicero(x = tmp,
name = "mario",
heigth = 1.78,
surname = "rossi",
birthdate = '2013-10-10',
weight = 80)
## Not run:
cicero(x = tmp,
strict = TRUE,
name = "mario",
heigth = c(1.78, 1.79),
surname = "rossi",
birthdate = '2013-10-10',
weight = 80)
## End(Not run)
## Example with function as x
## a possible application for function argument: choose the
## template in runtime and fill it with proper data.
select <- function(name, surname, height, weight)
{
tmpl1 <- "Hi, I'm <<name>> <<surname>>. My bmi is ok: <<bmi>>."
tmpl2 <- "Hi, I'm <<name>> <<surname>>. My bmi is not ok: <<bmi>>."
bmi <- weight/(height^2)
if (bmi < 25) cicero(x = tmpl1, name = name, surname = surname, bmi = bmi)
else cicero(x = tmpl2, name = name, surname = surname, bmi = bmi)
}
cicero(x = select,
name = "mario", surname = "rossi",
height = 1.78, weight = 82)
cicero(x = select,
name = "mario", surname = "rossi",
height = 1.78, weight = 75)
## cicero handles functions with argument ... as well
f_with_dots <- function(namefirst, ...){
arglist <- list(...)
res1 <- do.call(paste, arglist)
res2 <- do.call(paste, rev(arglist))
if (namefirst) res1 else res2
}
cicero(x = f_with_dots, namefirst = TRUE, name = "mario", surname = "rossi")
cicero(x = f_with_dots, namefirst = FALSE, name = "mario", surname = "rossi")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.