根据the docs,我可以在RMarkdown的SQL块中使用(简单的)R表达式,如下所示:

```{r}
subjects = 10
```

```{sql, connection=db}
SELECT * FROM trials WHERE subjects >= ?subjects
```

然而,在呈现的文档中,我希望看到实际值subjects,但它从字面上显示为键入的值,即

SELECT * FROM trials WHERE subjects >= ?subjects

您知道如何将SQL区块中的?subjects替换为实际值吗,即我希望看到

SELECT * FROM trials WHERE subjects >= 10

在我的渲染输出中.

推荐答案

knitr version 1.16开始,您可以使用选项sql.show_interpolated = TRUE来 Select 加入此行为.它已经被添加到这个PR中.

请从以下现有问题中查看此示例:

---
title: "Get SQL with evaluated variables from SQL chunks"
output: html_document
---
  
In order to provide a reproducible example, we use dplyr and dbplyr with `tbl_memdb` to load a table into a memory db.

```{r}
library(dplyr)
library(dbplyr)
iris_db <- tbl_memdb(iris)
# we isolate the connection object 
con <- src_memdb()$con
species <- "setosa"
```

We check that it works with SQL chunk to filter data into an R object

```{sql, connection=con, output.var="iris_subset", sql.show_interpolated = TRUE}
SELECT * FROM iris WHERE Species = ?species
```

```{r}
head(iris_subset)
```

You'll see the following where ?species has been replaced screenshot of HTML produced by Rmd file which uses knitr and the SQL interpolation opt-in

Sql相关问答推荐

Oracle SQL对列进行汇总并在列表底部显示总计

SQL JOIN of 2 Table with 2 sum

部分匹配表中元素的MariaDB查询查找结果

为什么两个不同的窗口函数给出不同的排序结果?

如何查询jsonb列是一个对象数组?

如何在postgres函数中插入后返回布尔值?

根据开始日期和结束日期的差异计算每天的计费

重用传递给 node 的参数-postgres upsert查询

在PostgreSQL中汇总连接表中的 case 值

此过程如何在不引用传递的参数值的情况下执行工作?

如何使用Informix创建一个临时表,将数据从根表导入并使用筛选条件

更新之前如何获得价值

如何 Select 一列具有最小值而另一列具有给定值的记录?

PostgresQL-根据另一列找到 3 个最低值

如何通过存储过程将 root 的下一个子 node 作为父 node ?

来自 SQL Server 的树层次 struct 图的 JSON

如何找到特定时间间隔内的最大和最小日期?

SQL Group By 然后映射出是否存在值

如何比较同一张表中的行并进行相应更新

如何优化sql请求?