R - Run source() in background
You can use system()
and Rscript to run your script as an asynchronous background process:
system("Rscript -e 'source(\"your-script.R\")'", wait=FALSE)
At the end of your script, you may save your objects with save.image()
in order to load them later, and notify of its completion with cat()
:
...
save.image("script-output.RData")
cat("Script completed\n\n")
Hope this helps!
Running a R function in background
You can use the future package (I'm the author) for this, e.g.
library("future")
plan(multiprocess)
# non-blocking
res %<-% test()
something_else()
Running jobs in background in R
What would help is to output it to a file when you have computed it and then parse that file everytime you open R. Write yourself a computeMatrix() function or script to produce a file with the matrix stored in a sensible format. Also write yourself a loadMatrix() function or script to read in that file and load the matrix into memory for use, then call or run loadMatrix everytime you start R and want to use the matrix.
In terms of running an R job in the background, you can run an R script from the command line with the syntax "R CMD BATCH scriptName" with scriptName replaced by the name of your script.
How to spawn a long-running parallel process in R that runs an R script?
To use r_bg
just wrap your source()
call in a function (which should be self-contained) like this:
library(callr)
# create dummy script
writeLines('writeLines(as.character(Sys.time()), "myResult.csv")', 'myRScript.R')
# execute dummy script in background R process
r_bg(function(){source('myRScript.R')})
# read results
read.csv('myResult.csv')
Opening a new instance of R and sourcing a script within that instance
Answering my own question in the event someone else is interested down the road.
After a couple of days of working on this, I think the best way to carry out this workflow is to not limit myself to working just in R. Writing a bash script offers more flexibility and is probably a more direct solution. The following example was suggested to me on another website.
#!/bin/bash
# Run task 1
Rscript Task1.R
# now run the three jobs that use Task1's output
# we can fork these using '&' to run in the background in parallel
Rscript Task2.R &
Rscript Task3.R &
Rscript Task4.R &
# wait until background processes have finished
wait %1 %2 %3
Rscript Task5.R
Related Topics
Excel Cell Coloring Using Xlsx
Filter One Selectinput Based on Selection from Another Selectinput
Distance of Point Feature to Nearest Polygon in R
Warning in Install.Packages:Installation of Package 'Tidyverse' Had Non-Zero Exit Status
In R, What Does "Loaded via a Namespace (And Not Attached)" Mean
R - How to Replace Parts of Variable Strings Within Data Frame
Remove Plot Margins in Ggplot2
Package 'Stringi' Does Not Work After Updating to R3.2.1
How to Set Attributes for a Variable in R
Shiny Selectinput to Select All from Dropdown
Apply Over Matrix by Column - Any Way to Get Column Name
R List Get First Item of Each Element
How to Change the Position of the Table of Contents in Rmarkdown