resultSet错误:使用SQL连接编译RMarkdown

时间:2018-02-03 23:01:00

标签: r postgresql database-connection r-markdown

我正在使用这种通用结构创建RMarkdown文件:

  • 将markdown连接到现有postgreSQL数据库的块
  • 使用SQL连接将现有表合并为一个主表,然后将主数据库导出为CSV文件的块。
  • 将新CSV文件作为数据框读取并执行的R块 统计分析

该脚本对我的分析运行得很好,但我确实在专用于合并表并导出为CSV的SQL块下面输出了错误。此外,当我尝试编织为PDF时,它会在这些块上停止并输出类似的错误。下面是我的工作及其产生的错误的简化示例。请帮我弄清楚如何最好地处理这个错误来编织文件。在做了一些研究之后,我仍然不太确定它意味着什么。

将我的表合并并准备好导出所有内容的块:

{sql, connection = db}
DROP TABLE IF EXISTS food_table;

WITH subquery AS (
   SELECT
      menu.recipes,
      menu.menu_price AS price 
   FROM ingredients
   INNER JOIN menu on ingredients.id = menu.recipeid

   UNION

   SELECT 
      portion * unitp AS price,
      substitute AS recipes
   FROM menu
   )
SELECT
   recipes.id
   ingredients.id
INTO food_table
FROM menu

导出为CSV以供以后使用的块:

{sql, connection = db}
COPY food_table TO '/Users/Name/Desktop/Project/food_table.csv' DELIMITER ',' CSV HEADER

运行以下任一块时出错:postgresqlFetch(res,n,...)出错:RS-DBI驱动程序:(resultSet与SELECT语句不对应)无法执行SQL块

运行knit PDF时输出错误:postgresqlFetch(res,n,...)出错:RS-DBI驱动程序:( resultSet与SELECT语句不对应)CALLS:... - >获取 - >获取 - > postgresqlFetch - > .Call Execution停止了

1 个答案:

答案 0 :(得分:1)

knitr SQL引擎目前在查询开头查找以下关键字(INSERTUPDATEDELETECREATE)是否应该在后台运行,否则默认为SELECT查询DBI::dbFetch。换句话说,根据您的错误判断,您的查询将包含resultSet(即返回数据),并且当它没有时会失败。

例如,如果您直接从数据库中读取food_table,那么块将起作用。用以下代码替换你的第二个块:

```{sql read_food_table, connection = db, output.var = "food_table"}
SELECT * FROM food_table
```

,对象food_table现在可用作R数据对象。

根据您正在使用的套餐,DBI::dbGetQuery 可以处理您的查询,您可以通过设置{{1}来强制knitr拨打电话} chunk选项:

max.print

如果您想运行显示不返回数据的查询,可以使用以下工作流程:

<强> MySQLCode.sql

{sql, connection = db, max.print = 0}

<强> RNotebook.Rmd

-- ---- create_food_table
DROP TABLE IF EXISTS food_table;

WITH subquery AS (
   SELECT
      menu.recipes,
      menu.menu_price AS price 
   FROM ingredients
   INNER JOIN menu on ingredients.id = menu.recipeid

   UNION

   SELECT 
      portion * unitp AS price,
      substitute AS recipes
   FROM menu
   )
SELECT
   recipes.id
   ingredients.id
INTO food_table
FROM menu

-- ---- export_food_table
COPY food_table TO '/Users/Name/Desktop/Project/food_table.csv' DELIMITER ',' CSV HEADER

命名代码块,分解它们并使用Load the SQL chunks from an external file invisibly. ```{r read_sql, include=FALSE, eval=TRUE, cache=FALSE} knitr::read_chunk("MySQLCode.sql")) ``` Create a DB connection using the appropriate library. ```{r create_con} library(...) con <- dbConnect(...) ``` For each SQL chunk, display (but do not evaluate) the chunk with the `sql` engine. Subsequently run (but do not show) the chunk with the `r` engine ```{sql create_food_table, eval=FALSE} ``` ```{r execute_create_food_table, echo=FALSE} dbExecute(con, knitr:::knit_code$get("create_food_table")) ``` ```{sql export_food_table, eval=FALSE} ``` ```{r execute_export_food_table, echo=FALSE} dbExecute(con, knitr:::knit_code$get("export_food_table")) ``` 的目的意味着不必为它们被使用的块重写它们。 由您决定是将代码分成块还是一次只分析整个文件knitr::read_chunk。您可能根本不需要依赖外部文件。

请确保安装最新版本的dbExecute(&gt; = 1.18),因为最近添加了支持SQL代码的块。