Using eval = F in sql code block in RMarkdown

Hi
I am trying to add sql code in a RMarkdown but I don't want to evaluate it. However, this doesn't seem to work:

```{sql, eval = F, connection = con}
SELECT "device", "referrer" 
FROM trade
WHERE "purchase" == "true"

Any idea how to do this properly?
Thanks
Renger

This small example works ok for me: When I render to HTML, the SQL chunk is not evaluate

---
title: test
output: html_document
---


```{r}
library(DBI)
# Create an ephemeral in-memory RSQLite database
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
dbListTables(con)
```

```{sql, connection = con, eval = FALSE}
SELECT * FROM mtcars WHERE cyl = 4
```

What happens if you leave out the first chunk where con is defined? (I think that is the problem)

I can't really leave it out because I need to define the con object that defines the connection to the database in the Rmd otherwise this won't work when I will render.

So the eval = F still needs to know "con". The chunk evaluates "con" and then sets eval = F for the query.

Oh you just want to show SQL code in your document with highlight ?

In that case, you can indeed omit the con - this works for me.

---
title: test
output: html_document
---

```{sql, eval = FALSE}
SELECT * FROM mtcars WHERE cyl = 4
```

but you won't be able to switch to TRUE as it will error. So this is not much different than just highlighting this chunk in markdown

---
title: test
output: html_document
---

```sql
SELECT * FROM mtcars WHERE cyl = 4
```

Is this what you are look for ?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.