Jason Becker recently noticed that wrapr::let
works well in binding R
knitr
worksheet parameters (link) (and the same likely holds for shiny
). This isn't something you want to expose to end-users, but is very powerful in managing your own reproducible research.
The idea is: knitr
already takes a params
block which is an arbitrary yaml
object and wrapr::let
is willing to treat string to string associations as name rebindings. If we just add the convention that all uppercase names are to be read as re-bindings (and not values) we can accept user specified function names and variable name directly from the RMarkdown controls.
For examine in the RMarkdown document that produced this note we included in the header the following params:
---
params:
FN: sin
VAR: east
TITLE: "User chosen function plot"
---
The user assignments are available as variable name and function substitutions. We are using the convention that name bindings are specified in all caps (an option of replyr::restrictToNameAssignments
available in version 0.2.01
and newer).
library("wrapr")
print(params)
## $FN
## [1] "sin"
##
## $VAR
## [1] "east"
##
## $TITLE
## [1] "User chosen function plot"
east = 7
let(
alias=restrictToNameAssignments(params),
expr={
print(paste(quote(VAR),VAR))
plot(FN(0.1*(1:20)))
title(params$TITLE)
})
## [1] "east 7"
Or, assuming everything you do is only standard evaluation and you don't care about capturing variable names: you can capture references to values once and have all subsequent blocks use those values.
FN <- let(restrictToNameAssignments(params), FN)
plot(FN(0.1*(1:20)))
title(params$title)
Notice the plot uses the user specified function, but does not know its original name (so can no longer print it in the y-axis).
wrapr::let
takes knitr
parameters one step further. The source for this note can be found here and the rendered output here.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.