Using bquote to create variable names with dots
functional-programming, r
Solution
I'd likely use `substitute()`:
i <- 4
substitute(XX <- state, list(XX = as.name(paste0("x.", i))))
# x.4 <- state
With `bquote()`, you could do:
with(list(XX=as.name(paste0("x.", i))), bquote(.(XX) <- state))
# x.4 <- state
But in either case you'll need to construct the name from `"x."` and `i`, as that's not something that `bquote()` does.
Problem
In my functional programming I currently use the following code snippet to generate code for a function body. ``` i <- 4 paste("x.", i, " <- state", sep = "") ``` This creates the code `x.4 <- state`. Now, I'd like to change to `bquote()`, but I have no idea to create this code snippet. A ``` i <- 4 bquote(x..(i) <- state) ``` fails, because of the dots. I use the dots to separate higher orders like `x.12.4`. All other delimiters like `_` or `-` are not allowed in variable names. Do you have an idea, or is it impossible with the dots?