我在R中写了下面的if语句。它工作正常,但有一个令人讨厌的警告信息,我1)不完全理解,2)想不得到。 : - )
关于1):它是什么意思
"警告消息:关闭未使用的连接3"
在这种情况下,数字(3)的含义是什么? 谷歌搜索给了我一些指针(How to fix error "closing unused connection"和Warning: closing unused connection n),它们似乎不起作用,但我知道:它可能是我。我究竟做错了什么? 下面是我的代码。
谢谢!
桑德
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()
答案 0 :(得分:2)
您应该做的是打开文件一次,并将连接保存为变量。然后,当您使用完毕后,请自行关闭连接。
# 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
您的方法可能会解决此特定情况。但在其他情况下,如果没有正确识别它们,可能会产生数千到数百个未闭合的连接,从而导致性能问题或崩溃。
答案 1 :(得分:0)
感谢@mpjdem,我做了必要的修改。
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){
等等等(代码的其余部分)
这就像一个魅力。再次感谢!