Revisão | d382ccb02f97a48ef8b4835b59bd04ba6d96458e (tree) |
---|---|
Hora | 2020-07-07 05:28:51 |
Autor | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
A small script to properly create an sqlite database and disconnect from it.
@@ -0,0 +1,43 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | +library(tidyverse) | |
4 | +library(haven) | |
5 | +library(DBI) | |
6 | +library(RSQLite) | |
7 | +library(dbplyr) | |
8 | + | |
9 | + | |
10 | +source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R") | |
11 | + | |
12 | + | |
13 | +df <- read_dta("NTM_hs6_2010_2018_v.12.dta") %>% as_tibble | |
14 | + | |
15 | +saveRDS(df, "ntm_fixed.RDS") | |
16 | + | |
17 | +write_csv(df, "ntm.csv") | |
18 | + | |
19 | + | |
20 | +## see https://themockup.netlify.app/posts/2019-04-28-nflfastr-dbplyr-rsqlite/ | |
21 | + | |
22 | + | |
23 | +## First create the database | |
24 | +mydb <- DBI::dbConnect(RSQLite::SQLite(), dbname="ntm.sqlite") | |
25 | + | |
26 | +###Then write content to the database. Give a name to the table. | |
27 | +DBI::dbWriteTable(mydb,"ntm_database",df, overwrite=T) | |
28 | + | |
29 | +## list the table | |
30 | +print("The content of the database is,") | |
31 | +print(DBI::dbListTables(mydb)) | |
32 | + | |
33 | + | |
34 | +### Probably I should run this at the end. | |
35 | +## See https://cran.r-project.org/web/packages/RSQLite/vignettes/RSQLite.html | |
36 | + | |
37 | + | |
38 | +dbDisconnect(mydb) | |
39 | +unlink("ntm.sqlite") | |
40 | + | |
41 | + | |
42 | + | |
43 | +print("So far so good") |