@@ -23,7 +23,7 @@ It is better in this case to request smaller amount of RAM and read data (effici
2323
2424- You are not limited by RAM any longer
2525- Compared to other file formats SQLite is very good in selecting certain lines (especially if you use indexing)
26- - You can use familiar dplyr syntax or execute SQL queries directly
26+ - You can use familiar ` dplyr ` syntax or execute SQL queries directly
2727 - [ dplyr] ( https://dplyr.tidyverse.org/ ) is an interface for working with data in a database, not for modifying remote tables.
2828 - [ DBI package] ( https://dbi.r-dbi.org/ ) allows to both read and modify tables
2929- SQLite is [ actually faster for common data analysis tasks] ( https://www.sqlite.org/speed.html ) than other popular databases.
@@ -44,26 +44,30 @@ It is better in this case to request smaller amount of RAM and read data (effici
4444## Command line (CLI) example
4545Create environment
4646``` sh
47- mkdir projects/sqlite-test
48- cd projects/sqlite-test
49- conda create -p ./cenv
50- conda activate ./cenv
51- conda install -y sqlite
47+ $ mkdir projects/sqlite-test
48+ $ cd projects/sqlite-test
49+ $ conda create -p ./cenv
50+ $ source activate ./cenv
51+ $ conda install -y sqlite
5252```
5353Then [ follow this SQLite example] ( https://sqlite.org/cli.html ) .
5454``` sh
55- sqlite3 db_file.sqlite
56- create table tbl1(one varchar(10), two smallint);
57- insert into tbl1 values(' hello!' ,10);
58- insert into tbl1 values(' goodbye' , 20);
59- select * from tbl1;
55+ $ sqlite3 db_file.sqlite
56+ sqlite> create table tbl1(one varchar(10), two smallint);
57+ sqlite> insert into tbl1 values(' hello!' , 10);
58+ sqlite> insert into tbl1 values(' goodbye' , 20);
59+ sqlite> select * from tbl1;
60+ hello!| 10
61+ goodbye| 20
6062` ` `
6163Now Close session (Ctrl-D).
6264
6365Reopen session to check if changes are saved
6466` ` ` sh
65- sqlite3 db_file.sqlite
66- select * from tbl1;
67+ $ sqlite3 db_file.sqlite
68+ sqlite> select * from tbl1;
69+ hello!| 10
70+ goodbye| 20
6771` ` `
6872
6973# # R example
@@ -75,15 +79,15 @@ conda will install pre-compiled packages. Which is good (faster) and bad (not fu
7579:::
7680
7781:::tip
78- Alternative: install packages to a local directory or use renv as described in [R Packages with renv](./04_r_packages_with_renv .md)
82+ Alternative: install packages to a local directory or use renv as described in [R Packages with renv](./03_r_packages_with_renv .md)
7983` ` ` sh
8084mkdir /scratch/$USER /projects/myTempProject
8185cd /scratch/$USER /projects/myTempProject
8286
83- module load anaconda3/2020.07
87+ module load anaconda3/2024.02
8488
85- conda create -p ./cenv -c conda-forge r=4.1
86- conda activate ./cenv
89+ conda create -p ./cenv -c conda-forge r=4.5
90+ source activate ./cenv
8791conda install -c r r-rsqlite
8892conda install -c r r-tidyverse
8993conda install -c conda-forge r-remotes
@@ -144,7 +148,7 @@ df_temp <- df_con %>% filter( row_number() %in% c(1, 3) ) %>% collect
144148
145149Save as feather
146150` ` ` R
147- feather::write_feather(df_temp, paste0( " file_ " , ind, " .feather" ) )
151+ feather::write_feather(df_temp, " my_data .feather" )
148152` ` `
149153
150154# ## Alternative: read csv file to SQLite directly
@@ -155,9 +159,39 @@ conda install -c conda-forge r-sqldf
155159R
156160library(sqldf)
157161## create data file
158- sqldf("attach allData as new")
159- ## read file directly from csv to sqlite
160- read.csv.sql(file = "test.tab", sql = "create table states_data as select * from file", dbname = "allData")
162+ # sqldf("attach allData as new")
163+
164+ # make csv file for this example
165+ write.csv(df_con, "df_con.csv", row.names = FALSE)
166+
167+ # read file directly from csv to sqlite
168+ read.csv.sql(file = "df_con.csv", sql = "create table states_data as select * from file", dbname = "allData")
169+
170+ # verify data in data frame
171+ dbListTables(con)
172+ [1] "fl" "sqlite_stat1" "sqlite_stat4" "states_data"
173+
174+ df_con_sd <- tbl(con, "states_data")
175+ df_con_sd
176+ # Source: table<`states_data`> [?? x 19]
177+ # Database: sqlite 3.50.1 [/scratch/netID/myTempProject/allData]
178+ year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
179+ <int> <int> <int> <int> <int> <int> <int> <int>
180+ 1 2013 1 1 517 515 2 830 819
181+ 2 2013 1 1 533 529 4 850 830
182+ 3 2013 1 1 542 540 2 923 850
183+ 4 2013 1 1 544 545 -1 1004 1022
184+ 5 2013 1 1 554 600 -6 812 837
185+ 6 2013 1 1 554 558 -4 740 728
186+ 7 2013 1 1 555 600 -5 913 854
187+ 8 2013 1 1 557 600 -3 709 723
188+ 9 2013 1 1 557 600 -3 838 846
189+ 10 2013 1 1 558 600 -2 753 745
190+ # ℹ more rows
191+ # ℹ 11 more variables: arr_delay <int>, carrier <chr>, flight <int>,
192+ # tailnum <chr>, origin <chr>, dest <chr>, air_time <int>, distance <int>,
193+ # hour <int>, minute <int>, time_hour <int>
194+ # ℹ Use `print(n = ...)` to see more rows
161195```
162196
163197## UI for SQLite - SQLiteStudio
0 commit comments