@@ -373,15 +373,11 @@ attempt_read <- function(fb_occurrence_obj) {
373373 fb_occurrence_obj [[" dt" ]] <- FALSE
374374 }
375375
376+ fb_occurrence_obj [[" nrows" ]] <- nlines(fb_occurrence_obj )
377+
376378 if (fb_occurrence_obj [[" count_only" ]]) {
377- ans <- nlines( fb_occurrence_obj )
379+ ans <- fb_occurrence_obj [[ " nrows " ]]
378380 } else {
379- n_rows <- NULL
380-
381- if (! identical(fb_occurrence_obj [[" n" ]], - 1L )) {
382- n_rows <- nlines(fb_occurrence_obj )
383- }
384-
385381 if (use_dt && has_pkgs(" data.table" )) {
386382 input <- as.character(fb_occurrence_obj [[" file" ]])
387383 input_list <- list (input = input , tsv = fb_occurrence_obj [[" tsv" ]])
@@ -399,7 +395,7 @@ attempt_read <- function(fb_occurrence_obj) {
399395 ans <- rd_read(fb_occurrence_obj )
400396 }
401397
402- attr(ans , " nrow" ) <- n_rows
398+ attr(ans , " nrow" ) <- fb_occurrence_obj [[ " nrows " ]]
403399 }
404400
405401 ans
@@ -720,7 +716,7 @@ dt_read <- function(fb_occurrence_obj) {
720716 vector(" list" , length(args [[" select" ]])), class = " data.frame"
721717 )
722718
723- if (file.exists(args [[" input" ]])) {
719+ if (file.exists(args [[" input" ]]) && fb_occurrence_obj [[ " nrows " ]] > 0L ) {
724720 df <- do.call(data.table :: fread , args )
725721 }
726722
@@ -787,7 +783,9 @@ rd_read <- function(fb_occurrence_obj) {
787783 skip_n <- 3
788784 }
789785
790- if (identical(n , 0L ) || inherits(con , " textConnection" )) {
786+ no_rows <- identical(fb_occurrence_obj [[" nrows" ]], 0L )
787+
788+ if (identical(n , 0L ) || inherits(con , " textConnection" ) || no_rows ) {
791789 df <- df [0L , ]
792790 } else {
793791 df <- utils :: read.delim(
@@ -805,7 +803,6 @@ rd_read <- function(fb_occurrence_obj) {
805803 for (i in seq_along(df )) {
806804 df [[i ]] <- cast_to_type(df [[i ]], classes [[i ]])
807805 }
808-
809806 }
810807
811808 idx <- ! cols %in% deselect(select )
@@ -1039,6 +1036,9 @@ nlines <- function(fb_occurrence_obj) {
10391036 on.exit(close(con ))
10401037
10411038 n <- - 1L
1039+
1040+ if (fb_occurrence_obj [[" is_dwc" ]]) n <- - 3L
1041+
10421042 cond <- ! inherits(con , " textConnection" )
10431043
10441044 while (cond ) {
0 commit comments