I am trying to work out if there is a way to pass parameters to the sql
chunks connected to BigQuery in RMarkdown:
```{r setup}
library(bigrquery)
bq_auth(path = "access_token.json")
db <- dbConnect(
bigquery(),
dataset = 'my_data',
project = 'my-project',
use_legacy_sql = FALSE
)
parameter_value = 10L
```
```{sql, echo=FALSE, connection=db, output.var="x}
SELECT @parameter_value
```
```{r}
print(x)
# I want to see 10 here.
```
See BigQuery parameterised query documentation here - https://cloud.google.com/bigquery/docs/parameterized-queries
Update 1
Though ?parameter_value
injections seems to work fine for scalars, it does not apply to vectors, e.g.:
```{r}
parameter_value = c(10L, 20L)
```
```{sql, echo=FALSE, connection=db, output.var="x}
SELECT UNNEST(?parameter_value)
```
will fail with:
Error in vapply(values, function(x) dbQuoteLiteral(conn, x), character(1)) : values must be length 1, but FUN(X[1]) result is length 2
Event with scalars it is not using BigQuery engine to parameterise the query.
Update 2
I think it will not be possible to do this right now, because some of the DBI APIs are not implemented in bigrquery
package, which I have raised an issue for. And knitr
package here