using non-standard evaluation with formula Bis

73 Views Asked by At

Non-Standard evaluation in R. I want to send a formula to a function that uses lm.

I have a data frame with one response: y and many predictors. I will fit a model inside a function. The function will receive a filtering criteria as a string and the name of the predictor variable as a string. The response will enter as a name. The function will filter on the filter criteria, then fit the a model using the predictor variable that was sent to it as a string. I can't get the predictor string to work correctly.

This is very close to using non-standard evaluation with formula. In fact I illustrate that solution, which gets me part of the way there. Difference: I want to send a string with the value of my predictor instead of sending the predictor to the function.

Use Case: Eventually I will put this in a shiny ap and let the user select the predictor and response as well as the filter.

Here is what works:

# create a data frame.
n <- 100
levels_1 <- sample(c("a","b","c"),n,replace=TRUE)
levels_2 <- sample(c("a","b","c"),n,replace=TRUE)
d <-tibble(l_1 = levels_1 ,l_2 = levels_2, y = rnorm(n))

# A function that works
my_lm <- function(d,predictor,response,filter_criteria){
  d1 <- d %>% filter(l_2 == 'a')
  lm(y ~ l_1,data=d1)
}
my_lm(d,l_1,y,'a')

my_lm2 <- function(d,predictor,response,filter_criteria){
  enquo_predictor <- enquo(predictor)
  enquo_response <- enquo(response)
  enquo_filter_criteria <- enquo(filter_criteria)
  d1 <- d %>% filter(l_2 == !!filter_criteria)
  form <- as.formula(paste(enquo_response, " ~ ", predictor)[2])
  # form <- as.formula(paste(enquo_response, " ~ ", enquo_predictor)[2])  wrong way to do it.
  lm(form,data=d1)
  #lm(!!enqu_preditor ~ !!enquo_response,data=d1)
}

selected_var <- names(d)[1]
selected_var
filter_value <- 'a'
my_lm2(d,l_1,y,filter_value)  # This works but is not what I want.
my_lm2(d,selected_var,y,filter_value)  # This does not work but is what I want to work.
0

There are 0 best solutions below