You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
blank.lines.skip won't work when the top lines is empty #5611
We should probably update the documentation that blank lines at the beginning of a file are always skipped until the first non-empty row is encountered. This is also mentioned by the verbose output (shortened it to highlight the important part).
library(data.table)
file2<- tempfile()
writeLines(c("", "", "a"), file2)
fread(file2, blank.lines.skip=FALSE, verbose=TRUE)
#> [05] Skipping initial rows if needed#> Positioned on line 3 starting: <<a>>
I get that but changing the behavior of blank.lines.skip = FALSE would probably break a lot of existing code so I doubt we will do that.
Possible changes I could imagine would be to introduce another argument or let blank.lines.skip accept other values to indicate you do not want to skip blank lines at the beginning.
Do we see a purpose of an additional argument to turn off the initial skipping of lines? Or is a documentation update fine to close this issue, shouldn't be difficult to implement regardless.
fread has already so many arguments so I wouldn't want to introduce a new argument for that specific case. What do you think blank.lines.skip=NULL for indicating this behavior? Other free value of NA seems off, although I would ultimately tend towards blank.lines.skip="none" or something similar
@ben-schwen I did some initial prototyping of a change and here's what I found:
It seems that to make this behavior work in the most intuitive way, (blank.lines.skip="none" doesn't skip any blank lines, for any number of columns) there's likely a lot more work involved as we have to rework how fread() does skipping blank lines with more than 1 column. Not sure if this one use case is enough to warrant this.
Currently, blank.lines.skip=FALSE is ignored if we have more than 1 column of data (not sure if this is documented): #6158 (comment)
If we make it so blank.lines.skip="none" only works with 1 column which is consistent with current blank.lines.skip=FALSE, then current changes should be most of the way there, barring some more testing. This would solve the MRE posted with the original issue, but I'm pretty sure OP intends to use this feature with data that has more than 1 column, which wouldn't be covered.
ATP, I feel a documentation change is the way to go. If we want to cover the MRE from the issue, the change is almost there. WDYT?
Tough question. Files with multiple columns and complete blank lines are clearly misspecified, so it's hard to guess what the intended behavior should be.
Does it play nice with complete blank lines, multiple columns and fill=TRUE?
f= tempfile()
input= c("a,b", ",", ",")
writeLines(input, f)
fread(f, blank.lines.skip=FALSE, fill=TRUE)
# a b# 1: NA NA# 2: NA NA
Seems to work, I think fill is what we needed.
I could initialize fill=TRUE whenever blank.lines.skip="none"?
Edit:
The above does work but only because column headers are specified. If the first line is empty, fread doesn't have a good way of defining columns, which is why the default behavior is to skip blanks until we find a non-empty line so we can define columns. If I bypass the skipping of the first blanks, fread just assumes the file to have one column, see:
f= tempfile()
input="\n\n\n\n1,3\n\n2,4\n\n"
writeLines(input, f)
fread(f, blank.lines.skip="none", fill=TRUE)
# V1 # 1: NA # 2: NA # --- # 8: NA # 9: NA
Following up on this issue -- I figured that skipping the initial blank lines isn't quite so trivial because AFAIU fread uses the first non-empty line to determine the columns of the result. Do we think this is still worth pursuing to handle this case?
Hmm, it seems fread() is not exactly suitable for this. For example, we will attempt to find free-text fields that include newlines but are otherwise properly quoted & can thus be understood as a "rectangular" table.
If we simply want to count the number of newlines, this SO Q&A look pretty good to me (and adjustments to respect \r-delimited lines are not too hard):
Activity
Yunuuuu commentedon Nov 3, 2023
Add a reprex, and use
readLines()to see the differenceCreated on 2023-11-03 with reprex v2.0.2
ben-schwen commentedon Dec 30, 2023
We should probably update the documentation that blank lines at the beginning of a file are always skipped until the first non-empty row is encountered. This is also mentioned by the verbose output (shortened it to highlight the important part).
Yunuuuu commentedon Dec 31, 2023
It’s not the document, we want to completely turn off the skipping of blank lines
ben-schwen commentedon Dec 31, 2023
I get that but changing the behavior of blank.lines.skip = FALSE would probably break a lot of existing code so I doubt we will do that.
Possible changes I could imagine would be to introduce another argument or let blank.lines.skip accept other values to indicate you do not want to skip blank lines at the beginning.
joshhwuu commentedon May 27, 2024
Do we see a purpose of an additional argument to turn off the initial skipping of lines? Or is a documentation update fine to close this issue, shouldn't be difficult to implement regardless.
ben-schwen commentedon May 27, 2024
freadhas already so many arguments so I wouldn't want to introduce a new argument for that specific case. What do you thinkblank.lines.skip=NULLfor indicating this behavior? Other free value ofNAseems off, although I would ultimately tend towardsblank.lines.skip="none"or something similarjoshhwuu commentedon Jun 4, 2024
@ben-schwen I did some initial prototyping of a change and here's what I found:
It seems that to make this behavior work in the most intuitive way, (
blank.lines.skip="none"doesn't skip any blank lines, for any number of columns) there's likely a lot more work involved as we have to rework howfread()does skipping blank lines with more than 1 column. Not sure if this one use case is enough to warrant this.Currently,
blank.lines.skip=FALSEis ignored if we have more than 1 column of data (not sure if this is documented):#6158 (comment)
If we make it so
blank.lines.skip="none"only works with 1 column which is consistent with currentblank.lines.skip=FALSE, then current changes should be most of the way there, barring some more testing. This would solve the MRE posted with the original issue, but I'm pretty sure OP intends to use this feature with data that has more than 1 column, which wouldn't be covered.ATP, I feel a documentation change is the way to go. If we want to cover the MRE from the issue, the change is almost there. WDYT?
ben-schwen commentedon Jun 4, 2024
Tough question. Files with multiple columns and complete blank lines are clearly misspecified, so it's hard to guess what the intended behavior should be.
Does it play nice with complete blank lines, multiple columns and
fill=TRUE?joshhwuu commentedon Jun 4, 2024
Seems to work, I think fill is what we needed.
I could initialize
fill=TRUEwheneverblank.lines.skip="none"?Edit:
The above does work but only because column headers are specified. If the first line is empty,
freaddoesn't have a good way of defining columns, which is why the default behavior is to skip blanks until we find a non-empty line so we can define columns. If I bypass the skipping of the first blanks,freadjust assumes the file to have one column, see:joshhwuu commentedon Jul 2, 2024
Following up on this issue -- I figured that skipping the initial blank lines isn't quite so trivial because AFAIU
freaduses the first non-empty line to determine the columns of the result. Do we think this is still worth pursuing to handle this case?MichaelChirico commentedon Jul 3, 2024
Hmm, it seems
fread()is not exactly suitable for this. For example, we will attempt to find free-text fields that include newlines but are otherwise properly quoted & can thus be understood as a "rectangular" table.If we simply want to count the number of newlines, this SO Q&A look pretty good to me (and adjustments to respect
\r-delimited lines are not too hard):https://stackoverflow.com/a/23456450/3576984
As to desired
fread()behavior... still not sure... what's the interaction withheader=FALSEfor the examples here?joshhwuu commentedon Jul 3, 2024