【问题标题】:R DBI object types incompatible with dplyr?R DBI 对象类型与 dplyr 不兼容?
【发布时间】:2020-11-21 17:24:38
【问题描述】:

我试图按照下面的代码示例from here

library(dplyr)

    con <- DBI::dbConnect(RSQLite::SQLite(), filename = ":memory:")
    mtcars_db <- copy_to(con, mtcars)
    
    mtcars_db %>%
      filter(cyl > 2) %>%
      select(mpg:hp) %>%
      head(10) %>%
      show_query()
    
    DBI::dbDisconnect(con)

我得到了以下错误,而不是网站上的结果:

storage.mode(x) 中的错误

  1. mtcars_db %>% filter(cyl > 2) %>% select(mpg:hp) %>% head(10) %>% 。 show_query()
  2. withVisible(eval(quote(_fseq(_lhs)), env, env))
  3. eval(quote(_fseq(_lhs)), env, env)
  4. eval(quote(_fseq(_lhs)), env, env)
  5. _fseq(_lhs)
  6. freduce(值, _function_list)
  7. function_list[i]
  8. 过滤器(., cyl > 2)

DBI 对象似乎已正确加载,并打印:

# Source:   table<mtcars> [?? x 11]
# Database: sqlite 3.30.1 []
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# … with more rows

即使只使用第一个管道也会发生错误:

mtcars_db %>%
          filter(cyl > 2)

另外,is.data.frame(mtcars_db) 返回 FALSE。

有人知道这里出了什么问题吗?

这里的管道是否只适用于 R 数据帧?

(这是最新的 R 4.03 和 tidyverse,在 Ubuntu 20.04 下)

【问题讨论】:

  • 请检查链接吗?在您链接到的页面上,我没有看到任何有问题的代码。
  • @Simon.S.A.当然,代码位于链接页面上的17.6 Customising evaluation with functions。这是第二个代码块。

标签: r database dplyr


【解决方案1】:

我的猜测是,在编写示例时,copy_to 返回了与数据库对象的连接。但是现在它的返回类型已经改变了(或者你加载的包版本不同了)。

如果是这样,您需要一个新命令来连接到远程表。请尝试以下操作:

# as per the question
library(dplyr)
con <- DBI::dbConnect(RSQLite::SQLite(), filename = ":memory:")
mtcars_db <- copy_to(con, mtcars)

# confirm is not a (remote) table
is.data.frame(mtcars_db)
# connect to the remote table
mtcars_db <- tbl(con, "mtcars")
# confirm now a (remote) table
is.data.frame(mtcars_db)

您可能还想使用class 并将class(mtcars)class(mtcars_db) 进行比较。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 2021-10-10
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2018-04-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多