2016-11-18 56 views
0

我有一個dplyr對象c1使用以下代碼創建。來自dplyr的SQL查詢顯示NULL

library(dplyr) 
library(RSQLite) 
my_db <- src_sqlite("my.sqlite3", create = T) 
women_sqlite <- copy_to(my_db, women, temporary = TRUE) 
c1 <- filter(women_sqlite, height == 62) 

現在,我想查看由dplyr生成的SQL查詢。如在dplyr vignette中所建議的,該命令應該是c1$query。然而,這並不表明SQL查詢了,卻顯示爲空,如下

>c1$query 
NULL 

我期待類似下面

SELECT * 
FROM `women` 
WHERE (`height` = 62.0) 

查詢的輸出,我將不勝感激,如果任何人都可以給我一些提示關於正在發生的事情。是否有可能再次檢索所需的查詢?

我R對話信息如下

R version 3.3.2 (2016-10-31) 
Platform: x86_64-apple-darwin13.4.0 (64-bit) 
Running under: OS X El Capitan 10.11.6 

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] RSQLite_1.0.0 DBI_0.5-1  dplyr_0.5.0 

loaded via a namespace (and not attached): 
[1] lazyeval_0.2.0 magrittr_1.5 R6_2.2.0  assertthat_0.1 tools_3.3.2 tibble_1.2  Rcpp_0.12.8 
+0

您對'copy_to'的使用看起來對我很可疑。該函數應該將兩個數據幀作爲輸入,而不是'tbl_sql'對象,這是'src_sqlite'返回的內容。 –

+0

@TimBiegeleisen請參閱dplyr [vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/databases.html)。這裏給出的例子還顯示'NULL'爲'c1 $ query' –

+1

你試過'show_query'嗎? –

回答

0

這是因爲,SQL數據源執行從dplyr 0.4.3改爲0.5。爲了得到查詢(和執行計劃),使用explain

> explain(c1) 
<SQL> 
SELECT * 
FROM `women` 
WHERE (`height` = 62.0) 


<PLAN> 
    addr  opcode p1 p2 p3  p4 p5 comment 
1  0   Init 0 13 0   00 <NA> 
2  1  OpenRead 0 2 1  2 00 <NA> 
3  2  Rewind 0 11 0   00 <NA> 
4  3  Column 0 0 1   00 <NA> 
5  4 RealAffinity 1 0 0   00 <NA> 
6  5   Ne 2 10 1 (BINARY) 6d <NA> 
7  6   Copy 1 3 0   00 <NA> 
8  7  Column 0 1 4   00 <NA> 
9  8 RealAffinity 4 0 0   00 <NA> 
10 9 ResultRow 3 2 0   00 <NA> 
11 10   Next 0 3 0   01 <NA> 
12 11  Close 0 0 0   00 <NA> 
13 12   Halt 0 0 0   00 <NA> 
14 13 Transaction 1 0 2  0 01 <NA> 
15 14 TableLock 1 2 0 women 00 <NA> 
16 15   Real 0 2 0  62 00 <NA> 
17 16   Goto 0 1 0   00 <NA> 

explain功能都dplyr 0.4.3和0.5工程;您不應該依賴隱藏的實現細節,如提取query組件。