2017-10-31 6 views
2

RSQLite를 사용하여 dbplyr 패키지를 사용하여 데이터베이스를 만들지 만 테이블을 쓰는 (및 다시 읽는) 데에도 불구하고 데이터베이스의 크기가 0 바이트입니다. 여기 내 스크립트입니다RSQLite 디스크 데이터베이스에 데이터를 "플러시"하려면 어떻게합니까?

library("RSQLite") 
library("dbplyr") 
library("dplyr") 

data(mtcars) 

con <- DBI::dbConnect(RSQLite::SQLite(), dbname = "./mtcars.db") 
copy_to(con, mtcars, "mtcars") 

print(tbl(con, "mtcars")) 

하지만 -l 끝에 LS에서 볼 수 있듯이 내 데이터베이스의 크기는 스크립트 (그래서 거기에 있어요) 데이터베이스에서 mtcars를 읽었다에도 불구하고, 0입니다. 데이터를 다른 프로그램과 공유하기 위해 데이터베이스 파일을 사용하고 싶습니다. 따라서 주기적으로 디스크에 데이터를 "플러시"합니까?

[email protected]:~/scratch$ R -f dplysqlite.r 

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree" 
Copyright (C) 2015 The R Foundation for Statistical Computing 
Platform: x86_64-pc-linux-gnu (64-bit) 

R is free software and comes with ABSOLUTELY NO WARRANTY. 
You are welcome to redistribute it under certain conditions. 
Type 'license()' or 'licence()' for distribution details. 

    Natural language support but running in an English locale 

R is a collaborative project with many contributors. 
Type 'contributors()' for more information and 
'citation()' on how to cite R or R packages in publications. 

Type 'demo()' for some demos, 'help()' for on-line help, or 
'help.start()' for an HTML browser interface to help. 
Type 'q()' to quit R. 

> 
> library("RSQLite") 
> library("dbplyr") 
Warning messages: 
1: replacing previous import by ‘rlang::enquo’ when loading ‘dbplyr’ 
2: replacing previous import by ‘rlang::quo’ when loading ‘dbplyr’ 
3: replacing previous import by ‘rlang::quos’ when loading ‘dbplyr’ 
4: replacing previous import by ‘rlang::quo_name’ when loading ‘dbplyr’ 
> library("dplyr") 

Attaching package: ‘dplyr’ 

The following objects are masked from ‘package:dbplyr’: 

    ident, sql 

The following objects are masked from ‘package:stats’: 

    filter, lag 

The following objects are masked from ‘package:base’: 

    intersect, setdiff, setequal, union 

> 
> data(mtcars) 
> 
> con <- DBI::dbConnect(RSQLite::SQLite(), dbname = "./mtcars.db") 
> copy_to(con, mtcars, "mtcars") 
> 
> print(tbl(con, "mtcars")) 
# Source: table<mtcars> [?? x 11] 
# Database: sqlite 3.19.3 [/home/tbrowne/scratch/mtcars.db] 
    mpg cyl disp hp drat wt qsec vs am gear carb 
    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
1 21.0  6 160.0 110 3.90 2.620 16.46  0  1  4  4 
2 21.0  6 160.0 110 3.90 2.875 17.02  0  1  4  4 
3 22.8  4 108.0 93 3.85 2.320 18.61  1  1  4  1 
4 21.4  6 258.0 110 3.08 3.215 19.44  1  0  3  1 
5 18.7  8 360.0 175 3.15 3.440 17.02  0  0  3  2 
6 18.1  6 225.0 105 2.76 3.460 20.22  1  0  3  1 
7 14.3  8 360.0 245 3.21 3.570 15.84  0  0  3  4 
8 24.4  4 146.7 62 3.69 3.190 20.00  1  0  4  2 
9 22.8  4 140.8 95 3.92 3.150 22.90  1  0  4  2 
10 19.2  6 167.6 123 3.92 3.440 18.30  1  0  4  4 
# ... with more rows 
> 
> 
[email protected]:~/scratch$ ls -l 
total 4 
-rw-rw-r-- 1 tbrowne tbrowne 194 Oct 31 11:04 dplysqlite.r 
-rw-r--r-- 1 tbrowne tbrowne 0 Oct 31 11:04 mtcars.db 

답변

2

당신은 RSQLite documentation에 의해 제안 된 패턴을 사용하지 않는 것입니다. 당신의 전체 코드는 다음과 같이 보일 것이 문서에 따르면

dbWriteTable(con, "mtcars", mtcars) 

:이 문서에는 SQLite는 테이블에 데이터 프레임을 복사 dbWriteTable를 사용 dbplyr 소스에 대한

con <- dbConnect(RSQLite::SQLite(), "./mtcars.db") 
data(mtcars) 
dbWriteTable(con, "mtcars", mtcars) 
dbListTables(con) 
# Fetch all query results into a data frame: 
dbGetQuery(con, "SELECT * FROM mtcars") 
+0

충분히 funnily 나는 실제로 테이블에 쓰는 제안 된 dplyr 방법을 사용하고 있습니다. 나는 당신이 dplyr에 의지하기보다는 오히려 운전사에게 곧장 가야만한다고 생각합니다. –

+0

@ThomasBrowne 나는 또한 이것이 의심 스럽다. 왜냐하면 당신이 옳았고 어떤 종류의 플러시가 있다고 의심되는 이유가 ... 그러나 그것도 SQLite 문서에는 아무것도 언급하지 않았기 때문에'dplyr'에서 올 것입니다. –

2

copy_to() 방법 (dbplyr:::copy_to.src_sql())에는 temporary 인수가 있으며 기본값은 TRUE입니다. 즉, 새 테이블은 활성 연결에 대해서만 표시되고 연결을 닫으면 사라집니다. 다음은 예상대로 작동합니다 : 팀에서 알 수 있듯이

copy_to(con, mtcars, "mtcars", temporary = FALSE) 

또는 dbWriteTable()를 사용합니다.