emehex
emehex

Reputation: 10538

Cache SQL chunk with R Markdown / Notebook without Knitting in RStudio

I have a chunk of SQL in my R Markdown / Notebook Document:

```{sql output.var = "df"}
SELECT * FROM FakeData
WHERE Date >= '2017-01-01
```

It takes literally 5 minutes to run. Is there an easy way to cache the result of the query without Knitting the document or writing the file to a CSV.

I'd perhaps like the cache to live for a couple of hours, or maybe a day (is there a way to adjust this as well?)

Upvotes: 2

Views: 1144

Answers (1)

sconfluentus
sconfluentus

Reputation: 4993

If you put cache=TRUE in the chunk options and you are working in rStudio, you can select a section of code and run it directly using the green arrows in the upper right of the rMarkdown/knitr console.

{sql output.var = "df", cache=TRUE} SELECT * FROM FakeData WHERE Date >= '2017-01-01

Also, I tend to run a regular R script in another window with EVERYTHING I am going to use in knitR. I find that I have less issues with package availability and caching if the data is stored in the global environment.

If you do it this way, and run with cache=TRUE you will definitely be able to save the data on the first run and skip the waiting next time around.

Upvotes: 4

Related Questions