Ankit Dhingra
Ankit Dhingra

Reputation: 7004

Replacing variable without quotes in R Markdown SQL Chunk

I am using a sql chunk chunk and using ?parameter in my query to substitute variables like:

```{sql}
Select * from some_table where some_column = ?parameter 
```

However, I want to be able to use the parameter to template a query instead eg:

```{r}
template_query <- "with template_table AS (Select * from another_table)"
```

```{sql}
?template_query

Select * from template_table
``` 

However, the template_query gets replaced as a complete string instead. How do I get this to work?

Upvotes: 2

Views: 194

Answers (1)

Victor Mayrink
Victor Mayrink

Reputation: 1151

I had a similar issue. The function glue_sql from tidyverse's glue package solved my problem:

```{r}
template_query <- glue_sql("with template_table AS (Select * from another_table)")
```

```{sql}
?template_query

Select * from template_table
``` 

Upvotes: 2

Related Questions