Title: | Read and Write CSV Files with Selected Conventions |
---|---|
Description: | Reads and writes CSV with selected conventions. Uses the same generic function for reading and writing to promote consistent formats. |
Authors: | Tim Bergsma |
Maintainer: | Tim Bergsma <[email protected]> |
License: | GPL-3 |
Version: | 0.6.2 |
Built: | 2025-02-07 05:12:12 UTC |
Source: | https://github.com/bergsmat/csv |
Reads or writes CSV files in a conventional way.
Generic, with methods for character and data.frame.
A length-one character argument is treated as a
filepath and tries to return a data.frame.
A data.frame argument is written to the specified
filepath. Typically, quote
and row.names
are FALSE and na
is ".". When reading, white
space and empty strings are treated as NA, and strip.white
is TRUE. When writing, values with commas or
double-quotes are double-quoted (and embedded double-quotes are doubled).
as.csv(x, ...)
as.csv(x, ...)
x |
object |
... |
passed arguments |
as.csv.character
, as.csv.data.frame
Other as.csv:
as.csv.character()
,
as.csv.data.frame()
data <- head(Theoph) filepath <- file.path(tempdir(),'theoph.csv') as.csv(data,filepath) as.csv(filepath)
data <- head(Theoph) filepath <- file.path(tempdir(),'theoph.csv') as.csv(data,filepath) as.csv(filepath)
Treat a character string as a CSV filename.
## S3 method for class 'character' as.csv( x, as.is = TRUE, na.strings = c("", "\\s", ".", "NA"), strip.white = TRUE, check.names = FALSE, source = getOption("csv_source", TRUE), ... )
## S3 method for class 'character' as.csv( x, as.is = TRUE, na.strings = c("", "\\s", ".", "NA"), strip.white = TRUE, check.names = FALSE, source = getOption("csv_source", TRUE), ... )
x |
character file path |
as.is |
passed to |
na.strings |
passed to |
strip.white |
passed to |
check.names |
passed to |
source |
whether to assign x as the source attribute of the return value |
... |
passed to |
If x is character, is length one, and is a path to a file, an attempt is made to read the file.
data.frame, with attribute 'source' set to x
Other as.csv:
as.csv.data.frame()
,
as.csv()
Saves a data.frame as CSV, using selected conventions.
## S3 method for class 'data.frame' as.csv(x, file, na = ".", quote = FALSE, auto = !quote, row.names = FALSE, ...)
## S3 method for class 'data.frame' as.csv(x, file, na = ".", quote = FALSE, auto = !quote, row.names = FALSE, ...)
x |
data.frame |
file |
passed to |
na |
passed to |
quote |
passed to |
auto |
double-quote column names and row values with embedded commas or double-quotes; the latter are escaped by doubling them |
row.names |
passed to |
... |
passed to |
invisible data.frame (x)
Other as.csv:
as.csv.character()
,
as.csv()
x <- data.frame( check.names = FALSE, stringsAsFactors = TRUE, person = 1:3, `name, suffix` = c("Bill Smith", 'Joseph "Joe" Hancock', "Mary Laguire, DDS") ) file <- tempfile() as.csv(x,file) y <- as.csv(file,as.is=FALSE) attr(y,'source') attr(y,'source') <- NULL x y stopifnot(identical(x,y))
x <- data.frame( check.names = FALSE, stringsAsFactors = TRUE, person = 1:3, `name, suffix` = c("Bill Smith", 'Joseph "Joe" Hancock', "Mary Laguire, DDS") ) file <- tempfile() as.csv(x,file) y <- as.csv(file,as.is=FALSE) attr(y,'source') attr(y,'source') <- NULL x y stopifnot(identical(x,y))