Hi. I’m Sharon Machlis at IDG Communications, here with Episode 48 of Do More With R: Run lengthy R tasks as local background jobs.
Have a script that’s gonna to take awhile to run? Here’s a quick tip: You can run it in your current RStudio project as a background job, so it doesn’t tie up the R console in your working project. Here’s how.
Instead of sourcing this script the usual way, see what happens when I click the source drop-down menu. There’s an option “Source as a Local Job.”
Now I can run this as a local background job. I have the option to send my entire existing global environment to the local job (which I don’t need to do here). I can also copy the local job results back to my existing environment, which I do want in this case. Since I don’t have anything in my global environment now, it’s fine to put the results in my project’s global environment. There’s no risk of overwriting anything important.
You can see a progress bar as the job runs. Meanwhile, I can run other R code. Eventually, my results will show up as objects in my current project global environment.
Once they do, I can then use those variables. I do need to re-load any libraries that my local job used, though; those don’t copy over.
If you don’t want to use the RStudio dialog box to source a script as a local job, you can use the rstudioapi package and its jobRunScript() function. Here I source the script as a local background job. The default is not to copy results to your current working environment. The argument exportEnv equals R_GlobalEnv does the copying. The job is running now, and I can run other code as it does.
Now I can plot the results just like I did before.
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel — where you can subscribe so you never miss an episode. Hope to see you next time. Stay healthy and safe, everyone!