Hello,
This is cross posted from StackOverflow:
https://stackoverflow.com/questions/50314015/is-there-any-method-to-defer-the-execution-of-code-in-r
Cross posting is discouraged in r-help.
Rui Barradas
On 5/13/2018 8:59 AM, akshay kulkarni wrote:
dear members,
I have created the following function to read a csv file from a given
URL:
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)
}
If I run the function, immediately it says that "file.string not
found". But when I run it after some time(a few seconds), it executes
normally. I think when `download.file` ecexecutes, it transfers control
to `read.csv, `and it tries to load the file which is not yet properly
saved. when I run it after some time, it tries to overwrite the
existing file, which it cannot, and the `read.csv`properly loads the
saved file.`
I want the function to execute the first time I run it. Is there any
way or a function to defer the action of `read.csv`until the file is
properly saved? Something like this:
download.file(dynamic.URL, "C:/Users/user/Desktop/bhav.csv.zip")
wait......
bhav.copy <- read.csv(file.string)
Ignore the fact that the destfile in download.file is different from
file.string; it is due to function of my system (windows 7).
Very many thanks for your time and effort...
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.