使用dplyr获取SQL表的列类型

Ale*_*nov 6 sqlite r dplyr

是否有dplyr(或其他包)命令用于获取SQL表的列(字段?)类型?例如...

library(RSQLite)
library(dplyr)

data(iris)

dat_sql <- src_sqlite("test.sqlite", create = TRUE)
copy_to(dat_sql, iris, name = "iris_df")

iris_tbl <- tbl(dat_sql, "iris_df")
iris_tbl
# Source:   query [?? x 5]
# Database: sqlite 3.8.6 [test.sqlite]
# 
#    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#           <dbl>       <dbl>        <dbl>       <dbl>   <chr>
# 1           5.1         3.5          1.4         0.2  setosa
# 2           4.9         3.0          1.4         0.2  setosa
# 3           4.7         3.2          1.3         0.2  setosa
# 4           4.6         3.1          1.5         0.2  setosa
# 5           5.0         3.6          1.4         0.2  setosa
# 6           5.4         3.9          1.7         0.4  setosa
# 7           4.6         3.4          1.4         0.3  setosa
# 8           5.0         3.4          1.5         0.2  setosa
# 9           4.4         2.9          1.4         0.2  setosa
# 10          4.9         3.1          1.5         0.1  setosa
# # ... with more rows
Run Code Online (Sandbox Code Playgroud)

我对一个命令感兴趣,它会告诉我前四列是类型的dbl,最后一列是chr(或者更好的是,R类型numeric和列character)而不是实际 collect存储内存中的数据.既然是印刷品,就必须有办法做到这一点,对吧?我试着str无济于事:

str(iris_tbl)
# List of 2
#  $ src:List of 2
#   ..$ con :Formal class 'SQLiteConnection' [package "RSQLite"] with 5 slots
#   .. .. ..@ Id                 :<externalptr> 
#   .. .. ..@ dbname             : chr "test.sqlite"
#   .. .. ..@ loadable.extensions: logi TRUE
#   .. .. ..@ flags              : int 6
#   .. .. ..@ vfs                : chr ""
#   ..$ path: chr "test.sqlite"
#   ..- attr(*, "class")= chr [1:3] "src_sqlite" "src_sql" "src"
#  $ ops:List of 3
#   ..$ src :List of 2
#   .. ..$ con :Formal class 'SQLiteConnection' [package "RSQLite"] with 5 slots
#   .. .. .. ..@ Id                 :<externalptr> 
#   .. .. .. ..@ dbname             : chr "test.sqlite"
#   .. .. .. ..@ loadable.extensions: logi TRUE
#   .. .. .. ..@ flags              : int 6
#   .. .. .. ..@ vfs                : chr ""
#   .. ..$ path: chr "test.sqlite"
#   .. ..- attr(*, "class")= chr [1:3] "src_sqlite" "src_sql" "src"
#   ..$ x   :Classes 'ident', 'sql', 'character'  chr "iris_df"
#   ..$ vars: chr [1:5] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" ...
#   ..- attr(*, "class")= chr [1:3] "op_base_remote" "op_base" "op"
#  - attr(*, "class")= chr [1:4] "tbl_sqlite" "tbl_sql" "tbl_lazy" "tbl"
# NULL
Run Code Online (Sandbox Code Playgroud)

Ste*_*pré 5

看一下glimpse()

这就像打印的转置版本:列沿着页面延伸,而数据则横向延伸。这使得可以查看数据框中的每一列。它有点像应用于数据框的 str,但它试图向您显示尽可能多的数据。(即使应用于远程数据源,它也始终显示底层数据。 )

这使:

> glimpse(iris_tbl)
#Observations: NA
#Variables: 5
#$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0,...
#$ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4,...
#$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5,...
#$ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2,...
#$ Species      <chr> "setosa", "setosa", "setosa", "setosa",...
Run Code Online (Sandbox Code Playgroud)

如果你想得到一个向量,你可以这样做:

vapply(as.data.frame(head(iris_tbl)), typeof, character(1))
Run Code Online (Sandbox Code Playgroud)

这使:

#Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#    "double"     "double"     "double"     "double"  "character" 
Run Code Online (Sandbox Code Playgroud)


TJ *_*ahr 5

打印远程表的预览时,看起来dplyr确实collect在表的前几行上使用了。由于dplyr会检索一些样本数据,因此您也可以这样做。

在这里,我们为与前几行的查询headcollect查询结果,并检查类每列的。

iris_tbl %>% 
  head %>% 
  collect %>% 
  lapply(class) %>% 
  unlist
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>    "numeric"    "numeric"    "numeric"    "numeric"  "character" 
Run Code Online (Sandbox Code Playgroud)

(与数据框一起使用时,将lapply执行列功能应用程序,因此它适用class于每一列。)

要获取dplyr使用的类型名称,请使用type_sum

iris_tbl %>% head %>% collect %>% lapply(type_sum) %>% unlist
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>        "dbl"        "dbl"        "dbl"        "dbl"        "chr" 
Run Code Online (Sandbox Code Playgroud)

  • 请注意:“type_sum()”可以让您更接近数据库类型而不是 R 类型,例如对于 postgres,它将返回“pq_json”,其中“typeof()”返回“character” (2认同)