knitr sql chunk not saving data into variable

感情迁移 提交于 2019-12-10 21:53:40

问题


My RMarkdown notebook with a SQL chunk runs fine when I run all the chunks one by one interactively, but when I try to knit, the SQL chunk does not have save the data into the specified variable. When the dataset that was supposed to be generated using the SQL chunk is referenced in later R chunks, the dataset variable is simply empty.

Here's an example

{r setup, include=FALSE, warning=FALSE, message=FALSE}
# load necessary libraries
library(bigrquery)
library(knitr)
library(tidyverse)

db <- dbConnect(dbi_driver(), dataset = 'sandbox', project = 'project_id', use_legacy_sql = FALSE)

df <- NULL
```


```{sql, connection=db, output.var=df}
select * from example_dataset
limit 10
```
returns dataset


```{r}
head(df)
```
NULL

I've tried the solution here (R: Knitr gives error for SQL-chunk), but it didn't solve my problem.


回答1:


Just ran into the same problem and it looks like you need to quote the variable you are assigning.

```{sql, connection=db, output.var="df"}
select * from example_dataset
limit 10
```

Source: http://rmarkdown.rstudio.com/authoring_knitr_engines.html#sql



来源:https://stackoverflow.com/questions/45556677/knitr-sql-chunk-not-saving-data-into-variable

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