Running RStudio (1.2) Background Jobs

The forthcoming RStudio 1.2 release has a new “Jobs” feature for running and managing background R tasks.

I did a series of threaded screencaps on Twitter but that doesn’t do the feature justice.

So I threw together a quick ‘splainer on how to run and Python (despite RStudio not natively supporting Python) code in the background while you get other stuff done, then work with the results.

Cover image from Data-Driven Security
Amazon Author Page

13 Comments Running RStudio (1.2) Background Jobs

  1. Pingback: Running RStudio (1.2) Background Jobs – Mubashir Qasim

  2. Randy Lai

    reticulate objects do not work because they are external pointers. You will need to convert them to R objects using py_to_r, then the flow should follow.

    Reply
  3. jonzadra

    Is there any way to:
    1) start a job using code rather than the gui;
    2) start a job from highlighted code or code in the clipboard rather than having to save it as a separate file?

    Reply
      1. jonzadra

        I’m curious how you use enslave(). Since the highlighted text would be in source, but you’d have to go to the console to run enslave(), the getActiveDocumentContext() will return the info on the cursor position in the console.

        Reply
        1. hrbrmstr

          The RStudio Addin handles the basic use-case. enslave() could be modified to work with the clipboard. If you file that as an issue I can likely make time to implement. There are some new changes coming to the RStudio “jobs” API when 1.2 finally comes to life so there will be changes as to how enslave() ultimately works as well.

          Reply
  4. thea

    Is there a way to access the output as the script is running in the background (rather than wait for the script to complete)? Tried to insert save.image() lines, but looks like nothing actually gets output until the job is complete. Maybe this would be better suited to the enslave() option you mention above…

    Reply
    1. hrbrmstr

      You’d pretty much have to serialize anything you’d need to a known directory location if you want interim access to objects. I just tried this using save.image() and it definitely works (said R data file has to be manually loaded into your current foreground R session).

      Reply
  5. Meg

    Hi, do you know if it’s possible to source another script from the job. I have my main script that I want to run as a job but there is a separate script with a bunch of functions that I want to source at the beginning. When I’ve tried to run the job it always runs the first few lines then halts when it reaches the line to source the functions script. Any tips? Thanks!

    Reply
    1. hrbrmstr

      (going to give this a go as a blog reply but it might need to be a video; just let me know if that wld help in the explanation; trying to communicate async stuff like jobs with multiple moving parts is non-trivial :-)

      I made a file in my local ~/jobs/ directory called srctest.R in it I have the following:

      y <- list(a = 10)
      
      source("~/jobs/tobesourced.R", local = TRUE)
      

      As can be inferred from ^^ I also have ~/jobs/tobesourced.R which just contains

      y$another <- "yep"
      

      When I run it from the Jobs pane with “R Script” == ~/jobs/srctest.R, “Working Directory” == ~/jobs, and “Copy job results” == “To results object…” (and leave “Run job with copy of global environment” unchecked) I end up with a srctest_results environment object y in it and both elements (with the correct values). So it would seem on the surface that it’s possible to do what you’re looking to do.

      Reply
  6. Pingback: RStudio v1.2의 추가 기능 : Background Job  | THE-R

Leave a Reply to Meg Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.