0

I am trying to pass a column name through the bi_class function within my broader create_sf_func() function to use to create tertile cutoffs for bivariate maps in R. I am using the following code, but cannot figure out how to get R to recognize my variable argument of my function as a variable in the dataframe. Any help would be greatly appreciated.

create_sf_func <- function(yrqrt, variable) {
  
  df <- bi_class(df2, x = QRT_TESTS_PER1K, y = df2[[variable]], style = "quantile", dim = 3)
  
  df_filtered <- sf::st_as_sf(df %>% filter(YEAR_QRT == yrqrt) %>% 
                                   right_join(all_fips, by=c("FIPS5"="FIPS5")) %>% 
                                   mutate(geometry = fips_geometry(FIPS5)) %>% filter(FIPS5<60), crs = 4269) %>% 
    shift_geometry()
  
  # Filter the data frame by year using the filter function
  # df_filtered <- filter(df, year == year)
  
  # Return the filtered data frame
  return(df_filtered)
}

df_cases2022Q2 <- create_sf_func("2022Q2", "CASES_PER100K")

This is the error I am getting:

Error in if (var %in% names(.data) == FALSE) { : 
the condition has length > 1
Phil
  • 7,287
  • 3
  • 36
  • 66
Quinterpret
  • 133
  • 6
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. If we can't run the code it's much more difficult to know what's going on. – MrFlick Dec 20 '22 at 19:58
  • 1
    Tricky to know what's going on as @MrFlick has noted. It's likely that `bi_class` is expecting a column name - what you're doing here is passing a whole column as a vector of arguments, rather than the column name. Try switching `df[[variable]]` for `ensym(variable)` and see if that works. – Andy Baxter Dec 20 '22 at 20:19
  • Thanks for the suggestion, I created a new post with a reproductible example here: https://stackoverflow.com/questions/74878535/passing-a-column-name-through-a-function – Quinterpret Dec 21 '22 at 15:50

1 Answers1

0

With {{ }}

func <- function(col) {
  mtcars %>%  
    filter({{col}} == 1)
}

func(carb)

                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Chamkrai
  • 5,912
  • 1
  • 4
  • 14
  • 5
    The OP is passing column names as strings to their function. The `{{ }}` syntax won't work with that. – MrFlick Dec 20 '22 at 19:59
  • Reposted the question with an example here: https://stackoverflow.com/questions/74878535/passing-a-column-name-through-a-function/74878649#74878649 – Quinterpret Dec 21 '22 at 16:16