Måske er dette en løsning for dig, ved at bruge glue_sql, det virker på min computer
https://community.rstudio .com/t/using-multiple-r-variables-in-sql-chunk/2940/13
Rmd bidder:
library(DBI)
library(glue)
library(RSQLite)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars, overwrite = TRUE)
cyl_int <- c(4L, 6L)
cyl_sql <- glue_sql("{cyl_int*}")
SELECT * FROM mtcars
WHERE cyl IN (?cyl_sql)
LIMIT 3
For en datokolonne skal du muligvis formatere den korrekt i henhold til din database eller bruge to_date(etc, '%d%...')
Her i sqlite er det nemt med ISO-8601-datoer, et eksempel:
library(DBI)
library(glue)
library(RSQLite)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
mtcars$DATE_1 <- seq(from = Sys.Date() - 15, to = Sys.Date() + nrow(mtcars) -16, by = 1) %>% as.character()
dbWriteTable(con, "mtcars", mtcars, overwrite = TRUE)
cyl_int <- c(4L, 6L)
cyl_sql <- glue_sql("{cyl_int*}")
d_date_sql <- glue_sql("{Sys.Date()*}", .con = con)
Bemærk, at du skal angive .con parameter for karakterparametre, som sagt her
SELECT cyl, mpg, DATE_1 FROM mtcars
WHERE DATE_1 >= ?d_date_sql
ORDER BY DATE_1