akshay kulkarni
2018-May-13 07:59 UTC
[R] is there any method to defer the execution of code in r?
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]]
Rui Barradas
2018-May-13 08:16 UTC
[R] is there any method to defer the execution of code in r?
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 at 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. >
Jeff Newmiller
2018-May-13 13:07 UTC
[R] is there any method to defer the execution of code in r?
I am puzzled by the use of the term "cross-posted" here... I don't see the OP or their question or any similar words from the question involved the the given link, though that link seems worth bringing it to the OP's attention. But the function given in the question seems to have other problems: A) The download.file function call puts its result in a different place than the read.csv reads data from. Unless the OP is manually moving files around with the file browser in the background I don't see how this would ever work. B) The file paths refer to C:/Users/user ... it would be highly unusual for there to be such a directory on any machine, so I am wondering how close any of this code is to what the OP actually did. C) Reading the help page for download.file, it appears to behave like most functions in R... it does not return until the operation is complete. That is, the premise of the question seems flawed, and a truly reproducible example would be needed to convince me to go down the delay rabbit hole. The OP has been around this list long enough to know where to go to find out how to make a reproducible example. On May 13, 2018 1:16:47 AM PDT, Rui Barradas <ruipbarradas at sapo.pt> wrote:>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 at 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 at 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.-- Sent from my phone. Please excuse my brevity.
Seemingly Similar Threads
- is there any method to defer the execution of code in r?
- is there any method to defer the execution of code in r?
- is there any method to defer the execution of code in r?
- is there any method to defer the execution of code in r?
- aborting the execution of a script...