MyNixOS website logo
Description

Enhanced Reading and Writing for 'SQLite' Databases.

Reads and writes data frames to 'SQLite' databases while preserving time zones (for POSIXct columns), projections (for 'sfc' columns), units (for 'units' columns), levels (for factors and ordered factors) and classes for logical, Date and 'hms' columns. It also logs changes to tables and provides more informative error messages.

readwritesqlite

Lifecycle:stable R-CMD-check Coveragestatus License:MIT CRANstatus CRAN Downloads

readwritesqlite

SQLite databases are a simple, powerful way to validate, query and store related data frames particularly when used with the RSQLite package. However, current solutions do not preserve (or check) meta data, log changes or provide particularly useful error messages.

readwritesqlite is an R package that by default

  • preserves (and subsequently checks) the following metadata
    • the class for logical, Date and hms columns
    • the levels for factors and ordered factors
    • the time zone for POSIXct columns
    • the units for unit columns
    • the projection for sfc columns
    • the sf column for sf objects
  • logs
    • the date time
    • system user
    • table creation and data insertion or deletion
  • provides informative error messages if
    • columns are missing
    • NOT NULL columns contain missing values
    • PRIMARY KEY column values in the input data are not unique

readwritesqlite also allows the user to

  • write environments (or named lists) of data frames (useful for populating databases)
  • delete existing data (and meta data) before writing (useful for converting an existing database)
  • replace existing data which causes unique or primary key conflicts (useful for updating databases)
  • confirm data can be written without committing any changes (useful for checking data)
  • check all existing tables are written to (useful for data transfers)
  • rearrange and add levels for factors and add levels for ordered factors
  • initialize the meta data for a new table by writing a data frame or sf data frame with no rows but logical, Date, factor, ordered, POSIXct, sfc or unit columns (useful for creating an empty database with additional informative checks)

readwritesqlite provides all these features through its rws_write() and rws_read() functions.

The rws_query() function allows the user to pass a SQL query. By default, the metadata (except the setting of the sf column) is, if unambiguously defined, preserved for each column in the final query. To enable this functionality the user should ensure that a) columns in tables which will be referenced in the same query should have different names or identical metadata and b) column names in the final query should match those in the referenced base tables.

The init, meta and log data are stored in separate tables from the main data which means that they do not interfere with other ways of interacting with a SQLite database.

Installation

To install the latest release from CRAN

install.packages("readwritesqlite")

To install the developmental version from GitHub

# install.packages("remotes")
remotes::install_github("poissonconsulting/readwritesqlite")

Demonstration

Key attribute information is preserved for many classes.

library(readwritesqlite)

# for nicer printing of data frames
library(tibble)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE

conn <- rws_connect()

rws_data <- readwritesqlite::rws_data
rws_data
#> # A tibble: 3 × 6
#>   logical date       factor ordered posixct             units
#>   <lgl>   <date>     <fct>  <ord>   <dttm>                [m]
#> 1 TRUE    2000-01-01 x      x       2001-01-02 03:04:05  10  
#> 2 FALSE   2001-02-03 y      y       2006-07-08 09:10:11  11.5
#> 3 NA      NA         <NA>   <NA>    NA                   NA

rws_write(rws_data, exists = FALSE, conn = conn)

rws_read_table("rws_data", conn = conn)
#> # A tibble: 3 × 6
#>   logical date       factor ordered posixct             units
#>   <lgl>   <date>     <fct>  <ord>   <dttm>                [m]
#> 1 TRUE    2000-01-01 x      x       2001-01-02 03:04:05  10  
#> 2 FALSE   2001-02-03 y      y       2006-07-08 09:10:11  11.5
#> 3 NA      NA         <NA>   <NA>    NA                   NA

The attribute information is stored in the metadata table

rws_read_meta(conn = conn)
#> # A tibble: 6 × 4
#>   TableMeta ColumnMeta MetaMeta          DescriptionMeta
#>   <chr>     <chr>      <chr>             <chr>          
#> 1 RWS_DATA  DATE       class: Date       <NA>           
#> 2 RWS_DATA  FACTOR     factor: 'x', 'y'  <NA>           
#> 3 RWS_DATA  LOGICAL    class: logical    <NA>           
#> 4 RWS_DATA  ORDERED    ordered: 'y', 'x' <NA>           
#> 5 RWS_DATA  POSIXCT    tz: Etc/GMT+8     <NA>           
#> 6 RWS_DATA  UNITS      units: m          <NA>

The user can add descriptions if they wish.

rws_describe_meta("rws_data", "posixct", "The time of a visit", conn = conn)
rws_describe_meta("rws_data", "units", "The site length.", conn = conn)
rws_read_meta(conn = conn)
#> # A tibble: 6 × 4
#>   TableMeta ColumnMeta MetaMeta          DescriptionMeta    
#>   <chr>     <chr>      <chr>             <chr>              
#> 1 RWS_DATA  DATE       class: Date       <NA>               
#> 2 RWS_DATA  FACTOR     factor: 'x', 'y'  <NA>               
#> 3 RWS_DATA  LOGICAL    class: logical    <NA>               
#> 4 RWS_DATA  ORDERED    ordered: 'y', 'x' <NA>               
#> 5 RWS_DATA  POSIXCT    tz: Etc/GMT+8     The time of a visit
#> 6 RWS_DATA  UNITS      units: m          The site length.

The log provides a record of data changes that have been made using readwritesqlite.

rws_read_log(conn = conn)
#> # A tibble: 2 x 5
#>   DateTimeUTCLog      UserLog TableLog CommandLog NRowLog
#>   <dttm>              <chr>   <chr>    <chr>        <int>
#> 1 2019-07-07 16:05:10 joe     RWS_DATA CREATE           0
#> 2 2019-07-07 16:05:11 joe     RWS_DATA INSERT           3

Don’t forget to disconnect when done.

rws_disconnect(conn)

Information

For more information on using readwritesqlite see the vignette using-readwritesqlite.

Contribution

Please report any issues.

Pull requests are always welcome.

Code of Conduct

Please note that the readwritesqlite project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Metadata

Version

0.2.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows