One line if statement in R - invalid first argument

exists, if-statement, object, r

Solution

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

Problem

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) ```

Original source