Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

r - How do I check the existence of a downloaded file

I've created a R markdown file that starts by loading a file from the web. I found the cache=TRUE to be a little flaky so I want to put an if condition in to check for the downloaded file before downloading it.

Current Code - Always downloads file

fileURL <- "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"
setInternet2(TRUE)
download.file(fileURL ,destfile="./data/samsungData.rda",method="auto")
load("./data/samsungData.rda")

Desired Code - only upload if if not already downloaded

 destfile="./data/samsungData.rda"    
 fileURL <-
 "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"   
 if (destFile doesNotExist) {
    setInternet2(TRUE)
    download.file(fileURL ,destfile,method="auto") }
    load("./data/samsungData.rda")
 }
 load(destfile)

What syntax will give me the condition "destFile doesNotExist"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use tryCatch

  if(!file.exists(destfile)){
    res <- tryCatch(download.file(fileURL,
                              destfile="./data/samsungData.rda",
                              method="auto"),
                error=function(e) 1)
    if(dat!=1) load("./data/samsungData.rda") 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...