Philippe Massicotte
Philippe Massicotte

Reputation: 1529

Why file.exists(tempfile()) return FALSE?

A simple question today for which I can't find a solution. Why file.exists() returns FALSE? I have plenty of space on my HD, so I do not understand what is going on.


file.exists(tempfile())
#> [1] FALSE

Created on 2019-05-21 by the reprex package (v0.3.0)

Upvotes: 2

Views: 521

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162321

You're getting that return value because tempfile() does not itself create a file. Instead, as described in ?tempfile:

'tempfile' returns a vector of character strings which can be used as names for temporary files.

To see this yourself, try the following

## `f` is just a character string
f <- tempfile()
f
## [1] "C:\\tmp\\RtmpUdx1MU\\file26fc52b52d77"
class(f)
## [1] "character"
file.exists(f)
## [1] FALSE

## Writing something to the path given by `f` is what creates the file
cat("Hello", file = f)
file.exists(f)
## [1] TRUE

Upvotes: 4

Related Questions