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

read_file on empty file throws error #356

Closed
torbjorn opened this Issue Jan 29, 2016 · 4 comments

Comments

Projects
None yet
3 participants
@torbjorn

torbjorn commented Jan 29, 2016

I'd expect to get an empty string, but instead get:
Error: Cannot read file /tmp/RtmpFO5GFJ/file4dc73de7d6a2.txt

This could be what you designed it to do, but its arguably not what most users would expect from this function, and it should at least be documented (and tested for)

My testcase:

f <- tempfile( fileext=".txt" )
cat( "", file=f )
expect_true( file.exists(f) )
expect_that(
    read.file( f ),
    is_identical_to( "" )
)
@torbjorn

This comment has been minimized.

torbjorn commented Mar 4, 2016

I see that most of the function is written in c, which I normally couldn't wrap my head around, but maybe simply checking filesize in plain R code would suffice to fix this?

@hadley

This comment has been minimized.

Member

hadley commented Jun 1, 2016

Slightly different case of #378.

Need to think if read_csv() on empty file should return 0 x 0 data frame

@torbjorn

This comment has been minimized.

torbjorn commented Jun 2, 2016

I believe that would be to overcomplicate things.

read_csv implies interpretation and perhaps a format (ie a csv table), read_file does not (not in the name at least).

.. and also not in the manual page, as it simply says:
"Read a file into a string."

"" or character() seems like the obvious result of reading an empty file, and the error reserved from when the file does not exist or otherwise cannot be read.

In the bigger scheme of things, something in the Rniverse should be able to read a file into a string. This is a natural thing to do, all the other cool scripting languages on the block have this. readr::read_file seems like the perfect fit, I (from my point of view) see no reason why its other friends in the readr package should impose roles on it.

@hadley

This comment has been minimized.

Member

hadley commented Jun 8, 2016

  • read_file() should return "'
  • read_lines() should return character()
  • read_csv() etc return a 0x0 data frame
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.