警告:关闭未使用的连接 n
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
readLines()
是一个函数,您不需要close()
它。您想要关闭由file()
函数打开的连接。此外,您在关闭任何连接之前return()
。就函数而言,return()
语句后面的行不存在。一种选择是保存
file()
调用返回的对象,因为您不应该只关闭函数打开的所有连接。这是一个非函数版本来说明这个想法:但是,要编写您的函数,我可能会采取稍微不同的策略:
其使用方式如下,上面创建的文本文件作为示例文件来读取,
我使用 < code>on.exit() 这样,一旦创建了
con
,如果函数由于某种原因终止,连接将被关闭。如果您将其留给最后一行之前的close(con)
语句,例如:该函数可能会在
readLines()
调用上失败并终止,因此连接不会被关闭。on.exit()
将安排关闭连接,即使函数提前终止也是如此。readLines()
is a function, you don'tclose()
it. You want to close the connection opened by thefile()
function. Also, you arereturn()
ing before you close any connections. As far as the function is concerned, the lines after thereturn()
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:To write your function, however, I would probably take a slightly different tack:
Which is used as follows, with the text file created above as an example file to read from
I used
on.exit()
so that oncecon
is created, if the function terminates, for whatever reason, the connection will be closed. If you left this just to aclose(con)
statement just before the last line, e.g.: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.