Skip to contents

Writes tabular data in the given format.

Usage

nemo_write(
  d,
  fpfix = NULL,
  format = "tsv",
  id = NULL,
  dbconn = NULL,
  dbtab = NULL
)

Arguments

d

(data.frame())
A data.frame (or tibble) with tidy data.

fpfix

(character(1))
File prefix. The file extension is generated automatically via the format argument. For a format of db, this is inserted into the nemo_pfix column.

format

(character(1))
Output format. One of tsv, csv, parquet, rds, or db.

id

(character(1))
ID to use in the first nemo_id column for this table.

dbconn

(DBIConnection(1))
Database connection object (see DBI::dbConnect). Used only when format is db.

dbtab

(character(1))
Database table name (see DBI::dbWriteTable). Used only when format is db.

Examples

d <- tibble::tibble(name = "foo", data = 123)
fpfix <- file.path(tempdir(), "data_test1")
format <- "csv"
id <- "run1"
nemo_write(d = d, fpfix = fpfix, format = format, id = id)
readr::read_csv(glue::glue("{fpfix}.csv.gz"))
#> Rows: 1 Columns: 3
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (2): nemo_id, name
#> dbl (1): data
#> 
#>  Use `spec()` to retrieve the full column specification for this data.
#>  Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 1 × 3
#>   nemo_id name   data
#>   <chr>   <chr> <dbl>
#> 1 run1    foo     123
if (FALSE) { # RPostgres::postgresHasDefault()
# for database writing
con <- DBI::dbConnect(RPostgres::Postgres())
tbl_nm <- "awesome_tbl"
nemo_write(d = d, fpfix = basename(fpfix), format = "db", id = "123", dbconn = con, dbtab = tbl_nm)
DBI::dbListTables(con)
DBI::dbReadTable(con, tbl_nm)
DBI::dbDisconnect(con)
}