【问题标题】:How to suppress warning message in R regarding closing unused connection?如何抑制 R 中有关关闭未使用连接的警告消息?
【发布时间】:2021-09-24 08:52:17
【问题描述】:

我已经在 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()

【问题讨论】:

    标签: r


    【解决方案1】:

    您应该做的是打开文件一次,然后将连接保存为变量。然后当你用完后,自己关闭连接。

    # 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
    

    您的方法可能会解决这种特殊情况。但在其他情况下,您可能会因为没有正确意识到它们而生成数千到数百万个未关闭的连接,从而可能导致性能问题或崩溃。

    【讨论】:

    • 但在我上面的特定示例中。然后我应该关闭TESTDELIMITER - 因为它来自readLines - 和filetype - 因为它来自summary(file(file.txt))
    • 不,这些变量不是连接。连接是即时创建的,但没有分配给变量,因此您无法再直接手动关闭它。你应该这样做: conn
    【解决方案2】:

    感谢@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){
        etc etc etc (rest of the code)
    

    这就像一个魅力。再次感谢!

    【讨论】:

      猜你喜欢
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      相关资源
      最近更新 更多