Automating R script with Windows 7

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

Automating R script with Windows 7

vincent.deluard

Hi R-users,

I am trying to automate the daily running of a simple R script from Windows 7.
From previous posts, I understand that this needs to be done with the task scheduler.
I can schedule my laptop to automatically open R at a certain time, but not to execute a script.

Secondary question: how do I save a list of R commands so that they get executed once the file is open?
Right now, I save my code in a notepad doc and paste over in R, but there has to be another way. I have tried saving my code as .r file using the editor and open the file with R later but this does not seem to execute the code.

I very much appreciate the help.

Vincent Deluard, CFA
 
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

Tyler Ritchie
You are looking to run R in batch mode

see How to run R in batch mode [1] and the Quick-R on Batch Processing [2]

[1] http://turing.une.edu.au/~stat356/Rbatch.html
[2]  http://www.statmethods.net/interface/batch.html

On Mon, Mar 5, 2012 at 8:55 AM, vincent.deluard
<[hidden email]>wrote:

>
> Hi R-users,
>
> I am trying to automate the daily running of a simple R script from Windows
> 7.
> >From previous posts, I understand that this needs to be done with the task
> scheduler.
> I can schedule my laptop to automatically open R at a certain time, but not
> to execute a script.
>
> Secondary question: how do I save a list of R commands so that they get
> executed once the file is open?
> Right now, I save my code in a notepad doc and paste over in R, but there
> has to be another way. I have tried saving my code as .r file using the
> editor and open the file with R later but this does not seem to execute the
> code.
>
> I very much appreciate the help.
>
> Vincent Deluard, CFA
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4446693.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
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: Automating R script with Windows 7

FJ M
In reply to this post by vincent.deluard

I created a txt file with R commands in it and then a batch file to process the txt file. The batch file could be scheduled. The batch file is:
 
REM on Microsoft Windows (adjust the path to R.exe as needed)
"C:\Program Files\R\R-2.13.2\bin\x64\R.exe" CMD BATCH    "C:\Users\Frank\Documents\R\Scripts\Multi_Graph.txt" "C:\Users\Frank\Documents\R\Scripts\Multi_Graph.out"
PAUSE
 
 
The text file is:
 
x <- seq(-4, 4, length=100)
hx <- dnorm(x)
degf <- c(1, 3, 8, 30)
colors <- c("red", "blue", "darkgreen", "gold", "black")
labels <- c("df=1", "df=3", "df=8", "df=30", "normal")
pdf("C:\\Users\\Frank\\Documents\\R\\Scripts\\Norm_Graph.pdf")
plot(x, hx, type="l", lty=2, xlab="x value",
  ylab="Density", main="Comparison of t Distributions")
for (i in 1:4){
  lines(x, dt(x,degf[i]), lwd=2, col=colors[i])
}
legend("topright", inset=.05, title="Distributions",
  labels, lwd=2, lty=c(1, 1, 1, 1, 2), col=colors)

 
This produces a nice plot.
 
Frank
 
 
---------------------------------------- > Date: Mon, 5 Mar 2012 08:55:44 -0800 > From: [hidden email] > To: [hidden email] > Subject: [R] Automating R script with Windows 7 > > > Hi R-users, > > I am trying to automate the daily running of a simple R script from Windows > 7. > >From previous posts, I understand that this needs to be done with the task > scheduler. > I can schedule my laptop to automatically open R at a certain time, but not > to execute a script. > > Secondary question: how do I save a list of R commands so that they get > executed once the file is open? > Right now, I save my code in a notepad doc and paste over in R, but there > has to be another way. I have tried saving my code as .r file using the > editor and open the file with R later but this does not seem to execute the > code. > > I very much appreciate the help. > > Vincent Deluard, CFA > > > -- > View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with!
 -Windows-7-tp4446693p4446693.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > 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
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: Automating R script with Windows 7

jholtman
In reply to this post by vincent.deluard
Just save your R script as a plain text file.  Here is a copy of a
file that I will show you how to execute; it is saved as \temp\test.r

cat('this is my R script executing\n', format(Sys.Date()))

Here is the command that I execute at the 'CMD' prompt and that you
would have scheduled by Windows:

C:\R-2.14.2\bin\i386\Rscript.exe \temp\test.r

This has the full path name (on my machine) to the 'Rscript.exe'
program included in R for executing scripts

Here is the output from the execution on the command prompt screen:

