是否有任何方法可以推迟r中代码的执行?

时间:2018-05-13 08:01:25

标签: r download read.csv

我创建了以下函数来从给定的URL读取csv文件:

function(){ 
                        s <- 1;

                        #first get the bhav copy
                        today <- c();ty <- c();tm <- c();tmu <- c();td <- c();

                       # get the URL first
                        today <- Sys.Date()
                        ty <- format(today, format = "%Y")
                        tm <- format(today, format = "%b")
                        tmu <- toupper(tm)
                        td <- format(today, format = "%d")

       dynamic.URL <- paste("https://www.nseindia.com/content/historical/EQUITIES/",ty,"/",tmu,"/cm",td,tmu,ty,"bhav.csv.zip", sep = "")
       file.string <- paste("C:/Users/user/AppData/Local/Temp/cm",td,tmu,ty,"bhav.csv")

                        download.file(dynamic.URL, "C:/Users/user/Desktop/bhav.csv.zip")
                        bhav.copy <- read.csv(file.string)
                        return(bhav.copy)
}

如果我运行该函数,则立即说“找不到file.string”。但是当我在一段时间(几秒钟)之后运行它时,它会正常执行。我认为当download.file ecexecutes时,它会将控制转移到read.csv,并尝试加载尚未正确保存的文件。当我在一段时间后运行它时,它会尝试覆盖它不能的现有文件,read.csv正确加载保存的文件。

我希望该函数在我第一次运行时执行。在文件正确保存之前,是否有任何方法或功能可以推迟read.csv的操作?像这样:

download.file(dynamic.URL, "C:/Users/user/Desktop/bhav.csv.zip")
                            wait......
                            bhav.copy <- read.csv(file.string)

忽略download.file中的dest文件与file.string不同的事实;这是由于我的系统功能(Windows 7)。

非常感谢你的时间和精力......

0 个答案:

没有答案