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

▼魔方 西西 提交于 2020-01-02 19:13:13

问题


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?)


回答1:


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.



来源:https://stackoverflow.com/questions/42284274/cache-sql-chunk-with-r-markdown-notebook-without-knitting-in-rstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!