32
32
return (g )
33
33
}
34
34
35
-
36
-
37
35
# # Based on readxl, although the implementation is different.
38
36
# # If max row is -1, read to end of row.
39
37
# # Row and column-numbers are 1-based
40
- .standardise_limits <- function (range , skip ) {
38
+ .standardise_limits <- function (range , skip , n_max ) {
41
39
if (is.null(range )) {
42
40
skip <- check_nonnegative_integer(x = skip , argument = " skip" )
41
+ n_max <- check_nonnegative_integer(x = n_max , argument = " n_max" )
42
+ if (n_max == Inf ) {
43
+ max_row <- - 1
44
+ } else {
45
+ max_row <- n_max + 1
46
+ }
43
47
limits <- c(
44
48
min_row = skip + 1 ,
45
- max_row = - 1 ,
49
+ max_row = max_row ,
46
50
min_col = 1 ,
47
51
max_col = - 1
48
52
)
49
53
} else {
50
- if (skip != 0 ) {
51
- warning(" Range and non-zero value for skip given. Defaulting to range." , call. = FALSE )
54
+ if (skip != 0 || n_max != Inf ) {
55
+ warning(" Range and non-default value for skip or n_max given. Defaulting to range." , call. = FALSE )
52
56
}
53
57
tryCatch({
54
- limits <- cellranger :: as.cell_limits(range )
58
+ limits <- cellranger :: as.cell_limits(range )
55
59
}, error = function (e ) {
56
60
stop(" Invalid `range`" )
57
61
})
82
86
row_names = FALSE ,
83
87
strings_as_factors = FALSE ,
84
88
verbose = FALSE ,
85
- as_tibble = TRUE ) {
89
+ as_tibble = TRUE ,
90
+ trim_ws = TRUE ,
91
+ n_max = Inf ) {
86
92
if (! file.exists(path )) {
87
93
stop(" file does not exist" , call. = FALSE )
88
94
}
104
110
if (! is.logical(as_tibble )) {
105
111
stop(" as_tibble must be of type `boolean" , call. = FALSE )
106
112
}
113
+ if (! is.logical(trim_ws )) {
114
+ stop(" trim_ws must be of type `boolean" , call. = FALSE )
115
+ }
107
116
if (row_names && as_tibble ) {
108
117
stop(" Tibbles do not support row names. To use row names, set as_tibble to false" , call. = FALSE )
109
118
}
115
124
stop(" Unknown col_types. Can either be a class col_spec, list, character, NULL or NA." ,
116
125
call. = FALSE )
117
126
}
127
+ if (! is.numeric(n_max )) {
128
+ stop(" n_max must be numeric." , call. = FALSE )
129
+ }
118
130
}
119
131
120
132
.return_empty <- function (as_tibble = FALSE ) {
127
139
return (data.frame ())
128
140
}
129
141
130
- .type_convert <- function (df , col_types = NULL , verbose = TRUE , na = c(" " , " NA" )) {
142
+ .type_convert <- function (df , col_types = NULL , verbose = TRUE , na = c(" " , " NA" ), trim_ws = TRUE ) {
131
143
if (verbose ) {
132
144
res <- readr :: type_convert(df = df , col_types , na = na )
133
145
} else {
134
146
suppressMessages({
135
- res <- readr :: type_convert(df = df , col_types , na = na )
147
+ res <- readr :: type_convert(df = df , col_types , na = na , trim_ws = trim_ws )
136
148
})
137
149
}
138
150
return (res )
139
151
}
140
152
141
- .handle_col_types <- function (res , col_types , verbose , na ) {
153
+ .handle_col_types <- function (res , col_types , verbose , na , trim_ws ) {
142
154
if (isTRUE(is.na(col_types )) || nrow(res ) == 0 ) {
143
155
return (res )
144
156
}
145
- .type_convert(df = res , col_types = col_types , verbose = verbose , na = na )
157
+ .type_convert(df = res , col_types = col_types , verbose = verbose , na = na , trim_ws = trim_ws )
146
158
}
147
159
148
160
# # standardise `sheet` parameter as a number, i.e. sheet_index
173
185
}
174
186
175
187
.read_ods <- function (path ,
176
- sheet = 1 ,
177
- col_names = TRUE ,
178
- col_types = NULL ,
179
- na = " " ,
180
- skip = 0 ,
181
- formula_as_formula = FALSE ,
182
- range = NULL ,
183
- row_names = FALSE ,
184
- strings_as_factors = FALSE ,
185
- verbose = FALSE ,
186
- as_tibble = TRUE ,
187
- .name_repair = " unique" ,
188
- flat = FALSE ) {
188
+ sheet = 1 ,
189
+ col_names = TRUE ,
190
+ col_types = NULL ,
191
+ na = " " ,
192
+ skip = 0 ,
193
+ formula_as_formula = FALSE ,
194
+ range = NULL ,
195
+ row_names = FALSE ,
196
+ strings_as_factors = FALSE ,
197
+ verbose = FALSE ,
198
+ as_tibble = TRUE ,
199
+ .name_repair = " unique" ,
200
+ flat = FALSE ,
201
+ trim_ws = TRUE ,
202
+ n_max = Inf ) {
189
203
.check_read_args(path ,
190
204
sheet ,
191
205
col_names ,
197
211
row_names ,
198
212
strings_as_factors ,
199
213
verbose ,
200
- as_tibble )
214
+ as_tibble ,
215
+ trim_ws ,
216
+ n_max )
201
217
path <- normalizePath(path )
202
218
if (flat ) {
203
219
.get_sheet_names_func <- get_flat_sheet_names_
207
223
.read_ods_func <- read_ods_
208
224
}
209
225
# # Get cell range info
210
- limits <- .standardise_limits(range , skip )
226
+ limits <- .standardise_limits(range , skip , n_max )
211
227
sheet_index <- .standardise_sheet(sheet = sheet , sheet_names = .get_sheet_names_func(file = path , include_external_data = TRUE ),
212
228
range = range )
213
229
strings <- .read_ods_func(file = path ,
233
249
byrow = TRUE ),
234
250
stringsAsFactors = FALSE )
235
251
res <- .change_df_with_col_row_header(x = res , col_header = col_names , row_header = row_names , .name_repair = .name_repair )
236
- res <- .handle_col_types(res , col_types = col_types , verbose = verbose , na = na )
252
+ res <- .handle_col_types(res , col_types = col_types , verbose = verbose , na = na , trim_ws = trim_ws )
237
253
if (strings_as_factors ) {
238
254
res <- .convert_strings_to_factors(df = res )
239
255
}
300
316
# ' Default is `"unique"`.
301
317
# '
302
318
# ' @param ods_format character, must be "auto", "ods" or "fods". The default "auto" is to determine the format automatically. By default, the format is determined by file extension, unless `guess` is `FALSE`.
303
- # ' @param guess logical. If the file extension is absent or not recognized, this
319
+ # ' @param guess logical, If the file extension is absent or not recognized, this
304
320
# ' controls whether we attempt to guess format based on the file signature or
305
321
# ' "magic number".
322
+ # ' @param trim_ws logical, should leading and trailing whitespace be trimmed?
323
+ # ' @param n_max numeric, Maximum number of data rows to read. Ignored if `range` is given.
306
324
# ' @return A tibble (\code{tibble}) or data frame (\code{data.frame}) containing a representation of data in the (f)ods file.
307
325
# ' @author Peter Brohan <peter.brohan+cran@@gmail.com>, Chung-hong Chan <chainsawtiney@@gmail.com>, Gerrit-Jan Schutten <phonixor@@gmail.com>
308
326
# ' @examples
@@ -343,7 +361,9 @@ read_ods <- function(path,
343
361
as_tibble = TRUE ,
344
362
.name_repair = " unique" ,
345
363
ods_format = c(" auto" , " ods" , " fods" ),
346
- guess = FALSE ) {
364
+ guess = FALSE ,
365
+ trim_ws = TRUE ,
366
+ n_max = Inf ) {
347
367
ods_format <- .determine_ods_format(path = path , guess = guess , ods_format = match.arg(ods_format ))
348
368
# # Should use match.call but there's a weird bug if one of the variable names is 'file'
349
369
.read_ods(path = path ,
@@ -359,24 +379,28 @@ read_ods <- function(path,
359
379
verbose = verbose ,
360
380
as_tibble = as_tibble ,
361
381
.name_repair = .name_repair ,
362
- flat = ods_format == " fods" )
382
+ flat = ods_format == " fods" ,
383
+ trim_ws = trim_ws ,
384
+ n_max = n_max )
363
385
}
364
386
365
387
# ' @rdname read_ods
366
388
# ' @export
367
389
read_fods <- function (path ,
368
- sheet = 1 ,
369
- col_names = TRUE ,
370
- col_types = NULL ,
371
- na = " " ,
372
- skip = 0 ,
373
- formula_as_formula = FALSE ,
374
- range = NULL ,
375
- row_names = FALSE ,
376
- strings_as_factors = FALSE ,
377
- verbose = FALSE ,
378
- as_tibble = TRUE ,
379
- .name_repair = " unique" ) {
390
+ sheet = 1 ,
391
+ col_names = TRUE ,
392
+ col_types = NULL ,
393
+ na = " " ,
394
+ skip = 0 ,
395
+ formula_as_formula = FALSE ,
396
+ range = NULL ,
397
+ row_names = FALSE ,
398
+ strings_as_factors = FALSE ,
399
+ verbose = FALSE ,
400
+ as_tibble = TRUE ,
401
+ .name_repair = " unique" ,
402
+ trim_ws = TRUE ,
403
+ n_max = Inf ) {
380
404
# # Should use match.call but there's a weird bug if one of the variable names is 'file'
381
405
.read_ods(path = normalizePath(path , mustWork = FALSE ),
382
406
sheet = sheet ,
@@ -391,5 +415,7 @@ read_fods <- function(path,
391
415
verbose = verbose ,
392
416
as_tibble = as_tibble ,
393
417
.name_repair = .name_repair ,
394
- flat = TRUE )
418
+ flat = TRUE ,
419
+ trim_ws = trim_ws ,
420
+ n_max = n_max )
395
421
}
0 commit comments