I have the following function, gigl
, where I am trying to capture the variables on the left and right of |
. Currently my code only captures the variables if it is named exactly s
or n
.
How can I generalize the following to evaluate any variable regardless of the name?
gigl <- function(form,data){
s <- rlang::f_lhs(f_lhs(form))
n <- rlang::f_rhs(f_lhs(form))
s <- eval_tidy(data$s) # don't like that I have to use the same name as inputed. Make more general.
n <- eval_tidy(data$n) # don't like that I have to use the same name as inputed. Make more general.
output <- tibble(n,s) %>% data.matrix()
output
}
fit <- gigl(s | n ~ 1 , data=df)
Here is some toy data
library(tidyverse)
df <- tribble(
~n, ~s,
10, 6,
8, 7,
6, 5
)
The following should work as above, but is currently not working
df2 <- tribble(
~total, ~positive,
10, 6,
8, 7,
6, 5
)
fit <- gigl(total | positive ~ 1 , data=df2)
The output should be
total positive
[1,] 10 6
[2,] 8 7
[3,] 6 5