I wanted to cache the result of a SQL chunk as well as its output.var
, but I couldn't. Knitting the .Rmd like bellow ends up with an error object 'select_1_result' not found
at the second run:
---
title: "sql chunk"
output: html_document
---
```{r setup, include=FALSE}
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
knitr::opts_chunk$set(echo = TRUE, connection = "con")
```
```{sql select_1, output.var="select_1_result", cache=TRUE}
SELECT 1
```
```{r print_result, error=TRUE}
knitr::kable(select_1_result)
```
It's strange. I guess this had been possible in the past, at least at the moment of this SO was answered:
Now I use a work around of using an external file and it's just fine, but am I wrong at some point?
```{sql select_1, output.var="select_1_result", eval=!file.exists("/path/to/cache_file")}
SELECT 1
```
```{r write_feather, eval=!file.exists("/path/to/cache_file")}
feather::write_feather(select_1_result, "/path/to/cache_file")
```
```{r read_feather, eval=!exists("select_1_result")}
select_1_result <- feather::read_feather("/path/to/cache_file")
```