@@ -94,7 +94,7 @@ range_read <- function(ss,
9494 guess_max = min(1000 , n_max ),
9595 .name_repair = " unique" ) {
9696 # check these first, so we don't download cells in vain
97- col_spec <- standardise_col_spec(col_names , col_types )
97+ col_spec <- standardise_col_spec(col_names , col_types , call = current_env() )
9898 check_character(na )
9999 check_bool(trim_ws )
100100 check_non_negative_integer(guess_max )
@@ -150,7 +150,7 @@ spread_sheet <- function(df,
150150 na = " " , trim_ws = TRUE ,
151151 guess_max = min(1000 , max(df $ row )),
152152 .name_repair = " unique" ) {
153- col_spec <- standardise_col_spec(col_names , col_types )
153+ col_spec <- standardise_col_spec(col_names , col_types , call = current_env() )
154154 check_character(na )
155155 check_bool(trim_ws )
156156 check_non_negative_integer(guess_max )
@@ -168,7 +168,8 @@ spread_sheet_impl_ <- function(df,
168168 ),
169169 na = " " , trim_ws = TRUE ,
170170 guess_max = min(1000 , max(df $ row )),
171- .name_repair = " unique" ) {
171+ .name_repair = " unique" ,
172+ call = caller_env()) {
172173 if (nrow(df ) == 0 ) {
173174 return (tibble :: tibble())
174175 }
@@ -186,7 +187,12 @@ spread_sheet_impl_ <- function(df,
186187 if (is.logical(col_names )) {
187188 # if col_names is logical, this is first chance to check/set length of
188189 # ctypes, using the cell data
189- ctypes <- rep_ctypes(max(df $ col ), ctypes , " column{?s} found in sheet" )
190+ ctypes <- rep_ctypes(
191+ max(df $ col ),
192+ ctypes ,
193+ " column{?s} found in sheet" ,
194+ call = call
195+ )
190196 }
191197
192198 # drop cells in skipped cols, update df$col and ctypes
@@ -201,11 +207,14 @@ spread_sheet_impl_ <- function(df,
201207 # if column names were provided explicitly, we need to check that length
202208 # of col_names (and, therefore, ctypes) == nc
203209 if (is.character(col_names ) && length(col_names ) != nc ) {
204- gs4_abort(c(
205- " Length of {.arg col_names} is not compatible with the data:" ,
206- " *" = " {.arg col_names} has length {length(col_names)}." ,
207- x = " But data has {nc} un-skipped column{?s}."
208- ))
210+ gs4_abort(
211+ c(
212+ " Length of {.arg col_names} is not compatible with the data:" ,
213+ " *" = " {.arg col_names} has length {length(col_names)}." ,
214+ " x" = " But data has {nc} un-skipped column{?s}."
215+ ),
216+ call = call
217+ )
209218 }
210219
211220 df $ cell <- apply_ctype(df $ cell , na = na , trim_ws = trim_ws )
@@ -236,58 +245,71 @@ spread_sheet_impl_ <- function(df,
236245
237246# # helpers ---------------------------------------------------------------------
238247
239- standardise_col_spec <- function (col_names , col_types ) {
240- check_col_names(col_names )
241- ctypes <- standardise_ctypes(col_types )
248+ standardise_col_spec <- function (col_names , col_types , call = caller_env() ) {
249+ check_col_names(col_names , call = call )
250+ ctypes <- standardise_ctypes(col_types , call = call )
242251 if (is.character(col_names )) {
243- ctypes <- rep_ctypes(length(col_names ), ctypes , " column name{?s}" )
252+ ctypes <- rep_ctypes(
253+ length(col_names ),
254+ ctypes ,
255+ " column name{?s}" ,
256+ call = call
257+ )
244258 col_names <- filter_col_names(col_names , ctypes )
245259 # if column names were provided explicitly, this is now true
246260 # length(col_names) == length(ctypes[ctypes != "COL_SKIP"])
247261 }
248262 list (col_names = col_names , ctypes = ctypes )
249263}
250264
251- check_col_names <- function (col_names ) {
265+ check_col_names <- function (col_names , call = caller_env() ) {
252266 if (is.logical(col_names )) {
253- return (check_bool(col_names ))
267+ return (check_bool(col_names , call = call ))
254268 }
255- check_character(col_names )
256- check_has_length(col_names )
269+ check_character(col_names , call = call )
270+ check_has_length(col_names , call = call )
257271}
258272
259273# input: a string of readr-style shortcodes or NULL
260274# output: a vector of col types of length >= 1
261- standardise_ctypes <- function (col_types ) {
275+ standardise_ctypes <- function (col_types , call = caller_env() ) {
262276 col_types <- col_types %|| % " ?"
263- check_string(col_types )
277+ check_string(col_types , call = call )
264278
265279 if (identical(col_types , " " )) {
266280 gs4_abort("
267281 {.arg col_types}, when provided, must be a string that contains at \\
268- least one readr-style shortcode." )
282+ least one readr-style shortcode." ,
283+ call = call
284+ )
269285 }
270286
271287 accepted_codes <- keep(names(.ctypes ), nzchar )
272288
273289 col_types_split <- strsplit(col_types , split = " " )[[1 ]]
274290 ok <- col_types_split %in% accepted_codes
275291 if (! all(ok )) {
276- gs4_abort(c(
277- " {.arg col_types} must be a string of readr-style shortcodes. \\
278- Unrecognized code{?s}{cli::qty(sum(!ok))}:" ,
279- bulletize(gargle_map_cli(col_types_split [! ok ]), bullet = " x" )
280- ))
292+ gs4_abort(
293+ c(
294+ " {.arg col_types} must be a string of readr-style shortcodes. \\
295+ Unrecognized code{?s}{cli::qty(sum(!ok))}:" ,
296+ bulletize(gargle_map_cli(col_types_split [! ok ]), bullet = " x" )
297+ ),
298+ call = call
299+ )
281300 }
282301 ctypes <- ctype(col_types_split )
283302 if (all(ctypes == " COL_SKIP" )) {
284- gs4_abort(" {.arg col_types} can't request that all columns be skipped." )
303+ gs4_abort(
304+ " {.arg col_types} can't request that all columns be skipped." ,
305+ call = call
306+ )
285307 }
286308 ctypes
287309}
288310
289311# makes sure there are n ctypes or n ctypes that are not COL_SKIP
290- rep_ctypes <- function (n , ctypes , comparator = " n" ) {
312+ rep_ctypes <- function (n , ctypes , comparator = " n" , call = caller_env() ) {
291313 if (length(ctypes ) == n ) {
292314 return (ctypes )
293315 }
@@ -302,12 +324,15 @@ rep_ctypes <- function(n, ctypes, comparator = "n") {
302324 # column{?s} found in sheet
303325 # column name{?s}
304326 comparator <- cli :: pluralize(sprintf(" {cli::qty(n)}%s{?s}" , comparator ))
305- gs4_abort(c(
306- " Length of {.arg col_types} is not compatible with {comparator}:" ,
307- x = " {length(ctypes)} column type{?s} specified." ,
308- x = " {n_col_types} un-skipped column type{?s} specified." ,
309- x = " But there {cli::qty(n)}{?is/are} {n} {comparator}."
310- ))
327+ gs4_abort(
328+ c(
329+ " Length of {.arg col_types} is not compatible with {comparator}:" ,
330+ x = " {length(ctypes)} column type{?s} specified." ,
331+ x = " {n_col_types} un-skipped column type{?s} specified." ,
332+ x = " But there {cli::qty(n)}{?is/are} {n} {comparator}."
333+ ),
334+ call = call
335+ )
311336}
312337
313338# removes col_names for skipped columns
0 commit comments