In R, read.table returns error on particular opening string

Viewed 29

Working in R (either R 4.0.3 on macOS Catalina using RStudio, or R 3.3.1 on linux), when I try to import (using read.table) a text file where the first 3 chars are "BZh", and there is at least one more char after that, I get an error.

I wrote a test to look at other character combos but "BZh*" is the only one that has import.table returning an error so far. I haven't tested all of the ascii chars or anything, just the upper and lowercase letters. Here's the test I wrote:

upper_chars = unlist(strsplit("ABCDEFGHIJKLMNOPQRSTUVWXYZ", split = ""))
lower_chars = unlist(strsplit("abcdefghijklmnopqrstuvwxyz", split = ""))
outpath="test.txt"
for (char1 in upper_chars){
  for (char2 in upper_chars){
    for (char3 in lower_chars){
      test_str <- paste(char1, char2, char3, "_", sep = "")
      write.table(test_str, outpath, quote = FALSE, row.names = FALSE, col.names = FALSE)
      tryCatch({
        read.table(outpath)
      },
      error = function(cond) {
        message(test_str)
      })
    }
  }
}

The only string reporting an error is "BZh" followed by at least one other char (I've tested that 4th char as "0", "_", and the upper- and lower-case chars, and they've all caused an error in read.table). "BZh" by itself doesn't report an error. Neither do "Bzh_" nor "bzh_" nor "bzH_"

I've tried making the files in vim (instead of writing tables using write.table in R), and get the same error, though I haven't tested that as much.

0 Answers
Related