1

I am working with the R programming language.

I am trying to learn how to schedule tasks using the Task Scheduler. I found this post Scheduling R Script and I am trying to follow the instructions from the answer provided by "user:: petermeissner":

  • Step 1:

I opened a notepad document and wrote a small R program that I want the task scheduler to run. In this program, I want to generate 100 random numbers, save these random numbers as an RDS file - and then repeat/overwrite many times:

# r program to run
a = rnorm(100,100,100)
saveRDS(a, "a.RDS")

Then, I saved this file as "myscript.exe"

  • Step 2:

I went to the "start menu" and typed in "Task Scheduler". I then clicked "Action" and "Create Task". Here, I created a new task uploaded the ".exe" file:

enter image description here

Step 3: Now, I added the details about this task - for example, I want this task to run every 5 minutes. I went to the "Triggers" tab and entered the following information:

enter image description here

My Problem: I waited for 1 hour and noticed that my task did not run even once. Furthermore, when I look at "All Running Tasks" - I noticed that the task I created did not even run!

enter image description here

Can someone please tell me what I am doing wrong and what I can do to fix this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    `Rscript.exe` is a program installed alongside R that runs R scripts. Your R scripts should be saved with a `.R` suffix, not `.exe`. You need to have task scheduler run `Rscript.exe` which should be installed in your program files alongside R, as the question you link says somewhere like `C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe"` (with whatever newer version of R you have, probably not 3.0.2). Then the name and path to your R script is what you put in the parameters. – Gregor Thomas Feb 14 '23 at 03:18
  • Just like if you wanted to run your R script, you would first open R, and then use R to run the script; you need to have Windows open `Rscript.exe` and use Rscript to run your R script. – Gregor Thomas Feb 14 '23 at 03:20
  • With that clarification, hopefully the steps at the linked question will make more sense. If you need additional help, please edit your question or ask a new one. – Gregor Thomas Feb 14 '23 at 03:21
  • @ Gregor Thomas: thank you for your reply! I now changed the name of my file to :"myscript.R" – stats_noob Feb 14 '23 at 03:22
  • I am not sure I understand the last point about "Rscript.exe" ... can you please explain this again? – stats_noob Feb 14 '23 at 03:25
  • When you (a user) want to run R code, the first thing you do is open a program... maybe Rgui, maybe RStudio, etc. These are launched from .exe files. Then you use the interface of that program to run the R code. When you want your computer to run R code in a file, your computer doesn't want or need RStudio. The program that it will use to run R code is called "Rscript". You already have "Rscript" in you R Program Files; it was installed when you installed R. The instruction you need to schedule is to tell your computer "use `Rscript.exe` to run this R code in `myscript.R`". – Gregor Thomas Feb 16 '23 at 20:57

1 Answers1

1

In a text editor create a batch file: "MyRscript.bat"

The batch file will contain one line:

C:\Program Files\R\R-4.2.2\bin\Rscript.exe C:\rscripts\myscript.R

Ensure that the path is correct to the Rscript.exe file and your script.

Now in task scheduler, schedule running "MyRscript.bat" at the desired time and frequency.

The advantage of creating the bat file is one can now edit this file after upgrading R or changing the script file without the hassle of working in the task scheduler.
Also it is good practice to define your working directory inside the script.

See this question for more information: Scheduling R Script

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • thank you for your answer! I made these changes - I still dont see anything in "All Running Tasks" ... have I done this correctly? – stats_noob Feb 14 '23 at 03:38
  • just a question - if I turn off my computer, and then turn the computer back on ... will this task start running again? or will I need to re-initialize this task? – stats_noob Feb 14 '23 at 03:38
  • Once it is set-up in task scheduler it should always be ready to run. No need to reinitialize it. – Dave2e Feb 14 '23 at 03:41
  • is there suppose to be only one quote over here? C:\Program Files\R\R-4.2.2\bin\Rscript.exe" .... or is there supposed to be two quotes? :C:\Program Files\R\R-4.2.2\bin\Rscript.exe" – stats_noob Feb 14 '23 at 03:43
  • Can "myscript.R" be saved in my regular working directory? – stats_noob Feb 14 '23 at 03:48
  • Is this supposed to actually run the R Script and create a file "a.RDS" on my computer? As of now, I keep getting a "pop-up window" on my screen that prompts me to open R Studio ... but the file "a.RDS" is not being created. – stats_noob Feb 14 '23 at 03:57
  • 1
    I'd suggest putting the full filepath for your RDS file so you know where to look for it. Instead of `saveRDS(a, "a.RDS")` use `saveRDS(a, "C:/...specific path.../a.RDS")` for some folder on your computer. – Gregor Thomas Feb 16 '23 at 20:53