有没有什么方法可以推迟r中代码的执行?
我创建了以下函数,用于从给定URL读取csv文件:有没有什么方法可以推迟r中代码的执行?,r,download,read.csv,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 th
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)
}
忽略download.file中的destfile与file.string不同的事实;这是由于我的系统(windows 7)的功能
非常感谢您花费的时间和精力……也许
Sys.sleep
能帮上忙。复制您的代码很困难,因为它取决于您自己的目录结构和一个似乎不存在的url()。你能编辑出一个可复制的例子吗?你的假设是完全错误的。直到上一行dowload.file
完成后,read.csv
才会启动。这不是Javascript,没有发生异步下载。所以我不知道出了什么问题,但我发现您没有将函数定义分配给一个有点可疑的对象。它应该类似于getdata=function(){..
,然后用getdata()
调用它。
download.file(dynamic.URL, "C:/Users/user/Desktop/bhav.csv.zip")
wait......
bhav.copy <- read.csv(file.string)