Skip to contents

I/O for data frames and typed CSVs

Usage

write_tcsv(dt, fout)

read_tcsv(fin)

Arguments

dt

A data frame to be written to file.

fout

Output file path.

fin

Path to a typed CSV to be read in.

sep

Character. The delimiter used for the typed CSV. Defaults to comma (,).

Examples

(d = data.frame(
    A_int = c(1L:4L, NA, NaN),
    A_dbl = c(seq(0, 1, length=4), NA, NaN),
    A_cpl = c((1:4) + 1i, NA, NaN),
    A_lgl = c(T, F, F, T, T, NA),
    A_chr = c(LETTERS[1:2], NA, "", '"', '\t'),
    A_NA_int = rep(NA_integer_, 6)
))
#>   A_int     A_dbl  A_cpl A_lgl A_chr A_NA_int
#> 1     1 0.0000000   1+1i  TRUE     A       NA
#> 2     2 0.3333333   2+1i FALSE     B       NA
#> 3     3 0.6666667   3+1i FALSE  <NA>       NA
#> 4     4 1.0000000   4+1i  TRUE             NA
#> 5    NA        NA     NA  TRUE     "       NA
#> 6   NaN       NaN NaN+0i    NA    \t       NA
tmp = tempfile()
write_tcsv(d, tmp)
xfun::file_string(tmp)  # print file content
#> "A_int","A_dbl","A_cpl","A_lgl","A_chr","A_NA_int"
#> "<numeric>","<numeric>","<complex>","<logical>","<character>","<integer>"
#> "  1","0.0000000","  1+1i","TRUE","A",
#> "  2","0.3333333","  2+1i","FALSE","B",
#> "  3","0.6666667","  3+1i","FALSE",,
#> "  4","1.0000000","  4+1i","TRUE","",
#> ,,,"TRUE","""",
#> ,,,,"	",
read_tcsv(tmp)
#> # A tibble: 6 × 6
#>   A_int  A_dbl A_cpl A_lgl A_chr A_NA_int
#>   <dbl>  <dbl> <cpl> <lgl> <chr>    <int>
#> 1     1  0     1+1i  TRUE  "A"         NA
#> 2     2  0.333 2+1i  FALSE "B"         NA
#> 3     3  0.667 3+1i  FALSE  NA         NA
#> 4     4  1     4+1i  TRUE   NA         NA
#> 5    NA NA       NA  TRUE  "\""        NA
#> 6    NA NA       NA  NA     NA         NA