Search code examples
rfileconnection-stringfile-connection

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


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?


Solution

  • 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"