Creation of R formula with interpolated Julia Symbol (RCall)



I have Julia Symbols with the name of the columns and I want to interpolate those to R (using RCall) to run a R’s lm(). I found and alternative using R’s as.formula to construct the formula from a string. But I was waiting Julia Symbols to be interpolated to R lm(data=$df, $(y) ~ $(x)) :

R> fit <- lm(data=$df, actual_col ~ global_kl) # OK

R> fit <- lm(data=$df, $(y) ~ $(x)) # NOT OK
Error in model.frame.default(formula = `#JL`$y ~ `#JL`$x, data = `#JL`$df,  : 
  object is not a matrix

R> fit <- lm(data=$df, $(string(y)) ~ $(string(x))) # NOT OK
Warning in model.response(mf, "numeric") : NAs introduced by coercion
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

R> fit <- lm(data=$df, as.formula($(string(y) * " ~ " * string(x)))) # Alternative

Is this behaviour correct?


I think this has to do with how R looks up variables specified in a formula. You can always do:

R> fit <- lm($(df[y]) ~ $(df[x]))