Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: could read_lines() accept negative n_max, to read from end of file #983

Closed
dan-reznik opened this issue Mar 29, 2019 · 3 comments

Comments

@dan-reznik
Copy link

dan-reznik commented Mar 29, 2019

I would like to do:

read_lines('fname.txt', n_max=-2) 

to read just the last line (since n_max=-1 currently reads the entire file). or

read_lines('fname.txt', n_max=-N)

to read the last N-1 lines in the file. this way i don't have to know how many lines there are, skip them, etc.

@jimhester
Copy link
Collaborator

While interesting, I don't think this is likely to be implemented. I would suggest you use tail(n = N, vroom::vroom_lines('fname.txt'))

@dan-reznik
Copy link
Author

dan-reznik commented Apr 3, 2020 via email

@USMortality
Copy link

USMortality commented Nov 6, 2023

Possible workaround:

To get the number of lines in a file without reading all its contents into memory in R, you can use the count.fields() function. This function counts the number of fields (columns) in each line of a file and returns a vector with the count for each line. By taking the length of this vector, you can determine the number of lines in the file.

Here's an example of how to use count.fields() to get the number of lines in a file:

line_count <- sum(count.fields("path/to/file.csv"))

Please note that count.fields() may read the file partially, but it does not load the entire file into memory. It only reads enough of the file to determine the number of fields in each line.

https://stackoverflow.com/questions/39691133/counting-rows-with-fread-without-reading-the-whole-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants