automating R script using Mac's Automator and Calendar - r

I have been trying to run a script automatically using the steps that I found online.
I am trying to run the following R script called AUTO.R
Here is what the script contains:
library(quantmod)
obs <- last(Ad(getSymbols("SPY", auto.assign=FALSE)))
saveRDS(obs, "SAMPLE.rds")
When I build the application it prints Workflow completed
I believe all is well until the time comes to run the script. The alarm pop-up in my desktop is displayed from Calendar but nothing runs. After a few minutes the folder where the .rds file should be saved does not contain anything.

Two suggested changes:
Your Automator task should be more like just /usr/local/bin/Rscript --vanilla /Users/rimeallthetime/Desktop/AUTO.R
You should explicitly set the path in saveRDS; i.e. saveRDS(obs, "/Users/rimeallthetime/Desktop/SAMPLE.rds")
Honestly, though, you should at least make a ~/bin dir (i.e. a directory called bin under your home directory, so in your case /Users/rimeallthetime/bin and put both the workflow and R script in there, and I'd also suggest creating another directory for output files vs the desktop.
UPDATE
I just let the calendar event run and this is really a crude way to automate what you want to do. You'd be better off in the long run using launchd, that way it's fully automated and requires no human intervnention at all (but you may need to adjust your script to send you a notification or "append" to the rds file).

Related

R: Learning How to Use Task Scheduler

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:
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:
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!
Can someone please tell me what I am doing wrong and what I can do to fix this?
Thanks!
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

In Jupyter notebook/lab, how can I automatically reset variables on exit?

I have a jupyter notebook containing sensitive data that I would like to not be cached inside the notebook. This would avoid jupyter's tendency to mix data and code.
In a notebook I can reset all variables using
%reset
Is there any way to run this automatically on exit, or on shutdown of the notebook or server?
Or is there a command-line script that could be run over a .ipynb, e.g. in a nightly cron job, to purge the file of stored variables (or - even better - only certain variables)?
Thanks!
nbclean allows some fairly complex customization on what gets cleaned and altered in the resulting notebook. You could do cron job with a script running that on your schedule. Or use Github actions to trigger upon actions such as push.

Schedule a function that belongs to an R package

I'm trying to build an R package whose goal is to run a series of analyses by taking input data and writing output data to an external database (PostgreSQL).
Specifically, I need a set of operations to be scheduled to run on a daily basis. Therefore, I have written some bash scripts with R code (using the header #!/usr/bin/env Rscript) and I have saved them into the exec/ folder of the R package. The scripts make several call to the package's core functions in R/ folder.
At this point, once installed the package on a linux server, how do I set up a crontab that is able to directly access the scripts in the exec/ folder?
Is this way of proceeding correct or is there a different best practice for such operations?
We do this all the bleeping time at work. Here at home I also have a couple of recurring cronjobs, eg for CRANberries. The exec/ folder you reference works, but my preferred solution is to use, say, inst/scripts/someScript.R.
Then one initial time you need create a softlink from your package library, say, /usr/local/lib/R/site-library/myPackage/scripts/someScript.R to a directory in the $PATH, say /usr/local/bin.
The key aspect is that the softlink persists even as you update the package. So now you are golden. All you now need is your crontab entry referencing someScript.R. We use a mix of Rscript and littler scripts.

use a batch script to run an R script interactively in the R shell

I can run an R script non-interactively from a batch file and save the output.
I can open an interactive R session using a batch file.
I can open R AND run a script from a batch file by doing one, then the other (but then the variables from the script are not available in the interactive session).
What I have not been able to do is to use a batch file to open an interactive R session and run a script from within that interactive session. How can I do this?
Regarding workarounds:
The purpose is to use the script to load and process a very large dataset daily, and then have that available for use during an interactive session in R, so using a non-interactive session is not an option for this task. Likewise I am aware that I can run an interactive session using the Windows command prompt but for a variety of reasons I do not want to do that. I want everything loaded into the R shell for use there. I realize Task Scheduler would be useful for this, but unfortunately, I do not have permissions to modify Task Scheduler. I am only allowed to use a batch file which is scheduled then by IT.
I apologize if I simply lack the vocabulary to search for the answer to my question effectively and welcome answers directing me to previous questions.
Thank you.
One possibility would be to create a Rprofile.site script that sources the setup file that loads the data. Then, from your batch script set the R_PROFILE variable for this session to point to this Rprofile.site script.
In your batch script,
#echo off
set R_PROFILE=C:\path\to\Rprofile.site
Rterm.exe
In Rprofile.site
.First <- function() {
print("Loading some stuff")
source("setup.R")
}
In setup.R
dat <- data.frame(x=1:10)
I guess you could do with the setup.R file and put it all in the .First function as well.

Autosys job in windows fails to copy all files but doesnt fail

We have a BOX scheduled in Autosys. If the BOX gets triggered at the scheduled time, all the PDFs generated out of one of the steps is not getting copied but the job is also not failing. When we are HOLDING the box and running step by step all outputs are getting copied.
A good troubleshooting step would be to either add in a sleep/delay step of a short time between the generation of the files and the downstream jobs.
A better way might be to use a file trigger or file watcher that will only let the below steps proceed if the files are all there (you can trigger on number of files or whatever stat is appropriate).
If your copy step is a simple copy command without any validation (like copy abc_file_*.pdf) then it wouldn't have any trouble copying whatever files it sees, even if not as many as you intend.

Resources