In your case, the {{ }}
have no special meaning at all. You would get the same results with
Dear `r paste(contacts$firstname[contacts$state == eachstate], sep = ", ")`,
For example, in R, these are equivalent
a<-1:10
a[a%%2==1]
a[a%%2=={{1}}]
a[a%%2==((1))]
You can add extra parenthesis/braces without changing the meaning of the code in R. So in your case, running
sampleRmd<-'Dear `r paste(contacts$firstname[contacts$state == eachstate], sep = ", ")`'
knit(text=sampleRmd, quiet=T)
would work just the same. Just as the contacts
variable is resolved to your current environment, so too is the eachstate
variable. Using or not using the braces doesn't make a difference. If you directly call the function that parses the inline code blocks, it might be more obvious what's going on
knitr:::parse_only('paste(contacts$firstname[contacts$state == {{eachstate}}], sep = ", ")')
# expression(paste(contacts$firstname[contacts$state == {
# {
# eachstate
# }
# }], sep = ", "))
The catch is that double braces are often used in "template" languages and knitr
does allow you to define such templates. These templates are basically text chunks where you can swap out values in the text with values of variables in your current scope in R. This replacement is done by the knit_expand()
function. Here's a minimal example in your case
sampleRmd<-'Dear `r paste(contacts$firstname[contacts$state == {{eachstate}}], sep = ", ")`'
contacts <- data.frame(
firstname=c("Adam","Becky","Clement","David"),
state=c("MI","CA","CA","MI")
)
eachstate <- "MI"
res <- knit_expand(text=sampleRmd)
res
# [1] "Dear `r paste(contacts$firstname[contacts$state == MI], sep = \", \")`"
Basically it just looks for anything between {{
and }}
and replaces that with the value of the variable of the same name. In this case is replaces {{eachstate}}
with the current value of the eachstate
variable which is "MI"
in this example. This is done before the R code is executed. Note that if you try to knit()
this you will get an error
knit(text=res)
# Error in NextMethod("[") : object 'MI' not found
This is because the expand function has put in the literal value of MI as if it where a character string into the code. It did not quote the value. You would have to change the template to
sampleRmd<-'Dear `r paste(contacts$firstname[contacts$state == "{{eachstate}}"], sep = ", ")`'
res <- knit_expand(text=sampleRmd)
knit(text=res, quiet=T)
# [1] "Dear Adam, David"
But in your example, you are not taking advantage of any of the templating features. You're basically just adding unnecessary symbols to the R expression.