警告:关闭未使用的连接 n

发布于 2024-11-14 23:19:53 字数 327 浏览 2 评论 0原文

getCommentary=function(){
    Commentary=readLines(file("C:\\Commentary\\com.txt"))
    return(Commentary)
    close(readLines)
    closeAllConnections()
}

我不知道这个功能有什么问题。当我在 R 中运行它时,它不断向我发出以下警告:

Warning message:
closing unused connection 5 ("C:\\Commentary\\com.txt") 
getCommentary=function(){
    Commentary=readLines(file("C:\\Commentary\\com.txt"))
    return(Commentary)
    close(readLines)
    closeAllConnections()
}

I have no idea what is wrong with this function. When I run this in R, it keeps giving me the following warning:

Warning message:
closing unused connection 5 ("C:\\Commentary\\com.txt") 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2024-11-21 23:19:53

readLines() 是一个函数,您不需要 close() 它。您想要关闭由 file() 函数打开的连接。此外,您在关闭任何连接之前return()。就函数而言,return() 语句后面的行不存在。

一种选择是保存 file() 调用返回的对象,因为您不应该只关闭函数打开的所有连接。这是一个非函数版本来说明这个想法:

R> cat("foobar\n", file = "foo.txt")
R> con <- file("foo.txt")
R> out <- readLines(con)
R> out
[1] "foobar"
R> close(con)

但是,要编写您的函数,我可能会采取稍微不同的策略:

getCommentary <- function(filepath) {
    con <- file(filepath)
    on.exit(close(con))
    Commentary <-readLines(con)
    Commentary
}

其使用方式如下,上面创建的文本文件作为示例文件来读取,

R> getCommentary("foo.txt")
[1] "foobar"

我使用 < code>on.exit() 这样,一旦创建了 con,如果函数由于某种原因终止,连接将被关闭。如果您将其留给最后一行之前的 close(con) 语句,例如:

    Commentary <-readLines(con)
    close(con)
    Commentary
}

该函数可能会在 readLines() 调用上失败并终止,因此连接不会被关闭。 on.exit() 将安排关闭连接,即使函数提前终止也是如此。

readLines() is a function, you don't close() it. You want to close the connection opened by the file() function. Also, you are return()ing before you close any connections. As far as the function is concerned, the lines after the return() statement don't exist.

One option is to save the object returned by the file() call, as you shouldn't be closing all connections only those your function opens. Here is a non-function version to illustrate the idea:

R> cat("foobar\n", file = "foo.txt")
R> con <- file("foo.txt")
R> out <- readLines(con)
R> out
[1] "foobar"
R> close(con)

To write your function, however, I would probably take a slightly different tack:

getCommentary <- function(filepath) {
    con <- file(filepath)
    on.exit(close(con))
    Commentary <-readLines(con)
    Commentary
}

Which is used as follows, with the text file created above as an example file to read from

R> getCommentary("foo.txt")
[1] "foobar"

I used on.exit() so that once con is created, if the function terminates, for whatever reason, the connection will be closed. If you left this just to a close(con) statement just before the last line, e.g.:

    Commentary <-readLines(con)
    close(con)
    Commentary
}

the function could fail on the readLines() call and terminate, so the connection would not be closed. on.exit() would arrange for the connection to be closed, even if the function terminates early.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文