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.
Without a function:
ff <- ds %>%
filter(file.ID=="Cars_20160601_01.hdf5") %>%
pull(frames) %>%
head(.,1)
ado %>%
filter(file.ID=="Cars_20160601_01.hdf5") %>%
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 - ff,
Time_sec = round((frames2/60),0)) %>%
select(-c(frames2, frames))
# A tibble: 13 x 5
ADO_name lane x.m y.m Time_sec
<chr> <chr> <dbl> <dbl> <dbl>
1 BMWC10 left 5694 458 248
2 ditiExpeon6 right 1735 455 12.0
3 F150C12 right 5856 247 248
4 Ford1 left 1477 458 12.0
5 llacCadi3 right 1193 455 12.0
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.
Please guide me what I'm doing wrong here?