【问题标题】:RMarkdown will not render from Rscript with sqlite connectionRMarkdown 不会从带有 sqlite 连接的 Rscript 呈现
【发布时间】:2016-08-08 20:51:00
【问题描述】:

如果 Rmarkdown 文档中有 dplyr SQLite 调用,我将无法使用 Rscript 渲染它们。以这个 MWE (test.Rmd) 为例,它需要 dplyr::lahman_sqlite() 表的本地副本中的表。

---
title: "TestFile"
output: html_document
---

```{r setup, include=FALSE}
library(dplyr)
lahman <- src_sqlite("lahman.sqlite")
```

```{r}
tbl(lahman, "Batting")
```

在实时 R 控制台会话中,我可以调用 rmarkdown::render("test.Rmd"),并且此文档按预期构建。但是如果我在命令行上调用Rscript -e 'rmarkdown::render("test.Rmd")',我会收到以下错误:

Quitting from lines 12-13 (test.Rmd)
Error in UseMethod("db_query_fields") :
  no applicable method for 'db_query_fields' applied to an object of class "SQLiteConnection"
Calls: render ... make_tbl -> structure -> op_base_remote -> db_query_fields

我在下面附上了我的 sessionInfo,但我在 OS X 和 Ubuntu 上都重复了这个错误。

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin15.5.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.5.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5     digest_0.6.9    assertthat_0.1  R6_2.1.2        DBI_0.4-1       formatR_1.4     magrittr_1.5    evaluate_0.9   
 [9] RSQLite_1.0.0   stringi_1.1.1   rmarkdown_0.9.6 tools_3.3.1     stringr_1.0.0   Lahman_4.0-1    yaml_2.1.13     htmltools_0.3.5
[17] knitr_1.13      tibble_1.0  

【问题讨论】:

  • dplyr library() 调用后添加requireNamespace("RSQLite")
  • @hrbrmstr 错误仍然存​​在。无论如何,加载 dplyr 库已经加载了该命名空间。
  • 另一方面,显式调用library(RSQLite) 确实有效。奇怪的是 Rscript 搞乱了命名空间调用...

标签: r sqlite dplyr r-markdown


【解决方案1】:

问题在于使用Rscript 时未加载methods 包。因此,需要通过在设置块中添加library(methods) 来显式加载它。

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2021-08-04
    相关资源
    最近更新 更多