From 0b0743a045554f727f230d5021a56bb7385e79c6 Mon Sep 17 00:00:00 2001 From: phgrosjean Date: Sat, 25 Aug 2018 10:45:52 +0200 Subject: [PATCH] Add write(), data_types() and comment to "read_write" options --- DESCRIPTION | 9 ++-- NAMESPACE | 2 + NEWS.md | 12 ++++- R/data_types.R | 56 ++++++++++++++++++++++ R/internal.R | 20 ++++++++ R/read.R | 26 ++-------- R/read_write_option.R | 78 ++++++++++++++++-------------- R/write.R | 101 +++++++++++++++++++++++++++++++++++++++ README.md | 2 +- TODO | 4 +- man/data_types.Rd | 52 ++++++++++++++++++++ man/read.Rd | 2 +- man/read_write_option.Rd | 2 +- man/write.Rd | 91 +++++++++++++++++++++++++++++++++++ 14 files changed, 387 insertions(+), 70 deletions(-) create mode 100644 R/data_types.R create mode 100644 R/write.R create mode 100644 man/data_types.Rd create mode 100644 man/write.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8a3aff6..87f26c8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,15 +1,16 @@ Package: data.io Type: Package -Version: 1.0.1 +Version: 1.1.0 Title: Data Input/Output, Read or Write Data from Files or Datasets in R Packages in Different Formats -Description: Read or write data from many different formats (tabular datasets, images, - ...) into R objects. Add labels and units in different languages. +Description: Read or write data from many different formats (tabular datasets, + from statistic software, ...) into R objects. Add labels and units in + different languages. Authors@R: c(person("Philippe", "Grosjean", role = c("aut", "cre"), email = "phgrosjean@sciviews.org")) Maintainer: Philippe Grosjean Depends: R (>= 3.3.0) Imports: tibble, tsibble, Hmisc, utils, readr, rlang, datasets, ggplot2, nycflights13 -Suggests: SciViews, readxl, haven, WriteXLS, openxlsx, covr, knitr, testthat +Suggests: SciViews, readxl, haven, WriteXLS, writexl, covr, knitr, testthat License: GPL-2 Encoding: UTF-8 LazyData: true diff --git a/NAMESPACE b/NAMESPACE index 341782a..1d48d3e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(as.dataframe) export(as_dataframe) export(cl) export(data_example) +export(data_types) export(hread_text) export(hread_xls) export(hread_xlsx) @@ -30,6 +31,7 @@ export(relative_path) export(type_from_extension) export(unlabelise) export(unlabelize) +export(write) importFrom(Hmisc,"label<-") importFrom(Hmisc,label) importFrom(readr,default_locale) diff --git a/NEWS.md b/NEWS.md index 0ae1354..67bdb7f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,18 @@ # data.io News +## Changes in data.io 1.1.0 + +- A basic version of write() is now available. + +- data_types() function added to easily get information about data types that + can be read() or write() + +- Description added into "read_write" options. + + ## Changes in data.io 1.0.1 -- bug corrected: forgot to change 'data' -> 'data.io' in 'read_write' options. +- Bug corrected: forgot to change 'data' -> 'data.io' in 'read_write' options. ## Changes in data.io 1.0.0 diff --git a/R/data_types.R b/R/data_types.R new file mode 100644 index 0000000..cdd3b8b --- /dev/null +++ b/R/data_types.R @@ -0,0 +1,56 @@ +#' List recognized file formats (types) for read() and write() +#' +#' @param types_only If `TRUE`, only a vector of types is returned, otherwise, +#' a `tibble` with fll specifications is provided. +#' @param view If `TRUE`, the result is "viewed" (displayed in a table in a +#' separate window, if the user interface allows it, e.g., in RStudio) and +#' returned invisibly. Otherwise, the results are returned normally. +#' +#' @description Dispaly information about data types that can read() and write() +#' can use, as well as, the original functions that are delegated (see they +#' respective help pages for more info and to know which additional parameters +#' can be used in read() and write()). +#' +#' @return An `tibble` with `types_only = FALSE`, or a character vector. +#' @details The function is mainly designed to be used interactively and to +#' provide information about file types that can be read() or write(). This +#' cannot be done through a man page because this list is dynamic and other +#' packages could add or change entries there. With `view = FALSE`, the function +#' can, nevertheless, be also used in a script or a R Markdown/Notebook +#' document. +#' @author Philippe Grosjean +#' @export +#' @seealso [read()], [write()] +#' @keywords utilities +#' @concept list file types that can be read or write +#' @examples +#' \dontrun{ +#' data_types() +#' data_types(TRUE) +#' } +#' # For non-interactive use, specify view = FALSE +#' data_types(view = FALSE) +#' data_types(TRUE, view = FALSE) +data_types <- function(types_only = FALSE, view = TRUE) { + `data types` <- getOption("read_write") + # If not installed yet, do it now! + if (is.null(`data types`)) + `data types` <- read_write_option() + + if (isTRUE(types_only)) + `data types` <- `data types`$type + + if (isTRUE(view)) { + # We don't necessarily want to use utils::View(). For instance, RStudio + # defines another version of that function, and we ant to use it instead! + view <- get0("View", mode = "function") + if (is.null(view)) { + warning("'View' function not found, return the data instead") + `data types` + } else { + view(`data types`) + invisible(`data types`) + } + } else `data types` +} + diff --git a/R/internal.R b/R/internal.R index e28b6de..dbcb118 100644 --- a/R/internal.R +++ b/R/internal.R @@ -1,3 +1,23 @@ .onLoad <- function(libname, pkgname) { read_write_option() } + +.get_function <- function(fun) { + # In case we have ns::fun + fun <- strsplit(fun, "::", fixed = TRUE)[[1L]] + if (length(fun) == 2L) { + res <- try(getExportedValue(fun[1L], fun[2L]), silent = TRUE) + if (inherits(res, "try-error")) + stop("You need function '", fun[2L], "' from package '", fun[1L], + "' to read these data. Please, install the package first", + " and make sure the function is available there.") + } else { + if (is.na(fun[1L])) + return(NA) + res <- get0(fun[1L], envir = parent.frame(), mode = "function", + inherits = TRUE) + if (is.null(res)) + stop("function '", fun[1], "' not found") + } + res +} \ No newline at end of file diff --git a/R/read.R b/R/read.R index df8ef3b..c1608a6 100644 --- a/R/read.R +++ b/R/read.R @@ -53,7 +53,7 @@ #' accross several \R packages. See `getOption("read_write")`. #' @author Philippe Grosjean #' @export -#' @seealso [read_csv()] +#' @seealso [data_types()], [write()], [read_csv()] #' @keywords utilities #' @concept read and import data #' @examples @@ -303,32 +303,12 @@ sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) { } else fun_item <- fun_list[fun_list$type == type, ] } - get_function <- function(fun) { - # In case we have ns::fun - fun <- strsplit(fun, "::", fixed = TRUE)[[1L]] - if (length(fun) == 2L) { - res <- try(getExportedValue(fun[1L], fun[2L]), silent = TRUE) - if (inherits(res, "try-error")) - stop("You need function '", fun[2L], "' from package '", fun[1L], - "' to read these data. Please, install the package first", - " and make sure the function is available there.") - } else { - if (is.na(fun[1L])) - return(NA) - res <- get0(fun[1L], envir = parent.frame(), mode = "function", - inherits = TRUE) - if (is.null(res)) - stop("function '", fun[1], "' not found") - } - res - } - # If header is not NULL and a hread_xxx() function is available, # read as many lines as there are starting with this string # and decrypt header data/metadata attribs <- NULL if (is.null(hfun)) - hfun <- get_function(fun_item$read_header) + hfun <- .get_function(fun_item$read_header) if (is.function(hfun) && !is.null(header) && header != "") { dat <- hfun(file = file, header.max = header.max, skip = skip, locale = locale) @@ -398,7 +378,7 @@ sidecar_file = TRUE, fun_list = NULL, hfun = NULL, fun = NULL, ...) { # Do we have a function to read these data? if (is.null(fun)) - fun <- get_function(fun_item$read_fun) + fun <- .get_function(fun_item$read_fun) # Read the data skip_all <- skip + n_header diff --git a/R/read_write_option.R b/R/read_write_option.R index 1ac2bfb..8fbf676 100644 --- a/R/read_write_option.R +++ b/R/read_write_option.R @@ -24,87 +24,91 @@ #' (read_write_option()) #' # To add a new type: #' tail(read_write_option(data.frame(type = "png", read_fun = "png::readPNG", -#' read_header = NA, write_fun = "png::writePNG"))) +#' read_header = NA, write_fun = "png::writePNG", comment = "PNG image"))) read_write_option <- function(new_type) { opts <- getOption("read_write", default = tibble::tribble( ~type, ~read_fun, ~read_header, - ~write_fun, + ~write_fun, ~comment, "csv", "readr::read_csv", "data.io::hread_text", - "readr::write_csv", + "readr::write_csv", "comma separated values", "csv2", "readr::read_csv2", "data.io::hread_text", - NA, + NA, "semicolon separated values", "xlcsv", "readr::read_csv", "data.io::hread_text", - "readr::write_excel_csv", + "readr::write_excel_csv", "write a CSV file more easily readable by Excel", "tsv", "readr::read_tsv", "data.io::hread_text", - "readr::write_tsv", + "readr::write_tsv", "tab separated values", "fwf", "readr::read_fwf", "data.io::hread_text", - NA, # TODO: a writer here! + NA, "fixed width file", # TODO: a writer here! "log", "readr::read_log", NA, - NA, # TODO: a writer here! + NA, "standard log file", # TODO: a writer here! "rds", "readr::read_rds", NA, - "readr::write_rds", + "readr::write_rds", "R data file (no compression by default)", "txt", "readr::read_file", NA, - "readr::write_file", + "readr::write_file", "text file (as length 1 character vector)", "raw", "readr::read_file_raw", NA, - NA, # TODO: a writer here! + NA, "binary file (read as raw vector)", + # TODO: a writer here! "ssv", "readr::read_table", "data.io::hread_text", - NA,#Space separated values + NA, "space separated values (strict)", "ssv2", "readr::read_table2", "data.io::hread_text", - NA, + NA, "space separated values (relaxed)", "csv.gz", "readr::read_csv", "data.io::hread_text", - "readr::write_csv", + "readr::write_csv", "gz compressed comma separated values", "csv2.gz", "readr::read_csv2", "data.io::hread_text", - NA, + NA, "gz compressed semicolon separated values", "tsv.gz", "readr::read_tsv", "data.io::hread_text", - "readr::write_tsv", + "readr::write_tsv", "gz compressed tab separated values", "txt.gz", "readr::read_file", NA, - "readr::write_file", + "readr::write_file", "gz compressed text file", "csv.bz2", "readr::read_csv", "data.io::hread_text", - "readr::write_csv", + "readr::write_csv", "bz2 compressed comma separated values", "csv2.bz2","readr::read_csv2", "data.io::hread_text", - NA, + NA, "bz2 compressed semicolon separated values", "tsv.bz2", "readr::read_tsv", "data.io::hread_text", - "readr::write_tsv", + "readr::write_tsv", "bz2 compressed tab separated values", "txt.bz2", "readr::read_file", "data.io::hread_text", - "readr::write_file", + "readr::write_file", "bz2 compressed text file", "csv.xz", "readr::read_csv", "data.io::hread_text", - "readr::write_csv", + "readr::write_csv", "xz compressed comma separated values", "csv2.xz", "readr::read_csv2", "data.io::hread_text", - NA, + NA, "xz compressed semicolon separated values", "tsv.xz", "readr::read_tsv", "data.io::hread_text", - "readr::write_tsv", + "readr::write_tsv", "xz compressed tab separated values", "txt.xz", "readr::read_file", NA, - "readr::write_file", + "readr::write_file", "xz compressed text file", # Buggy right now!! "csvy", "csvy::read_csvy", NA, "csvy::write_csvy", + # "comma separated value with YAML header", "xls", "readxl::read_excel", "data.io::hread_xls", - "WriteXLS::WriteXLS", + "WriteXLS::WriteXLS", "Excel old .xls format", "xlsx", "readxl::read_excel", "data.io::hread_xlsx", - "openxlsx::write.xlsx", + "writexl::write_xlsx", "Excel new .xlsx format", #"openxlsx::write.xlsx", "dta", "haven::read_dta", NA, - "haven::write_dta", + "haven::write_dta", "Stata DTA format", # read_dta() = read_stata() "sas", "haven::read_sas", NA, - "haven::write_sas", + "haven::write_sas", "SAS format", "sas7bdat","haven::read_sas", NA, - "haven::write_sas", + "haven::write_sas", "SAS format (sas7bdat)", "sav", "haven::read_sav", NA, - "haven::write_sav", + "haven::write_sav", "SPSS .sav format", + "zsav", "haven::read_sav", NA, + "haven::write_sav", "SPSS .zsav format", "por", "haven::read_por", NA, - NA, + NA, "SPSS .por format", # read_por()/read_sav() = read_spss() "xpt", "haven::read_xpt", NA, - "haven::write_xpt" #, + "haven::write_xpt", "SPSS transport format (FDA compliant)"#, #"feather", "feather::read_feather",NA, - #"feather::write_feather" + #"feather::write_feather", "transportable feather format" )) if (!missing(new_type)) { # Check it is in a correct format if (!is.data.frame(new_type)) stop("new_type must be a data.frame") - if (ncol(new_type) != 4) - stop("new_type must contain 4 columns", - " (type, read_fun, read_header & write_fun") + if (ncol(new_type) != 5) + stop("new_type must contain 5 columns", + " (type, read_fun, read_header, write_fun & comment") names(new_type) <- names(opts) opts <- rbind(opts, new_type) } diff --git a/R/write.R b/R/write.R new file mode 100644 index 0000000..bceef3c --- /dev/null +++ b/R/write.R @@ -0,0 +1,101 @@ +#' Write data from \R in files in different formats +#' +#' @param x An object to write in a file. The accepted class depends on what the +#' delegated function expects (in many cases, a `data.frame` or `tibble` is just +#' fine). If `type` is not provided, a `data.frame` is **not** suitable because +#' only an atomic vector can be provided. Give a `matrix` instead, if you want +#' to write tabular data, or provide `type = "txt"` for instance. +#' @param file The path to the file to write to. If `type` is not provide, a +#' connection, or a character string naming the file to write to. If `""``, +#' print to the standard output connection. If it is `"|cmd"`, the output is +#' piped to the command given by ‘cmd’. +#' @param ncolumns The number of columns to write the data in when `type` is +#' provoded, this is by-passed. +#' @param append If `TRUE` and `type` is not provided, the data `x` are appended +#' to the connection. +#' @param sep A string used to separate columns. Using `sep = "\t"` gives tab +#' delimited output; default is `" "` when `type` is not provide, or the default +#' provided by the delegated function if this parameter is present there. +#' @param type The type (format) of data to read. +#' @param fun_list The table with correspondance of the types, read, and write +#' functions. +#' @param ... Further arguments passed to the write function, when `type` is +#' explicitly provided. +#' +#' @description Write \R data into a file, in different formats. +#' +#' @return `x` is returned invisibly (on the contrary to [base::write()] which +#' returns `NULL`). +#' @details This function is designed to be fully compatible with +#' [base::write()], while allowing to specify `type` also, and get a more +#' interesting behaviour in this case. Hence, when `type` is **not** provided, +#' either with `write(type = ...)`, or `write$...()`, the default code is used +#' and a plain text file wit fields separated by spaces (be default) is written. +#' When type is provided, then the exportation is delegated to specific +#' functions (see [data_types()]) to write the data in different formats. +#' @author Philippe Grosjean +#' @export +#' @seealso [data_types()], [read()], [write_csv()], [base::write()] +#' @keywords utilities +#' @concept write and export data +#' @examples +#' # Always specify type to delegate to more sophisticated functions +#' # (type = NULL explicitly indicated meaning: "guess from file extension") +#' urchin <- read("urchin_bio", package = "data.io") +#' write(urchin, "urchin_temporary.csv", type = NULL) +#' # To use a format more easily readable by Excel +#' write(urchin, "urchin_temporary.csv", type = "xlcsv") +#' # ... equivalently (and more compact) +#' write$xlcsv(urchin, "urchin_temporary.csv") +#' # Tidy up +#' unlink("urchin_temporary.csv") +#' +#' # Write in Excel format +#' write$xlsx(urchin, "urchin_temporary.xlsx") +#' # Tidy up +#' unlink("urchin_temporary.xlsx") +#' +#' # Use base::write() code to output atomic vectors (and matices) in text files +#' # when you don't specify type= +#' mat1 <- matrix(1:12, nrow = 4) +#' # To get a similar presentation in the file, you have to do: +#' write(t(mat1), "my_temporary_data.txt", ncolumns = 3) +#' file.show("my_temporary_data.txt") +#' # Tidy up +#' unlink("my_temporary_data.txt") +#' rm(mat1) +write <- structure(function(x, file = "data", +ncolumns = if (is.character(x)) 1 else 5, append = FALSE, sep = " ", +type = NULL, fun_list = NULL, ...) { + # If type is missing, base::write() is used! + if (missing(type)) { + base::write(x, file = file, ncolumns = ncolumns, sep = sep) + return(invisible(x)) + } + + # Get fun_list from options() (and possibly install it) + if (is.null(fun_list)) + fun_list <- getOption("read_write") + # If not installed yet, do it now! + if (is.null(fun_list)) + fun_list <- read_write_option() + + if (is.null(type)) + type <- type_from_extension(file) + if (is.null(type)) + stop("no type provided, and impossible to guess") + if (!type %in% fun_list$type) { + stop("type '", type, "' is unknown") + } else fun_item <- fun_list[fun_list$type == type, ] + + fun_name <- fun_item$write_fun + if (is.na(fun_name)) + stop("no write function is provided for this type") + fun <- .get_function(fun_name) + if (!missing(sep)) { + res <- fun(x, file, sep = sep, ...) + } else { + res <- fun(x, file, ...) + } + invisible(x) +}, class = c("function", "subsettable_type")) diff --git a/README.md b/README.md index 767d4b1..88f9418 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # data.io - SciViews datasets and read()/write() functions [![Linux & OSX Build Status](https://travis-ci.org/SciViews/data.io.svg )](https://travis-ci.org/SciViews/data.io) -[![Win Build Status](https://ci.appveyor.com/api/projects/status/github/SciViews/data.io?branch=master&svg=true)](http://ci.appveyor.com/project/phgrosjean/data.io) +[![Win Build Status](https://ci.appveyor.com/api/projects/status/github/SciViews/data-io?branch=master&svg=true)](http://ci.appveyor.com/project/phgrosjean/data-io) [![Coverage Status](https://img.shields.io/codecov/c/github/SciViews/data.io/master.svg) ](https://codecov.io/github/SciViews/data.io?branch=master) [![CRAN Status](http://www.r-pkg.org/badges/version/data.io)](http://cran.r-project.org/package=data.io) diff --git a/TODO b/TODO index 4d0dddc..9573d20 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,7 @@ # data.io To Do list +* Completion in R and RStudio for `read$` and `write$`. + * geyser translation files are missing! * label versus comment for a dataframe? @@ -15,8 +17,6 @@ * Look at fst package for ultra-fast csv read/write (+ data.table). -* write() + keep it compatible with base::write()! - * Functions to write headers too: write -> read should restore same object! * Integrate units with the quantities, convertr, prettyunits and units packages. diff --git a/man/data_types.Rd b/man/data_types.Rd new file mode 100644 index 0000000..1ce8ec7 --- /dev/null +++ b/man/data_types.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data_types.R +\name{data_types} +\alias{data_types} +\title{List recognized file formats (types) for read() and write()} +\usage{ +data_types(types_only = FALSE, view = TRUE) +} +\arguments{ +\item{types_only}{If \code{TRUE}, only a vector of types is returned, otherwise, +a \code{tibble} with fll specifications is provided.} + +\item{view}{If \code{TRUE}, the result is "viewed" (displayed in a table in a +separate window, if the user interface allows it, e.g., in RStudio) and +returned invisibly. Otherwise, the results are returned normally.} +} +\value{ +An \code{tibble} with \code{types_only = FALSE}, or a character vector. +} +\description{ +Dispaly information about data types that can read() and write() +can use, as well as, the original functions that are delegated (see they +respective help pages for more info and to know which additional parameters +can be used in read() and write()). +} +\details{ +The function is mainly designed to be used interactively and to +provide information about file types that can be read() or write(). This +cannot be done through a man page because this list is dynamic and other +packages could add or change entries there. With \code{view = FALSE}, the function +can, nevertheless, be also used in a script or a R Markdown/Notebook +document. +} +\examples{ +\dontrun{ +data_types() +data_types(TRUE) +} +# For non-interactive use, specify view = FALSE +data_types(view = FALSE) +data_types(TRUE, view = FALSE) +} +\seealso{ +\code{\link[=read]{read()}}, \code{\link[=write]{write()}} +} +\author{ +Philippe Grosjean \href{mailto:phgrosjean@sciviews.org}{phgrosjean@sciviews.org} +} +\concept{ +list file types that can be read or write +} +\keyword{utilities} diff --git a/man/read.Rd b/man/read.Rd index 2083131..5a39b5a 100644 --- a/man/read.Rd +++ b/man/read.Rd @@ -236,7 +236,7 @@ haven_example <- function(path) } } \seealso{ -\code{\link[=read_csv]{read_csv()}} +\code{\link[=data_types]{data_types()}}, \code{\link[=write]{write()}}, \code{\link[=read_csv]{read_csv()}} } \author{ Philippe Grosjean \href{mailto:phgrosjean@sciviews.org}{phgrosjean@sciviews.org} diff --git a/man/read_write_option.Rd b/man/read_write_option.Rd index 7cb0d56..c4337e4 100644 --- a/man/read_write_option.Rd +++ b/man/read_write_option.Rd @@ -29,7 +29,7 @@ import or export data for the different types (formats). (read_write_option()) # To add a new type: tail(read_write_option(data.frame(type = "png", read_fun = "png::readPNG", - read_header = NA, write_fun = "png::writePNG"))) + read_header = NA, write_fun = "png::writePNG", comment = "PNG image"))) } \seealso{ \code{\link[=read]{read()}}, \code{\link[=getOption]{getOption()}} diff --git a/man/write.Rd b/man/write.Rd new file mode 100644 index 0000000..26dacca --- /dev/null +++ b/man/write.Rd @@ -0,0 +1,91 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/write.R +\name{write} +\alias{write} +\title{Write data from \R in files in different formats} +\usage{ +write(x, file = "data", ncolumns = if (is.character(x)) 1 else 5, + append = FALSE, sep = " ", type = NULL, fun_list = NULL, ...) +} +\arguments{ +\item{x}{An object to write in a file. The accepted class depends on what the +delegated function expects (in many cases, a \code{data.frame} or \code{tibble} is just +fine). If \code{type} is not provided, a \code{data.frame} is \strong{not} suitable because +only an atomic vector can be provided. Give a \code{matrix} instead, if you want +to write tabular data, or provide \code{type = "txt"} for instance.} + +\item{file}{The path to the file to write to. If \code{type} is not provide, a +connection, or a character string naming the file to write to. If \code{""``, print to the standard output connection. If it is}"|cmd"`, the output is +piped to the command given by ‘cmd’.} + +\item{ncolumns}{The number of columns to write the data in when \code{type} is +provoded, this is by-passed.} + +\item{append}{If \code{TRUE} and \code{type} is not provided, the data \code{x} are appended +to the connection.} + +\item{sep}{A string used to separate columns. Using \code{sep = "\t"} gives tab +delimited output; default is \code{" "} when \code{type} is not provide, or the default +provided by the delegated function if this parameter is present there.} + +\item{type}{The type (format) of data to read.} + +\item{fun_list}{The table with correspondance of the types, read, and write +functions.} + +\item{...}{Further arguments passed to the write function, when \code{type} is +explicitly provided.} +} +\value{ +\code{x} is returned invisibly (on the contrary to \code{\link[base:write]{base::write()}} which +returns \code{NULL}). +} +\description{ +Write \R data into a file, in different formats. +} +\details{ +This function is designed to be fully compatible with +\code{\link[base:write]{base::write()}}, while allowing to specify \code{type} also, and get a more +interesting behaviour in this case. Hence, when \code{type} is \strong{not} provided, +either with \code{write(type = ...)}, or \code{write$...()}, the default code is used +and a plain text file wit fields separated by spaces (be default) is written. +When type is provided, then the exportation is delegated to specific +functions (see \code{\link[=data_types]{data_types()}}) to write the data in different formats. +} +\examples{ +# Always specify type to delegate to more sophisticated functions +# (type = NULL explicitly indicated meaning: "guess from file extension") +urchin <- read("urchin_bio", package = "data.io") +write(urchin, "urchin_temporary.csv", type = NULL) +# To use a format more easily readable by Excel +write(urchin, "urchin_temporary.csv", type = "xlcsv") +# ... equivalently (and more compact) +write$xlcsv(urchin, "urchin_temporary.csv") +# Tidy up +unlink("urchin_temporary.csv") + +# Write in Excel format +write$xlsx(urchin, "urchin_temporary.xlsx") +# Tidy up +unlink("urchin_temporary.xlsx") + +# Use base::write() code to output atomic vectors (and matices) in text files +# when you don't specify type= +mat1 <- matrix(1:12, nrow = 4) +# To get a similar presentation in the file, you have to do: +write(t(mat1), "my_temporary_data.txt", ncolumns = 3) +file.show("my_temporary_data.txt") +# Tidy up +unlink("my_temporary_data.txt") +rm(mat1) +} +\seealso{ +\code{\link[=data_types]{data_types()}}, \code{\link[=read]{read()}}, \code{\link[=write_csv]{write_csv()}}, \code{\link[base:write]{base::write()}} +} +\author{ +Philippe Grosjean \href{mailto:phgrosjean@sciviews.org}{phgrosjean@sciviews.org} +} +\concept{ +write and export data +} +\keyword{utilities}