>C:\R-2.14.2\bin\i386\Rscript.exe \temp\test.r
this is my R script executing
 2012-03-05

So it is easy to setup.  Let me know if you have any problems.

On Mon, Mar 5, 2012 at 11:55 AM, vincent.deluard
<[hidden email]> wrote:

>
> Hi R-users,
>
> I am trying to automate the daily running of a simple R script from Windows
> 7.
> >From previous posts, I understand that this needs to be done with the task
> scheduler.
> I can schedule my laptop to automatically open R at a certain time, but not
> to execute a script.
>
> Secondary question: how do I save a list of R commands so that they get
> executed once the file is open?
> Right now, I save my code in a notepad doc and paste over in R, but there
> has to be another way. I have tried saving my code as .r file using the
> editor and open the file with R later but this does not seem to execute the
> code.
>
> I very much appreciate the help.
>
> Vincent Deluard, CFA
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4446693.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
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: Automating R script with Windows 7

Mikhail Titov-2
In reply to this post by vincent.deluard
"vincent.deluard" <[hidden email]> writes:

> I am trying to automate the daily running of a simple R script from Windows
> 7.
>>From previous posts, I understand that this needs to be done with the task
> scheduler.

That is correct.

> I can schedule my laptop to automatically open R at a certain time, but not
> to execute a script.

For example you can use

schtasks /create /tn "My R task" /sc DAILY /ST 03:00:00 /TR
C:\path_to_your_batch_file.cmd

to start task daily at 3am.

> Secondary question: how do I save a list of R commands so that they get
> executed once the file is open?

I highly recommend to read a manual on Rscript and use it in your batch
file instead of the "source"-ing mentioned below.

> Right now, I save my code in a notepad doc and paste over in R, but there
> has to be another way.

Consider using some IDE. If not Emacs+ESS or Eclipse, then at least Tinn-R.

> I have tried saving my code as .r file using the
> editor and open the file with R later but this does not seem to execute the
> code.

You should "source" your file to "execute" it, i.e.

source("path_to_my.R")

--
Mikhail

______________________________________________
[hidden email] mailing list
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: Automating R script with Windows 7

vincent.deluard
This post has NOT been accepted by the mailing list yet.
In reply to this post by jholtman
Thanks for the quick answer.

I believe I must do sth wrong.

When I enter the path to my 'RScript.exe' program (C:\Program Files\R\R-2.11.1\bin\RScript.exe  on my machine), the command prompt displays the following error message

 'C:\Program' is not recognized as an internal or external command, operable program or batch file

What am I missing here?
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

vincent.deluard
In reply to this post by jholtman
Hi Jim,

Please disregard my earlier post -- I have done some research and realized the space between after "program " was the the issue. I can now open RScript.exe from the command prompt using the abbreviated form:

C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe

From your earlier post, I understand the structure to type in the command prompt should be:
'Path to RScript.exe' SPACE 'Path to the .R file I want to execute'

Which should translate as the following on my machine:
C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe \C:\Users\Vincent\Documents\temp\test.r
Yet I get the following error message:
'Fatal Error: cannot open file \C:\Users\Vincent\Documents\temp\test.r'

What am I doing wrong?

Thanks for your help!
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

vincent.deluard
In reply to this post by jholtman
Hey Jim,

Thanks for the help. It took me a little bit of research but I got your solution to work.
For everybody who has the same problem

(1) remember to use the abbreviated names (no space) in folder paths:
ex: 'C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe'

(2) put the folder with the R script to run in the same location (in my case: C:\)
C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe \temp\dailyroutine.r

Using the task scheduler in Windows 7.0 is straightforward.

Vincent Deluard, CFA
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

maviney
hi

i have tried all day in getting this to work, but i have fail

I cant even schedule it to open up rscript, even by manually i cant open rscript

Pls help
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

Jeff Newmiller
Sounds like operating system troubles, not R troubles.

If you want help with invoking R, please take a deep breath, read the FAQs and the posting guide, and tell us exactly what you did at the command line if you still think R is the problem.

Note that any problems you may be having with the access permissions that your interactive or automatically-triggered environment is restricted by are almost impossible for us to remotely troubleshoot, and are not on-topic for this mailing list.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

maviney <[hidden email]> wrote:

>hi
>
>i have tried all day in getting this to work, but i have fail
>
>I cant even schedule it to open up rscript, even by manually i cant
>open
>rscript
>
>Pls help
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635237.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>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
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: Automating R script with Windows 7

