R tip: Schedule R scripts on a Mac

Hi, I’m Sharon Machlis, Director of Editorial
Data & Analytics at IDG Communications. I’m here with Episode 11 of Do More With
R: Schedule R scripts to run from the Mac command line. RStudio is a handy interface to work interactively. But sometimes once you finish your script,
you’d like it to run every week or every day, without having to kick it off manually. For important, mission-critical work, there
are scheduling solutions like RStudio Connect. For smaller things where you can live with
the occasional fail, you can set up a Mac to run scripts from the command line. Let me walk you through one way to do this. Here, I’ve got a test script that saves
the system time in a variable called current_time. The 2nd line prints that current time. Third line creates a message variable with
the character string “This is a test I’m running at” and the current time variable. By the way, if you’ve never used the glue
package before, it can be a nice alternative to using paste. Any code or variable name inside curly braces, like current_time,
within a quoted character string gets evaluated, and the value is used in the string. OK, 4th line uses the base R cat() function,
which displays or saves contents of a variable. Here, I’m saving it to a file called test.txt. Let me run this with the source button. And you can see what my current time is in
the Environment tab, and when the test.txt file was modified Now I’ll run that from the command line. There are a couple of ways to do this. I use the Rscript command. That should have been installed with R. You
can find out exactly where that is by running which Rscript in your Mac terminal: Let me change directories so my terminal is
in my More With R directory. In this Linux-like terminal, the command to
change directories is cd, and not R’s set.w.d. Now I can run my test.R script with Rscript
space test.R: And you can see that it ran. That’s a Unix/Linux thing. Running the c.h.m.o.d change mode command
on the file sets the files permissions. Typical way is with change mode 755 and the
filename. 755 lets the file owner read, write, and execute
the file (that’s the 7); and everyone else can read and execute it (but not change it;
those are the 5’s). Let me do that Last step is to set up what’s called a cron
job, using something called crontab. In theory, cron isn’t the preferred way
to do this on a Mac. The new approved way is to use Apple’s launchd. But I’ve been using cron for a long time,
and I think it’s easier than launchd. cron still works. And, there’s a cool R package that works
with cron and RStudio add-ins. If you are familiar with Unix cron jobs, you
can schedule them in your Mac terminal. Otherwise, you may want to check out the cronR
package. If you want its RStudio add-in to work, you
need a few other packages, too. I like using the pacman package for installing
and loading packages. Here I’m installing cronR from the b.n.o.s.a.c. GitHub repository. Then with p_load, I’m installing a few packages
if they’re not already on my system –miniUI, shiny, and shinyFiles. If they’re already on my system, they just
load. Mine are all already installed, including
the add-in. You can see it in RStudio under Addins If you click “Schedule R scripts on Linux/Unix”
under CRONR, here’s what comes up I can choose my test file Pick the time I want, and whether I want it
running every hour, every day, and so on. Then I just need to click on the Create job
button at the bottom, and Done. You can see in the bottom left RStudio panel,
details for the new cron job that was created on my system. One really important point: The test.txt file
that’s created by this cron job will NOT be in my project directory. RStudio understands that you’re in your
project directory. cron won’t. On a Mac, that file will be in your home directory,
unless you specify another location. If your cron job includes saving a file, remember
this. Otherwise you can end up spending a lot of
time wondering why your script isn’t running, when it IS running, but it’s storing your
data someplace you didn’t expect. I know this first-hand. Oh, and one final point: Of course if your
Mac is off, the cron job won’t run. But if it’s asleep, the script won’t run
either. You can make sure your Mac wakes up a minute
or two before your job is scheduled to run by going to System Preferences>
Energy Saver>Schedule and set the Start Up or Wake time. OK, that’s it for this episode, thanks for
watching! For more R tips, head to the More With R video
page at go.infoworld.com/morewithR. That’s https go dot infoworld dot com slash
more with R, all lowercase except for the R. Or, you can add the “Do More With R”
playlist to your YouTube library. So long, and hope to see you next episode!

2 thoughts on “R tip: Schedule R scripts on a Mac

Leave a Reply

Your email address will not be published. Required fields are marked *