knitr sql chunk不将数据保存到变量中 [英] knitr sql chunk not saving data into variable

查看:139
本文介绍了knitr sql chunk不将数据保存到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我以交互方式逐个运行所有块时,具有SQL块的My RMarkdown笔记本运行良好,但是当我尝试编写时,SQL块没有将数据保存到指定变量中。当在后面的R块中引用应该使用SQL块生成的数据集时,数据集变量就是空的。



这里是一个例子

  {r setup,include = FALSE,warning = FALSE,message = FALSE} 
#加载必要的库
库(bigrquery )
library(knitr)
library(tidyverse)
$ b $ db
df< - NULL
```


```{sql,connection = db,output.var = df}
select * from example_dataset
limit 10
```
返回数据集

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

我试过这里的解决方案( R:Knitr给出SQL块的错误),但它没有解决我的问题。

解决方案

Just ran进入相同的问题,它看起来像你需要引用你分配的变量。

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

资料来源: http://rmarkdown.rstudio.com/ authoring_knitr_engines.html#sql


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.

解决方案

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

这篇关于knitr sql chunk不将数据保存到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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