0

R notebooks allow us to write SQL code directly with a dedicated chunk defined by {sql}. However, that call isn't very useful until we get its output and process it in the next R chunk:

```{sql output.var="data"}
SELECT * FROM foo
```

```{r}
dt = data.table(data)
# do stuff with the data
```

Is there a way to bind such chunks together, so that running one automatically runs the other as well? Even better, can it be one-way (such that running the SQL automatically calls the R block, but running the R block doesn't run the SQL)?

To clarify, I'd like to know if it's possible to do something like

```{sql output.var="data" runNextChunk=TRUE}
SELECT * FROM foo
```

Or perhaps

```{sql output.var="data" runAfter="bar"}
SELECT * FROM foo
```

```{r name="bar"}
dt = data.table(data)
# do stuff with the data
```

Obviously, once the notebook is complete, I can knit it and all the cells will be processed. However, while developing I often find myself making adjustments to my SQL call and then having to move the caret to the next chunk and runing it as well.

I've looked at the R Markdown docs, but haven't found anything like this, so here's to hoping I just missed it.

Wasabi
  • 2,879
  • 3
  • 26
  • 48
  • @Parfait I don't mean to call the cells as if they were functions (i.e. ```{r} callSQLchunk()```). I mean a way to indicate to the notebook interpreter that the cells are bound somehow. I am hoping there's a declaration option like ```{sql runNextChunk=True}``` or maybe ```{sql afterRun="foo"}``` followed by ```{r name="foo"}```). I've looked at the R markdown documentation but haven't found anything, but am just hoping I missed it. – Wasabi Jun 26 '19 at 17:36
  • @Parfait, we seem to be talking past each other. In R notebooks, data from one language chunk is available in R chunks. For example, the SQL chunk in my example above is declared with `{sql output.var="data"}`. This means that the query's result is stored in an R dataframe variable called `data`. That table is then used in my R chunk (in this example, I used `data` to create an R data.table). My only problem is that I need to run the SQL chunk, move the caret to the R chunk, and then run it to analyze that data, when I'd rather have the notebook run the R code whenever I run the SQL code. – Wasabi Jun 26 '19 at 18:26
  • Got it. So the chunks do not run in sequence with SQL chunk preceding R in order? What happens when you knit the document? The R chunk fails with the unknown *data* object? Also, the `sql` chunk runs [DBI API under the hood](https://bookdown.org/yihui/rmarkdown/language-engines.html#sql). – Parfait Jun 26 '19 at 18:29
  • @Parfait correct. The SQL chunk is before the R, and running the SQL chunk does not automatically run the R chunk as well. Knitting works since it runs all chunks in sequence, but that's time-consuming (when compared with just running these two cells) and this question is more about the development phase when I'm running and changing things. – Wasabi Jun 26 '19 at 19:13
  • 1
    Wait. So if you run the SQL chunk first and then run the R chunk next, the R chunk fails? Let me see if I can reproduce your issue on a different machine tonight. That would be a very interesting result since it is a DBI call underneath! – Parfait Jun 26 '19 at 19:15
  • @Parfait, sorry, hadn't seen the edit. No, no, if I run the SQL and then the R chunk, everything works fine. You seem to be reading this as a technical issue, but it's more of a usability one. And what you suggest is exactly what I'm doing right now. It's just that if I modify the SQL call, I can't see how it impacts my analysis without also running the subsequent R chunk. So I just want to know if I can ask the notebook to run the chunks together or if I have to continue what I'm doing: run the SQL, move the caret down to the R chunk, and then run that. – Wasabi Jun 26 '19 at 20:33
  • 1
    My advice is then just run everything in one chunk at least during development. Since the `sql` chunk is a wrapper to DBI `dbGetQuery(conn, "SELECT ...")` call, just run that at beginning of `R` chunk. – Parfait Jun 27 '19 at 14:14

0 Answers0