Skip navigation

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.

11 Comments

  1. 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.

    • danke! I’ll give that a go after our camping trip. #ty

  2. 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?

    • Kinda. There’s https://github.com/hrbrmstr/hrbraddins/blob/master/R/enslave.R (and a cpl other ones folks have developed) which will take a selection and then write it to a temp file for it to be executed. It could easily be modified to do the clipboard dance as well.

      • Thanks!

      • 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.

        • 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.

  3. 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…

    • 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).

  4. 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!

    • (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.


2 Trackbacks/Pingbacks

  1. […] article was first published on R – rud.is, and kindly contributed to […]

  2. […] 소스: Running RStudio (1.2) Background Jobs | rud.is […]

Leave a Reply

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