Bart Joosen
In R you should slashes instead of backslashes:
C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe C:/Users/Vincent/Documents/temp/test.r

Bart
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

Ista Zahn
In reply to this post by vincent.deluard
On Mon, Mar 5, 2012 at 2:47 PM, vincent.deluard
<[hidden email]> wrote:

> Hi Jim,
>
> Please disregard my earlier post -- I have done some research and realized
> the space between after "program " was the the issue. I can now open
> RScript.exe from the command prompt using the abbreviated form:
>
> C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe
>
> >From your earlier post, I understand the structure to type in the command
> prompt should be:
> 'Path to RScript.exe' SPACE 'Path to the .R file I want to execute'
>
> Which should translate as the following on my machine:
> C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe
> \C:\Users\Vincent\Documents\temp\test.r
> Yet I get the following error message:
> 'Fatal Error: cannot open file \C:\Users\Vincent\Documents\temp\test.r'

I don't think you want the first backslash in the test.r path, i.e.,
it should be C:\Users\Vincent\Documents\temp\test.r instead of
\C:\Users\Vincent\Documents\temp\test.r

Best,
Ista

>
> What am I doing wrong?
>
> Thanks for your help!
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4447406.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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
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: Automating R script with Windows 7

maviney
In reply to this post by Jeff Newmiller
Thank you for your help

So i have tried many ways on different computers, but i believe i have an operating system, as I  open RScript up in my local directory it just flashes at me and them disappears

I tried to task schedule, using the following code

"C:\Program Files\R\R-2.12.1\bin\i386\Rscript.exe"

but the Rscript just flashes at me

Thanks for ur assistance, its back to researching my problem  
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

Jeff Newmiller
I did not suggest that you don't HAVE an operating system... simply that you don't know how to use yours.

However you accomplish starting programs automatically, you WILL need to specify both the R interpreter (which you do seem to be accomplishing) and the name of the script you wish to run (which you don't appear to be doing). Appropriate use of quoting may be necessary if you have spaces in your paths.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

maviney <[hidden email]> wrote:

>Thank you for your help
>
>So i have tried many ways on different computers, but i believe i have
>an
>operating system, as I  open RScript up in my local directory it just
>flashes at me and them disappears
>
>I tried to task schedule, using the following code
>
>"C:\Program Files\R\R-2.12.1\bin\i386\Rscript.exe"
>
>but the Rscript just flashes at me
>
>Thanks for ur assistance, its back to researching my problem  
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635275.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>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
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: Automating R script with Windows 7

Bart Joosen
In reply to this post by maviney
I would try first without the task scheduler:
Make a .bat file and run this from the command line.
This way you can see what is going on without the flashing window that is opened and closed immeadiately.


maviney wrote
I tried to task schedule, using the following code

"C:\Program Files\R\R-2.12.1\bin\i386\Rscript.exe"

but the Rscript just flashes at me

Thanks for ur assistance, its back to researching my problem
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

ANswer_777
This post has NOT been accepted by the mailing list yet.
In reply to this post by maviney
Hi,
The condition of mine is same with you!
Could you help?
Jie
Reply | Threaded
Open this post in threaded view
|

Re: Automating R script with Windows 7

Frantech
This post has NOT been accepted by the mailing list yet.
In reply to this post by vincent.deluard
This works - It runs R code from the scheduler:

Firstly I changed the "Path" system environment variable by appending the following to it:
    ;C:\Program Files\R\R-2.15.2\bin\

I have a file called D:\R Programming\05 R in batch\Test.R that contains this R code:
    cat('This is my R script executing\n', format(Sys.Date()))

Now I have this batch file C:\Users\Frank\Desktop\myR.bat which contains:
    CMD /k Rscript.exe "D:\R Programming\05 R in batch\Test.R"

    The /k switch keeps the window open after it executes.

When I run the batch file here is the output:
C:\Users\Frank\Desktop>CMD /k Rscript.exe "D:\R Programming\05 R in batch\Test.R
"
This is my R script executing
 2014-04-16
C:\Users\Frank\Desktop>


This will also work under the scheduler but you need "logon as a batch job" access.

To make this security change:
    Windows Logo+R keyboard shortcut to the run command
    type "secpol.msc"        without the quotes
    Security Settings
    Local Policies
    User Rights Assignment
    Log on as a batch job
    Rt click, Properties, "your_user_name"