使用dplyr编程-如何处理引号/enquotes
我需要创建一个包装器函数,用dplyr执行数据库调用 首先创建一个可复制的示例:使用dplyr编程-如何处理引号/enquotes,r,dplyr,R,Dplyr,我需要创建一个包装器函数,用dplyr执行数据库调用 首先创建一个可复制的示例: library("DBI") library("dplyr") conn = DBI::dbConnect(RSQLite::SQLite(), path = ":memory:") df = expand.grid(indate = as.character(as.POSIXct(seq(as.Date('2017/06/06'), as.Date('2018/02/12'), by="day"))), nam
library("DBI")
library("dplyr")
conn = DBI::dbConnect(RSQLite::SQLite(), path = ":memory:")
df = expand.grid(indate = as.character(as.POSIXct(seq(as.Date('2017/06/06'), as.Date('2018/02/12'), by="day"))), name = c("Canada","Japan","USA"), stringsAsFactors = FALSE)
copy_to(conn, df, "lineups_country",
temporary = FALSE,
indexes = list(
"indate",
"name"
)
)
这是在没有包装器函数的情况下运行良好的代码:
res = tbl(conn, table)
# filter the country
res = res %>% filter(name %in% c("Canada","Japan"))
# filter the date
res = res %>% filter(indate >= "2018-01-01")
res %>% show_query()
df2=res %>% collect()
unique(df$name);unique(df2$name)
min(df$indate);min(df2$indate)
现在,为了创建包装器函数,我已经阅读了文档
然而,事情对我来说还很不清楚,特别是关于引号/引文
以下是我尝试过的:
myFun = function(conn, table,
dateCol = "indate",
startDate = as.POSIXct("2018-01-01"),
key = list(name = c("Australia","Japan"))) {
on.exit({dbDisconnect(conn)})
res = tbl(conn, table)
res %>% show_query()
# filter the country
countryCol = names(key)
enquo_country <- enquo(countryCol) #enquo_country <- rlang::sym(countryCol) #
res = res %>% filter(!!enquo_country %in% key[[1]])
res %>% show_query()
# filter the date
enquo_dateCol <- enquo(dateCol) #enquo_country <- rlang::sym(names(key)) #
res = res %>% filter(!!enquo_dateCol >= as.character(startDate))
res %>% show_query()
return(res %>% collect())
}
这会产生一个错误:
matchx表中出错,nomatch=0L:“match”需要向量
论据
您需要更改一些内容: 指定一个表,而不是表,它是一个函数; 对通过调用nameskey返回的字符向量使用sym将其转换为quosure; 如果你要使用enquo,不要引用dateCol。如果要引用它,请使用sym。 始终如一地命名起始日期; 将起始日期转换为字符没有特别的意义;不管怎样,它都处理得很好。 图书馆数据库 图书馆弹琴 conn=DBI::dbConnectRSQLite::SQLite,路径=:内存: df=expand.gridindate=as.characteras.POSIXctseqas.Date'2017/06/06',as.Date'2018/02/12',by=day, name=cCanada,日本,美国,stringsAsFactors=FALSE 复制到CONN、df、阵容和国家/地区, 临时=假, 索引=listindate,名称 myFun=功能控制,表格, dateCol=indate, 起始日期=as.POSIXct2018-01-01, key=listname=cAustralia,日本{ on.exit{dbDisconnectconn} res=tblconn,表格 res%>%show\u查询 过滤全国 国家/地区%filter!!国家/地区%in%关键字[[1]] res%>%show\u查询 过滤日期 enquo\u dateCol%过滤器!!enquo\u dateCol>=开始日期 res%>%show\u查询 returnres%>%收集 } 现在: df2 >挑选* >来自“国家队”` > >挑选* >来自“国家队”` >“加拿大”、“日本”中的“姓名”所在位置 > >挑选* >从选择* >来自“国家队”` >“加拿大”、“日本”中的“姓名”所在位置 >其中'indate`>='2018-01-01T05:00:00Z' df2 >一个tibble:82 x 2 >独立名称 > >2018-01-02 19:00:00加拿大 >2018-01-02 19:00:00日本 >3 2018-01-03 19:00:00加拿大 >4 2018-01-03 19:00:00日本 >5 2018-01-04 19:00:00加拿大 >6 2018-01-04 19:00:00日本 >7 2018-01-05 19:00:00加拿大 >8 2018-01-05 19:00:00日本 >9 2018-01-06 19:00:00加拿大 >10 2018-01-06 19:00:00日本 > ... 还有72行 “key”和“dateCol”的名称是字符输入,使用rlang中的sym将其转换为符号进行计算
myFun = function(conn, table,
dateCol = "indate",
startDate = as.POSIXct("2018-01-01"),
key = list(name = c("Australia","Japan"))) {
on.exit({dbDisconnect(conn)})
res = tbl(conn, table)
res %>%
show_query()
# filter the country
countryCol = names(key)
country <- rlang::sym(countryCol)
res <- res %>%
filter(!! (country) %in% key[[1]])
res %>%
show_query()
# filter the date
dateCol <- rlang::sym(dateCol)
res <- res %>%
filter(!! (dateCol) >= startDate)
res %>%
show_query()
return(res %>%
collect())
}
-运行函数
df2 <- myFun(conn,
table = "lineups_country", # the table name
key = list(name = c("Canada", "Japan")),
dateCol = "indate",
startDate = as.POSIXct("2018-01-01"))
#<SQL>
#SELECT *
#FROM `lineups_country`
#<SQL>
#SELECT *
#FROM `lineups_country`
#WHERE (`name` IN ('Canada', 'Japan'))
#<SQL>
#SELECT *
#FROM (SELECT *
#FROM `lineups_country`
#WHERE (`name` IN ('Canada', 'Japan')))
#WHERE (`indate` >= '2017-12-31T18:30:00Z')
head(df2, 5)
# A tibble: 5 x 2
# indate name
# <chr> <chr>
#1 2018-01-01 05:30:00 Canada
#2 2018-01-01 05:30:00 Japan
#3 2018-01-02 05:30:00 Canada
#4 2018-01-02 05:30:00 Japan
#5 2018-01-03 05:30:00 Canada
我们不需要在%key[[1]]中使用%>%filterUQcountry%来避免!!传播到其他的表达?@RockScience我错过了乡村的c。您可以使用UQ或!!。如果你不想传播它,请过滤!!国家%in%key[[1]]它来自哪里?@RockScience没关系。它可能来自深部状态或类似于1984年的状态:=