使用R Markdown/Notebook缓存SQL块而无需在RStudio中进行编织 [英] Cache SQL chunk with R Markdown / Notebook without Knitting in RStudio

查看:98
本文介绍了使用R Markdown/Notebook缓存SQL块而无需在RStudio中进行编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的R Markdown/笔记本文档中有很多SQL:

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

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

实际上需要5分钟才能运行.有没有一种简便的方法来缓存查询结果,而无需编织文档或将文件写入CSV.

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

推荐答案

如果在块选项中放入cache=TRUE,并且您正在rStudio中工作,则可以选择一段代码并使用中的绿色箭头直接运行它rMarkdown/knitr控制台的右上角.

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

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

此外,我倾向于在knitR中使用的所有内容的另一个窗口中运行常规R脚本.我发现如果数据存储在全局环境中,则软件包可用性和缓存方面的问题会更少.

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.

如果您这样做,并以cache = TRUE进行运行,那么您肯定可以在第一次运行时保存数据,而无需等待下一次等待.

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.

这篇关于使用R Markdown/Notebook缓存SQL块而无需在RStudio中进行编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