2

有没有办法直接从tbl_dbi对象中获取字段名称(db_mtcars如下)?

 library(RSQLite)
 library(dbplyr)
 library(dplyr)
 con <- dbConnect(RSQLite::SQLite(), ":memory:")
 dbWriteTable(con, "mtcars", mtcars)

 db_mtcars <- tbl(con, "mtcars")
4

2 回答 2

5

是的colnames()

> colnames(db_mtcars)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
于 2017-11-15T19:15:29.563 回答
2

colnames(db_mtcars)是简单的解决方案,DBI::dbListFields(con, "mtcars")是奇特的解决方案。即使names(db_mtcars)失败,两者都可以工作。

于 2017-11-15T19:21:27.590 回答