0 votes
1 view
in R Programming by (5.3k points)

I've created an 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 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"

1 Answer

0 votes
by (25.3k points)
edited by

To check the existence of a file and throw and error, you can use the tryCatch statement as follows:

 fileURL <- "https://dl.dropbox.com/u/7710864/courseraPublic/samsungData.rda"

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 Intellipaat Community. Get your technical queries answered by top developers !


Categories

...