5
5
# ' @param escape The type of escape to use when quotes are in the data.
6
6
# ' - `double` - quotes are escaped by doubling them.
7
7
# ' - `backslash` - quotes are escaped by a preceding backslash.
8
+ # ' - `sep` - tabs, newlines, and backslashes are escaped as `\t`, `\n`, and `\\`
8
9
# ' - `none` - quotes are not escaped.
9
10
# ' @param quote How to handle fields which contain characters that need to be
10
11
# ' quoted.
37
38
# ' # vroom_write(mtcars, "mtcars.tsv.xz")
38
39
vroom_write <- function (x , file , delim = ' \t ' , eol = " \n " , na = " NA" , col_names = ! append ,
39
40
append = FALSE , quote = c(" needed" , " all" , " none" ), escape =
40
- c(" double" , " backslash" , " none" ), bom = FALSE , num_threads =
41
+ c(" double" , " backslash" , " sep " , " none" ), bom = FALSE , num_threads =
41
42
vroom_threads(), progress = vroom_progress(), path = deprecated()) {
42
43
43
44
if (lifecycle :: is_present(path )) {
@@ -53,6 +54,15 @@ vroom_write <- function(x, file, delim = '\t', eol = "\n", na = "NA", col_names
53
54
quote <- match.arg(quote )
54
55
escape <- match.arg(escape )
55
56
57
+ if (escape == " sep" ) {
58
+ if (! all(c(delim , eol ) %in% c(" \t " , " \n " , " \r " , " \r\n " ))) {
59
+ stop(" Can only escape separators `\\ t`, `\\ n`, and `\\ r`" )
60
+ }
61
+ if (quote != " none" ) {
62
+ warning(" quotes in data will not be escaped with `escape = sep`" )
63
+ }
64
+ }
65
+
56
66
opts <- get_vroom_write_opts(quote , escape , bom )
57
67
58
68
# Standardise path returns a list, but we will only ever have 1 output file.
@@ -109,7 +119,8 @@ vroom_write_opts <- function() c(
109
119
" quote_all" = 2L ,
110
120
" escape_double" = 4L ,
111
121
" escape_backslash" = 8L ,
112
- " bom" = 16L
122
+ " bom" = 16L ,
123
+ " escape_sep" = 32L
113
124
)
114
125
115
126
# ' Convert a data frame to a delimited string
@@ -121,7 +132,7 @@ vroom_write_opts <- function() c(
121
132
# ' @inheritParams vroom_write
122
133
# ' @export
123
134
vroom_format <- function (x , delim = " \t " , eol = " \n " , na = " NA" , col_names = TRUE ,
124
- escape = c(" double" , " backslash" , " none" ),
135
+ escape = c(" double" , " backslash" , " sep " , " none" ),
125
136
quote = c(" needed" , " all" , " none" ),
126
137
bom = FALSE ,
127
138
num_threads = vroom_threads()) {
@@ -135,6 +146,15 @@ vroom_format <- function(x, delim = "\t", eol = "\n", na = "NA", col_names = TRU
135
146
quote <- match.arg(quote )
136
147
escape <- match.arg(escape )
137
148
149
+ if (escape == " sep" ) {
150
+ if (! all(c(delim , eol ) %in% c(" \t " , " \n " , " \r " , " \r\n " ))) {
151
+ stop(" Can only escape separators `\\ t`, `\\ n`, and `\\ r`" )
152
+ }
153
+ if (quote != " none" ) {
154
+ warning(" quotes in data will not be escaped with `escape = sep`" )
155
+ }
156
+ }
157
+
138
158
opts <- get_vroom_write_opts(quote , escape , bom )
139
159
140
160
# This seems to work ok in practice
0 commit comments