Question

I have encountered some strange behaviour of the readLines()-function which is basically included for all R-runs. As I have done it houndreds or thousands of times before, I tried to read the lines of a file like this:

workingDir <- ""

tempFile <- file(paste(workingDir, "/stationaer_mittel004_head3D.csv", sep="", collapse=""), open="r")
s_mittel001_head <- readLines(tempFile)
close(tempFile)
s_mittel001_head

This does not read the file content properly and results in something like this:

[1] "\"" ""   ""   ""   ""

I have double- and tripöle and quadruple-checked if I have done something wrong with the filename but it works just fine when accessing the file in a file- or webbrowser. Subsequently, I plainly copied the file content to another csv and tried to open this one. It actually worked. I am on the administrive account of my laptop and both files show no special permission restrictions. Creating a new file with the same filename solves the issue, too.

**That's why I would like to know if somebody knows what might cause this behaviour.

Thank you!**

Était-ce utile?

La solution

As @CarlWitthoft pointed out, readLines() has problems reading ASCII-files with certain linebreak-types (don't know which exactly: vs vs ). This results in a list of practically empty strings. Furthermore, the function seemingly is unable to see the end of the file.

The final solution: Copy and paste the file content in a new file using a program with "more appropiate" linebreak-standard.

Autres conseils

For me this lead me to this post https://github.com/tidyverse/readr/issues/857 where the comment by mcleanle set me down the right path.
The root cause was actually the encoding language. My file was encoded as UCS-2 LE BOM, changing to UTF-8 fixed this for me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top