You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think I must be missing something obvious here, but I'm not sure what to make of this error. I have a simple function that accepts one argument which is used to filter a df object and pull out the values from a particular column. I had originally written this just using vanilla dplyr, but wanted to see if I could make it more efficient with dtplyr.
The underlying dtplyr code works fine outside the function, but whenever I try to call the function, I get an object not found error. I feel like I must be missing something simple here, any help would be greatly appreciated.
library(reprex)
library(data.table)
library(dtplyr)
library(tidyverse)
data("mtcars")
mtcars<-mtcars %>% rownames_to_column("car_name")
mtcars_lazy<- lazy_dt(mtcars)
special_cars<-mtcars %>%
sample_n(10) %>%
pull(car_name)
# This worksmtcars_lazy %>% filter(car_name==special_cars[1]) %>% as_tibble() %>% pull(mpg)
#> [1] 30.4# Wrapping in function...check_mpg<-function(car){
print(paste("This is a: ",car))
mtcars_lazy %>%
filter(car_name==car) %>% as_tibble() %>% pull(mpg)
}
# Doesn't work
check_mpg(special_cars[1])
#> [1] "This is a: Lotus Europa"#> Error in eval(stub[[3L]], x, enclos): object 'car' not found
The problem is that dtplyr is interpretting car as a column name, which doesn't exist.
You therefore need to use bang notation (!!) to make it clear that car is a variable, not a column.
check_mpg<-function(car){
print(paste("This is a: ",car))
mtcars_lazy %>%
filter(car_name==!!car) %>% as_tibble() %>% pull(mpg)
}
check_mpg(special_cars[1])
#> [1] "This is a: Lincoln Continental"#> [1] 10.4
Now, as to why this problem appears in a function but not in the global environment, I don't know. I even tried modifying the non-function version by replacing car_name == special_cars[1] with car_name == car (having previously defined car), but it still works there.
I think I must be missing something obvious here, but I'm not sure what to make of this error. I have a simple function that accepts one argument which is used to filter a
df
object and pull out the values from a particular column. I had originally written this just using vanilla dplyr, but wanted to see if I could make it more efficient with dtplyr.The underlying
dtplyr
code works fine outside the function, but whenever I try to call the function, I get an object not found error. I feel like I must be missing something simple here, any help would be greatly appreciated.Created on 2020-09-03 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: