Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line if statement in R - invalid first argument

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)
like image 374
Konrad Avatar asked Jul 16 '14 17:07

Konrad


1 Answers

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
like image 123
nrussell Avatar answered Nov 15 '22 20:11

nrussell