These functions are equivalent to write_csv()
etc., but instead
of writing to disk, they return a string.
format_delim(x, delim, na = "NA", append = FALSE, col_names = !append, quote_escape = "double") format_csv(x, na = "NA", append = FALSE, col_names = !append, quote_escape = "double") format_csv2(x, na = "NA", append = FALSE, col_names = !append, quote_escape = "double") format_tsv(x, na = "NA", append = FALSE, col_names = !append, quote_escape = "double")
x | A data frame to write to disk |
---|---|
delim | Delimiter used to separate values. Defaults to |
na | String used for missing values. Defaults to NA. Missing values
will never be quoted; strings with the same value as |
append | If |
col_names | Write columns names at the top of the file? Must be either
|
quote_escape | The type of escaping to use for quoted values, one of
"double", "backslash" or "none". You can also use |
A string.
Factors are coerced to character. Doubles are formatted using the grisu3
algorithm. POSIXct's are formatted as ISO8601 with a UTC timezone Note:
POSIXct
objects in local or non-UTC timezones will be converted to UTC time
before writing.
All columns are encoded as UTF-8. write_excel_csv()
and write_excel_csv2()
also include a
UTF-8 Byte order mark
which indicates to Excel the csv is UTF-8 encoded.
write_excel_csv2()
and write_csv2
were created to allow users with
different locale settings save csv files with their default settings ;
as
column separator and ,
as decimal separator. This is common in some European countries.
Values are only quoted if needed: if they contain a comma, quote or newline.
The write_*()
functions will automatically compress outputs if an appropriate extension is given. At present, three
extensions are supported, .gz
for gzip compression, .bz2
for bzip2 compression and .xz
for lzma compression. See
the examples for more information.
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
#>#> #> #> #> #> #> #> #> #> #> #> #> #>#> # A tibble: 6 x 11 #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 #> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 #> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 #> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 #> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1#> 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#> 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#> 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#> [1] "x\n1\n2\n.\n"# Quotes are automatically as needed df <- data.frame(x = c("a", '"', ",", "\n")) cat(format_csv(df))#> x #> a #> """" #> "," #> " #> "