0

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!**

Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
  • You can call readLines directly on the file name, you don't need to open and close a connection. – Thomas Oct 09 '13 at 15:17
  • 1
    Looks like the usual vs vs line termination problem. Your original file may have been created with some Satan's Spawn like MicrosoftWord, and thus has undesirable EOL characters. – Carl Witthoft Oct 09 '13 at 15:59
  • Actually, it was created by Modelmuse, a pre- and postprocessor for the groundwater flow model Modflow (and friends). After creating similiar files using the same functionalities, I definately agree with you. It is just the way Modelmuse outputs the file. – Florian R. Klein Oct 09 '13 at 16:15

2 Answers2

0

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.

Florian R. Klein
  • 1,375
  • 2
  • 15
  • 32
0

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.

Markus G.
  • 1,620
  • 2
  • 25
  • 49