is there any method to defer the execution of code in r?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

is there any method to defer the execution of code in r?

akshay kulkarni
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Rui Barradas
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Jeff Newmiller
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 <[hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Rui Barradas
Hello,

I don't understand.

It *is* the same question. Same code, same words. And same 'AKSHAY  M
KULKARNI' (the OP here) and 'AKshayKulkarni' (SO).

Exactly the same.

Rui Barradas

On 5/13/2018 2:07 PM, Jeff Newmiller wrote:

> 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 <[hidden email]> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Jeff Newmiller
Not when I click on that link.

On May 13, 2018 7:37:50 AM PDT, Rui Barradas <[hidden email]> wrote:

>Hello,
>
>I don't understand.
>
>It *is* the same question. Same code, same words. And same 'AKSHAY  M
>KULKARNI' (the OP here) and 'AKshayKulkarni' (SO).
>
>Exactly the same.
>
>Rui Barradas
>
>On 5/13/2018 2:07 PM, Jeff Newmiller wrote:
>> 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 <[hidden email]>
>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]]
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>>
>>>
>>> ______________________________________________
>>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Berry, Charles


> On May 13, 2018, at 9:24 AM, Jeff Newmiller <[hidden email]> wrote:
>
> Not when I click on that link.
>
>

Nor me, but what I get is actually

https://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r

note the number is *different* than 50314015 - the OPs posting

Seems like a bug - perhaps in SO's server?

HTH,

Chuck

p.s. the OP's question was cross-referenced on the page I got.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Rui Barradas
Hello,

OK, I have checked your link and it does seem like a bug somewhere.
Note that the question in your link was "asked Jul 23 '09 at 22:17".
I am talking about a question "asked 8 hours ago". That in the mean time
was closed as duplicate.

Anyway, sorry for the noise unrelated to R-Help, I will stop it now.

Rui Barradas

On 5/13/2018 5:41 PM, Berry, Charles wrote:

>
>
>> On May 13, 2018, at 9:24 AM, Jeff Newmiller <[hidden email]> wrote:
>>
>> Not when I click on that link.
>>
>>
>
> Nor me, but what I get is actually
>
> https://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r
>
> note the number is *different* than 50314015 - the OPs posting
>
> Seems like a bug - perhaps in SO's server?
>
> HTH,
>
> Chuck
>
> p.s. the OP's question was cross-referenced on the page I got.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

Duncan Murdoch-2
In reply to this post by Berry, Charles
On 13/05/2018 12:41 PM, Berry, Charles wrote:

>
>
>> On May 13, 2018, at 9:24 AM, Jeff Newmiller <[hidden email]> wrote:
>>
>> Not when I click on that link.
>>
>>
>
> Nor me, but what I get is actually
>
> https://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r
>
> note the number is *different* than 50314015 - the OPs posting
>
> Seems like a bug - perhaps in SO's server?

I suspect it is due to Rui having higher "reputation" on SO, so he can
see a question there that has been closed by a moderator (because
someone judged it to be a duplicate of the older question that you see).
  Since he has more reputation points, he gets to moderate decisions
like that, and can perhaps undo them.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: is there any method to defer the execution of code in r?

David Winsemius

> On May 13, 2018, at 10:01 AM, Duncan Murdoch <[hidden email]> wrote:
>
> On 13/05/2018 12:41 PM, Berry, Charles wrote:
>>> On May 13, 2018, at 9:24 AM, Jeff Newmiller <[hidden email]> wrote:
>>>
>>> Not when I click on that link.
>>>
>>>
>> Nor me, but what I get is actually
>> https://stackoverflow.com/questions/1174799/how-to-make-execution-pause-sleep-wait-for-x-seconds-in-r
>> note the number is *different* than 50314015 - the OPs posting
>> Seems like a bug - perhaps in SO's server?
>
> I suspect it is due to Rui having higher "reputation" on SO, so he can see a question there that has been closed by a moderator (because someone judged it to be a duplicate of the older question that you see).

Not a bug. "A feature". I also saw the recently posted question probably because of my "rep" and see that it was closed by two other users, one of whom, Jaap, has the ability to close questions with a single vote although he is not a moderator. Lower rep users need a total of 5 votes to close.

>  Since he has more reputation points, he gets to moderate decisions like that, and can perhaps undo them.

If there were a reason to undo the closure, there is an option to "reopen", but there is no reason that I can see. The 2009 posting seems to have elicited a sensible reply from Dirk.

>
> Duncan Murdoch
>
> ______________________________________________

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law

______________________________________________
[hidden email] 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.