如何检查本地文件的存在

时间:2023-01-01 16:28:21

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.

我创建了一个R markdown文件,从web上加载文件开始。我发现缓存=TRUE,所以我想在下载文件之前先检查下载的文件。

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"

什么语法会给我条件"destFile doesn ' t exist "

3 个解决方案

#1


46  

You can use tryCatch

您可以使用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") 
}

#2


19  

As per the answer given by @agstudy

根据@agstudy给出的答案

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

#3


4  

An easy way to check the existence of a file in your working directory is: which(list.files() == "nameoffile.csv")

检查工作目录中文件是否存在的一个简单方法是:which(list.files() = "nameoffile.csv")

This doesn't exactly answer his question but I thought this might be helpful to someone who simply wants to check if a particular file is there in their directory.

这并不能确切地回答他的问题,但我认为这可能对那些只想检查某个文件是否在他们的目录中的人有帮助。

#1


46  

You can use tryCatch

您可以使用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") 
}

#2


19  

As per the answer given by @agstudy

根据@agstudy给出的答案

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

#3


4  

An easy way to check the existence of a file in your working directory is: which(list.files() == "nameoffile.csv")

检查工作目录中文件是否存在的一个简单方法是:which(list.files() = "nameoffile.csv")

This doesn't exactly answer his question but I thought this might be helpful to someone who simply wants to check if a particular file is there in their directory.

这并不能确切地回答他的问题,但我认为这可能对那些只想检查某个文件是否在他们的目录中的人有帮助。