2

I am pretty excited to have found Jeff and Dirk's application littler to run R functions from terminal. ¡kudos!
Since then, I have been able to pass my functions to my development team and have them running in other servers.

My question is about it's deployment. Before passing it to others, I try it out in my computer and prepare it with RStudio... (also kudos).
I was wondering if there's a command to run in the script on which I can tell if the function is run from the command or if it's been executed with R.

Thanks.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Diego-MX
  • 2,279
  • 2
  • 20
  • 35

2 Answers2

3

I don’t know whether there’s a littler specific answer. But in general it is impossible (or very hard) in R to determine how the code is run, which was one of the motivations for my work on modules.

The only thing R knows is whether the code is being run in an interactive shell (via interactive()).

With modules, you can test whether module_name() is set, analogous to Python’s __name__:

if (is.null(module_name()) && ! interactive()) {
    # Stand-alone, execute main entry point
}

if (! is.null(module_name())) {
    # Code is being loaded as a module.
}

I’ve written a small wrapper based on this which I’m using to write my command line applications. For instance, a very simple cat-like application would look as follows:

#!/usr/bin/env Rscript

sys = modules::import('sys')

sys$run({
    if (length(sys$args) == 0) {
        message('Usage: ', script_name(), ' filename')
        sys$exit(1)
    }

    input = sys$args[1]
    cat(readLines(input))
})
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I anticipate `interactive` does what I need. I'm gonna need some time to look at your "modules". – Diego-MX Feb 18 '15 at 17:26
  • 1
    @Diego Yes, it may be enough for you. The problem that `interactive()` has, and which modules solve, is that it will also be `TRUE` for library code which you are loading in your interactive session. This is annoying when you want to reuse parts of your code, but *also* have this code be independently executable. This is a pretty common pattern in Python packages, which I found useful in R as well. – Konrad Rudolph Feb 18 '15 at 17:31
2

I am not sure I understand your question. Do you mean something like

edd@max:~$ which r
/usr/local/bin/r
edd@max:~$ 

You can compare the result of which against the empty string as nothing comes back when you ask for a non-existing program.

edd@max:~$ which s      # we know we don't have this
edd@max:~$ 

You can then use the result of which r to check for, say, the version:

edd@max:~$ `which r` --version
r ('littler') version 0.2.2

git revision 8df31e5 as of Thu Jan 29 17:43:21 2015 -0800
built at 19:48:17 on Jan 29 2015
using GNU R Version 3.1.2 (2014-10-31)

Copyright (C) 2006 - 2014  Jeffrey Horner and Dirk Eddelbuettel

r is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License.  For more information about
these matters, see http://www.gnu.org/copyleft/gpl.html.

edd@max:~$ 

Edit: As you seem confused about interactive() true or false, consider r --help:

edd@max:~$ r --help

Usage: r [options] [-|file]

Launch GNU R to execute the R commands supplied in the specified file, or
from stdin if '-' is used. Suitable for so-called shebang '#!/'-line scripts.

Options:
  -h, --help           Give this help list
      --usage          Give a short usage message
  -V, --version        Show the version number
  -v, --vanilla        Pass the '--vanilla' option to R
  -t, --rtemp          Use per-session temporary directory as R does
  -i, --interactive    Let interactive() return 'true' rather than 'false'
  -q, --quick          Skip autoload / delayed assign of default libraries
  -p, --verbose        Print the value of expressions to the console
  -l, --packages list  Load the R packages from the comma-separated 'list'
  -d, --datastdin      Prepend command to load 'X' as csv from stdin
  -e, --eval  expr     Let R evaluate 'expr'


edd@max:~$ 

and

edd@max:~$ r -e'print(interactive())'
[1] FALSE
edd@max:~$ r -i -e'print(interactive())'
[1] TRUE
edd@max:~$ 

but that is setting it as opposed to querying it.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • What I meant to ask is closer to @Konrad's answer above. It looks like `interactive` does the trick. – Diego-MX Feb 18 '15 at 17:21