Accessing the attributes of a file connection created via file()

1.2k views Asked by At

I'm creating a file connection via path <- file("C:/test.txt") and when printing the object associated to the connection I can see the connection's "attributes":

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

However, I can't seem to figure out how to actually access the various attribute values

Here's what I tried so far:

> 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

Any ideas?

2

There are 2 answers

4
Josh O'Brien On BEST ANSWER

A quick look at base:::print.connection will show that you want 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"
1
neilfws On

The closest I can get to what you want is to use summary(). For example:

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

The error using file.info() is because that function expects the path to the file, i.e. "C:/test.txt", as its argument.