
Define default read/write options and add items to it
Source:R/read_write_option.R
read_write_option.Rd
Define the functions that read()
or write() must call to
import or export data for the different types (formats).
Arguments
- new_type
A data.frame with four columns:
type
,read_fun
,read_header
andwrite_fun
containing each a single character string orNA
.type
is the usual extension for this type of file, e.g.,png
for PNG images,read_fun
,read_header
andwrite_fun
are character strings with "pkg::fun" format ("pkg" is the package containing the function and "fun" is the function name), or just "fun" if the function is visible on the search path.
Value
The data.frame with all known formats is returned invisibly. The same
data.frame is also saved in the read_write`` option, and can be retrieved directly with
getOption("read_write")`.
Author
Philippe Grosjean phgrosjean@sciviews.org
Examples
# The default options
(read_write_option())
#> # A tibble: 32 × 5
#> type read_fun read_header write_fun comment
#> <chr> <chr> <chr> <chr> <chr>
#> 1 csv readr::read_csv data.io::hread_text readr::write_csv comma …
#> 2 csv2 readr::read_csv2 data.io::hread_text NA semico…
#> 3 xlcsv readr::read_csv data.io::hread_text readr::write_excel_csv write …
#> 4 tsv readr::read_tsv data.io::hread_text readr::write_tsv tab se…
#> 5 fwf readr::read_fwf data.io::hread_text NA fixed …
#> 6 log readr::read_log NA NA standa…
#> 7 rds readr::read_rds NA readr::write_rds R data…
#> 8 txt readr::read_file NA readr::write_file text f…
#> 9 raw readr::read_file_raw NA NA binary…
#> 10 ssv readr::read_table data.io::hread_text NA space …
#> # ℹ 22 more rows
# To add a new type:
tail(read_write_option(data.frame(type = "png", read_fun = "png::readPNG",
read_header = NA, write_fun = "png::writePNG", comment = "PNG image")))
#> # A tibble: 6 × 5
#> type read_fun read_header write_fun comment
#> <chr> <chr> <chr> <chr> <chr>
#> 1 sas7bdat haven::read_sas NA haven::write_sas SAS format (sas7bdat)
#> 2 sav haven::read_sav NA haven::write_sav SPSS .sav format
#> 3 zsav haven::read_sav NA haven::write_sav SPSS .zsav format
#> 4 por haven::read_por NA NA SPSS .por format
#> 5 xpt haven::read_xpt NA haven::write_xpt SPSS transport format (…
#> 6 png png::readPNG NA png::writePNG PNG image