Reputation: 18585
I'm writing simple code in R where I'm checking whether given file exists in the working directory and if it doesn't I'm downloading zip file with the data and unzipping it in R. As it happens, if the file exists the R objects corresponding to binary (getBinaryURL) and connection to the file are connected. I would like to remove them after successful download. I drafted those one sentence if statements but they return error Error in exists(bin) : invalid first argument. It's intermediately obvious to me what's wrong with the syntax.
if (exists(bin)) rm(bin)
if (exists(con)) rm(con)
if (exists(dataurl)) rm(dataurl)
Upvotes: 5
Views: 18584
Reputation: 18602
Your if()
statement isn't the problem, you need to quote the object passed to exists()
.
> AnObject <- seq(1:10)
> exists(AnObject)
Error in exists(AnObject) : invalid first argument
> exists("AnObject")
[1] TRUE
Upvotes: 13