I am trying to write a function using tidy eval approach. The code works without using the function but I get error with the function. You can see the code with and without the function.
find_starting_xy <- function(ds, ado, scn){
require(tidyverse)
require(rlang)
scn <- enquo(scn)
# What is the ID of the 1st frame of External Driver in the scenario?
first_frame <- ds %>%
filter(file.ID== !! scn) %>%
pull(frames) %>%
head(.,1)
# Starting xy locations of ados in this scenario:
ado %>%
filter(file.ID== !! scn) %>%
select(ADO_name, frames, pos.2.m, pos.1.m, lane) %>%
group_by(ADO_name) %>%
do(data.frame(head(., 1))) %>%
ungroup() %>%
mutate(x.m = pos.2.m + 9857.24,
y.m = pos.1.m - 84.73 + 456.59) %>%
select(-c(pos.2.m, pos.1.m)) %>%
mutate(frames2 = frames - first_frame,
Time_sec = round((frames2/60),0)) %>%
select(-c(frames2, frames))
}
> find_starting_xy(ds, ado, Cars_20160601_01.hdf5)
Show Traceback
Rerun with Debug
Error in filter_impl(.data, quo) :
Evaluation error: object 'Cars_20160601_01.hdf5' not found.
Can you please turn this into a reprex (short for minimal reproducible example)? It's much easier to discuss code with code, and the same contents, etc.
(Essentially, it's impossible for me to say without knowing what your data data look like.)
As @mara said reprex would be useful, but in the meantime I'm not sure why you need tidy eval in this case.
From your second example the only time you use scn variable is in filter, correct? Then why do you want to pass it as a bare variable? In other words, what exact problem would you have if you pass scn as a string?
If you do pass it as a string, then you don't need to use enquo and !! at all, just write filter(file.ID == scn) and that's it. This way you can change where your data is coming from quite easily.