Using bquote to create dotted variable names

In my functional programming, I am currently using the following code snippet to generate code for the function body.

i <- 4 paste("x.", i, " <- state", sep = "") 

This creates the code x.4 <- state . Now I would like to switch to bquote() , but I am not going to create this piece of code. A

 i <- 4 bquote(x..(i) <- state) 

fails due to points.

I use dots to highlight higher orders, for example x.12.4 . All other delimiters, such as _ or - , are not allowed in variable names.

Do you have an idea, or is it impossible with dots?

+4
source share
1 answer

I would use substitute() :

 i <- 4 substitute(XX <- state, list(XX = as.name(paste0("x.", i)))) # x.4 <- state 

With bquote() you can do:

 with(list(XX=as.name(paste0("x.", i))), bquote(.(XX) <- state)) # x.4 <- state 

But in any case, you will need to create a name from "x." and i , as that is not what bquote() does.

+3
source

All Articles