1

I would like to write an R script that can close and reopen + run itself.

This is needed for an API query that I am trying to make and which seems to require me going through these steps once every hour to be able to make additional requests. I tried to use the source() function - and simply run my script from itself every hour- but with this the API keeps rejecting additional requests; it seems that actually closing and opening the program is necessary.

I also tried to use the system() command - as described here to actually open R and execute the script - but I was not able to figure out how to implement this in a Mac environment.

Would you have any suggestions on how to do this?

nevrome
  • 1,471
  • 1
  • 13
  • 28
lippi
  • 11
  • 1

1 Answers1

0

The usual way to run a script every x amount of time on a Unix system is a cronjob. I'm not familiar with macOS, but apparently it works just as on Linux.

  1. Open the job list to edit it (you can of course use another editor instead of nano)
env EDITOR=nano crontab -e
  1. Add a line/job to the file. This runs any command line command. You can use Rscript to run an R script.
0 * * * * Rscript "/path/to/your/script.R"
  1. Exit and safe. This script should now run every hour.

If you want to change the timing check out crontab.guru. Check out this answer, if the cronjob reports Rscript to be missing.

nevrome
  • 1,471
  • 1
  • 13
  • 28