11

我正在通过创建文件连接,path <- file("C:/test.txt")并且在打印与连接关联的对象时,我可以看到连接的“属性”:

> path
  description         class          mode          text        opened 
"C:/test.txt"        "file"           "r"        "text"      "closed" 
     can read     can write 
        "yes"         "yes" 

但是,我似乎无法弄清楚如何实际访问各种属性值

这是我到目前为止所尝试的:

> attributes(path)
$class
[1] "file"       "connection"

$conn_id
<pointer: 0x0000004b>

> path$description
Error in path$description : $ operator is invalid for atomic vectors

> path["description"]
[1] NA

> file.info(path)
Error in file.info(path) : invalid filename argument

有任何想法吗?

4

2 回答 2

13

快速浏览一下base:::print.connection就会发现您想要summary(path).

summary(path)
$description
[1] "C:/test.txt"

$class
[1] "file"

$mode
[1] "r"

$text
[1] "text"

$opened
[1] "closed"

$`can read`
[1] "yes"

$`can write`
[1] "yes"
于 2012-08-28T11:50:14.523 回答
1

我能得到的最接近你想要的是使用summary()。例如:

summary(path)$mode
[1] "rt"

使用file.info()的错误是因为该函数需要文件的路径,“C:/test.txt”作为其参数。

于 2012-08-28T11:51:13.670 回答