Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rscript detect if R script is being called/sourced from another script

I have a written a script that when it is sourced checks if the script is being run interactively using interactive(). If it is run interactively, it does not search for command line arguments. However, if it is not run interactively, it searches for command line arguments and throws an error.

This is normally fine, but sometimes I write a second R script that I want to run independently just to process some data. So Script2 sources Script1, and Script1 detects that it is not being run interactively, and begins searching for command line arguments and throwing errors.

Is there a way besides interactive() that a script can detect its context? For example, I would want separate behavior when it is being run directly vs when it is being loaded for access to one of its internal functions. With packages I could do something like dplyr::arrange() to access arrange without having to load all of dplyr.

EDIT: My current very janky workaround has been to start an interactive session, source Script1, use save.image() to save the functions, and then in Script2 use load to load the saved .RData file. But obviously this is not...elegant.


I don't think the exact code I use is that relevant, but including it in case someone feels this is important to the answer...

Stripped down example code:

#!/usr/bin/env Rscript

library(optparse)

function1 <- function(etc,etc) {}
function2 <- function(etc,etc) {}

if(!interactive()) { 

    # example call
    # Rscript create_reference_file.R -c cd4cd8 -o /home/outputfolder/ 

    option_list = list(
        make_option(c('-c', '--cell'), type = 'character', default = NULL,
                    help = 'the name of the cell',
                    metavar = 'character'),
        make_option(c('-o','--outdir'), type = 'character', default = NULL, 
                    help = 'the location where you wish to store your output',
                    metavar = 'character'),
    )

    opt_parser <- OptionParser(option_list = option_list)
    opt <- parse_args(opt_parser)

    function1(opt); function2(opt) # etc etc, I do stuff with the opt inputs
}
like image 392
Brandon Avatar asked Dec 21 '17 20:12

Brandon


People also ask

Can you run an Rscript from another Rscript?

You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.

What does Rscript -- vanilla mean?

Let's break it down line by line: #!/usr/bin/env Rscript --vanilla. Sometimes called a 'shebang', this line tells the Linux and MacOS command line interpreters (which both default to one called 'bash'), what you want to use to run the rest of the code in the file.

How do you call another file in R?

R file from another . R file, you can call the source("abc. R") followed by source("xyz. R") (assuming that both these files are in your current working directory.


1 Answers

EDIT

Okay, this is a LOT more like python's __name__ trick. (Previous answer below, kept for historical reasons.)

function1 <- function(etc,etc) {}
function2 <- function(etc,etc) {}

if (sys.nframe() == 0L) {
    library(optparse)
    # ...
}

It is about as minimalist as one could hope for, does not require the sourceing script to know anything about it, and seems to work well even when nested.

Other possible mechanisms could be used (additional functions required) by looking at script names, per Rscript: Determine path of the executing script. Many plausible (some really good) solutions exist there, but they all require a pre-defined function not defined in a base package (or non-trivial code included in the script to be sourced). If you want to "assume package X is installed", then your script becomes potentially non-portable.


(Previous answer, I suggest you use above.)

I'll throw this out as a hack ... it's only slightly less janky than your workaround, but it relies on the calling script knowing something of what the called script is testing for.

If the calling script sets a variable:

BEING_SOURCED_FROM_SOMEWHERE <- TRUE

then the called script can check for it:

function1 <- function(etc,etc) {}
function2 <- function(etc,etc) {}

if (! exists("BEING_SOURCED_FROM_SOMEWHERE")) {
  library(optparse)
  # ...
}

I don't like it. It isn't as flexible as python's

if __name__ == "__main__":
    import optparse
    # ...

But I think I dislike it less than your use of save and load for function definitions.

like image 94
r2evans Avatar answered Sep 23 '22 11:09

r2evans