简体   繁体   中英

File.exists() doesn't work

I have to be either blind or stupid, but I cannot move forward with this:

I have a variable sourceName defined as:

sourceName = file.path(dataDatabase, dates[1], paste0(exchange, "_", ticker, "_trade.csv"))

which is this string:

 "/home/malejg/goxtrader/data/2014-12-12/Bitfinex_BTC_trade.csv"

When I check the existenece of the file, I got the following result:

> file.exists(sourceName)
> [1] TRUE

so the file definitely exists. But when I use the above code in the if statement going like:

 if(file.exists(sourceName)){
    some sample code here}

it throws an error:

 Error in if (file.exists(sourceName)) { : argument is of length zero

How is it possible? The code works fine on Windows, so is there some Linux related problem?

SESSION INFO:

sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-pc-linux-gnu (64-bit)

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils
datasets methods base

other attached packages: [1] data.table_1.9.4 xts_0.9-7
zoo_1.7-11

loaded via a namespace (and not attached): [1] chron_2.3-45
grid_3.1.2 lattice_0.20-30 plyr_1.8.1 [5] Rcpp_0.11.5
reshape2_1.4.1 stringr_0.6.2 tools_3.1.2

This is an extremely speculative answer...

I can get file.exists to return a logical vector of zero length only as follows:

> file.exists(character(0))
logical(0)

So my tentative hypothesis is that you mistakenly wiped that variable that should have been a file path and passed a zero length character vector to file.exists by mistake.

This would imply that isTRUE is the safest option in one sense, but personally I can hardly think of a case where passing a zero length vector to file.exists would be intentional on my part, and I'd probably want to see that error and fix it.

You should use isTRUE()

if (isTRUE(file.exists(sourceName))) {
    cat('Found')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM