This is about twice as fast as write.csv(), and never writes row names. output_column() is a generic method used to coerce columns to suitable output.

write_delim(x, path, delim = " ", na = "NA", append = FALSE,
  col_names = !append)

write_csv(x, path, na = "NA", append = FALSE, col_names = !append)

write_excel_csv(x, path, na = "NA", append = FALSE, col_names = !append)

write_tsv(x, path, na = "NA", append = FALSE, col_names = !append)

Arguments

x

A data frame to write to disk

path

Path or connection to write to.

delim

Delimiter used to separate values. Defaults to " ". Must be a single character.

na

String used for missing values. Defaults to NA. Missing values will never be quoted; strings with the same value as na will always be quoted.

append

If FALSE, will overwrite existing file. If TRUE, will append to existing file. In both cases, if file does not exist a new file is created.

col_names

Write columns names at the top of the file?

Value

write_*() returns the input x invisibly.

Output

Factors are coerced to character. Doubles are formatted using the grisu3 algorithm. POSIXct's are formatted as ISO8601.

All columns are encoded as UTF-8. write_excel_csv() also includes a UTF-8 Byte order mark which indicates to Excel the csv is UTF-8 encoded.

Values are only quoted if needed: if they contain a comma, quote or newline.

References

Florian Loitsch, Printing Floating-Point Numbers Quickly and Accurately with Integers, PLDI '10, http://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf

Examples

tmp <- tempfile() write_csv(mtcars, tmp) head(read_csv(tmp))
#> Parsed with column specification: #> cols( #> mpg = col_double(), #> cyl = col_integer(), #> disp = col_double(), #> hp = col_integer(), #> drat = col_double(), #> wt = col_double(), #> qsec = col_double(), #> vs = col_integer(), #> am = col_integer(), #> gear = col_integer(), #> carb = col_integer() #> )
#> # A tibble: 6 × 11 #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int> #> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 #> 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 #> 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 #> 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# format_* is useful for testing and reprexes cat(format_csv(head(mtcars)))
#> mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb #> 21,6,160,110,3.9,2.62,16.46,0,1,4,4 #> 21,6,160,110,3.9,2.875,17.02,0,1,4,4 #> 22.8,4,108,93,3.85,2.32,18.61,1,1,4,1 #> 21.4,6,258,110,3.08,3.215,19.44,1,0,3,1 #> 18.7,8,360,175,3.15,3.44,17.02,0,0,3,2 #> 18.1,6,225,105,2.76,3.46,20.22,1,0,3,1
cat(format_tsv(head(mtcars)))
#> mpg cyl disp hp drat wt qsec vs am gear carb #> 21 6 160 110 3.9 2.62 16.46 0 1 4 4 #> 21 6 160 110 3.9 2.875 17.02 0 1 4 4 #> 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 #> 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2 #> 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
cat(format_delim(head(mtcars), ";"))
#> mpg;cyl;disp;hp;drat;wt;qsec;vs;am;gear;carb #> 21;6;160;110;3.9;2.62;16.46;0;1;4;4 #> 21;6;160;110;3.9;2.875;17.02;0;1;4;4 #> 22.8;4;108;93;3.85;2.32;18.61;1;1;4;1 #> 21.4;6;258;110;3.08;3.215;19.44;1;0;3;1 #> 18.7;8;360;175;3.15;3.44;17.02;0;0;3;2 #> 18.1;6;225;105;2.76;3.46;20.22;1;0;3;1
df <- data.frame(x = c(1, 2, NA)) format_csv(df, na = ".")
#> [1] "x\n1\n2\n.\n"
# Quotes are automatically as needed df <- data.frame(x = c("a", '"', ",", "\n")) cat(format_csv(df))
#> x #> a #> """" #> "," #> " #> "
# A output connection will be automatically created for output filenames # with appropriate extensions. dir <- tempdir() write_tsv(mtcars, file.path(dir, "mtcars.tsv.gz")) write_tsv(mtcars, file.path(dir, "mtcars.tsv.bz2")) write_tsv(mtcars, file.path(dir, "mtcars.tsv.xz"))