0

I've written the if statement below in R. It works fine, but there's a pesky warning message which I 1) don't completely understand and 2) would like to not get. :-)

With respect to 1): What does it mean

"Warning message: closing unused connection 3"

And what does the number (3) mean in this context? Googling found me some pointers (How to fix error "closing unused connection" and Warning: closing unused connection n), which do not seem to work, but I know: it's probably me. What am I doing wrong? Below's my code.

Thanks!

Sander

filetype = summary(file(opt$datagwas))$class

if(filetype == "gzfile"){
    cat("\n* The file appears to be gzipped, checking delimiter now...")
    TESTDELIMITER = readLines(opt$datagwas, n = 1)
    cat("\n* Data header looks like this:\n")
    print(TESTDELIMITER)
    if(grepl(",", TESTDELIMITER) == TRUE){
    cat("\n* Data is comma-seperated, loading...\n")
    GWASDATA_RAW = fread(paste0("zcat < ",opt$datagwas), 
    header = TRUE, sep = ",", dec = ".", 
    na.strings = c("", "NA", "na", "Na", "NaN", 
    "Nan", ".","N/A","n/a", "N/a"),
    blank.lines.skip = TRUE)
    } else {
    cat ("\n\n*** ERROR *** Something is rotten in the City of Gotham. The    GWAS data is neither comma, tab, space, nor semicolon delimited. Double     back, please.\n\n", file=stderr()) # print error messages to stder
    }
    } else if(filetype != "gzfile") {
    cat("\n* The file appears not to be gezipped, checking delimiter     now...")
    TESTDELIMITER = readLines(opt$datagwas, n = 1)
    cat("\n* Data header looks like this:\n")
    print(TESTDELIMITER)
    if(grepl(",", TESTDELIMITER) == TRUE){
    cat("\n* Data is comma-seperated, loading...\n")
    GWASDATA_RAW = fread(opt$datagwas, 
    header = TRUE, sep = ",",dec = ".",
    na.strings = c("", "NA", "na", "Na","NaN",
    "Nan", ".","N/A","n/a","N/a"),
    blank.lines.skip = TRUE)
    } else {
    cat ("\n\n*** ERROR *** Something is rotten in the City of Gotham. The     GWAS data is neither comma, tab, space, nor semicolon delimited. Double     back, please.\n\n", file=stderr()) # print error messages to stder
    }
    } else {
    cat ("\n\n*** ERROR *** Something is rotten in the City of Gotham. We     can't determine the file type of the GWAS data. Double back, please.\n\n",     file=stderr()) # print error messages to stder
    }
closeAllConnections()
Community
  • 1
  • 1

2 Answers2

3

What you should do is open the file once, and save the connection as a variable. Then when you're done using it, close the connection yourself.

# Open it
conn <- file(opt$datagwas)

# Extract the information you need
filetype <- summary(conn)$class
TESTDELIMITER <- readLines(conn, n = 1)

# Then close it
close(conn)

# Continue with if-clause, fread, etc as before

Your approach might solve this particular case. But in other cases you might be generating thousands to millions of unclosed connections by not being properly aware of them, leading perhaps to performance issues or crashes.

mpjdem
  • 1,504
  • 9
  • 14
  • But in my particular example above. Should I then close `TESTDELIMITER` - as it comes from `readLines` - and `filetype` - as it comes from `summary(file(file.txt))`? – Sander W. van der Laan Dec 07 '16 at 09:19
  • 1
    No, these variables are not connections. The connection was created on the fly, but not assigned to a variable so you have no direct way to manually close it anymore. You should do something like this: conn <- file(opt$datagwas) filetype <- summary(conn)$class TESTDELIMITER <- readLines(conn, n = 1) close(conn) # Continue with if-clause, fread, etc as before – mpjdem Dec 07 '16 at 09:28
0

Thanks to @mpjdem I made the necessary changes.

datagwas_connection <- file(opt$datagwas)
filetype <- summary(datagwas_connection)$class
TESTDELIMITER <- readLines(datagwas_connection, n = 1)
close(datagwas_connection)
if(filetype == "gzfile"){
    cat("\n* The file appears to be gzipped, checking delimiter now...")
    cat("\n* Data header looks like this:\n")
    print(TESTDELIMITER)
    if(grepl(",", TESTDELIMITER) == TRUE){
    etc etc etc (rest of the code)

This works like a charm. Thanks again!

antonio
  • 10,629
  • 13
  • 68
  • 136