Class RScriptExecutor


  • public class RScriptExecutor
    extends java.lang.Object
    The RScriptExecutor provides an interface for invoking R scripts via system commands. This provides a simple alternative to using rJava, which relies on JNI bits. The interface supports passing and retrieving values in a tabular format, as well as arbitrary arguments. The values are sent and retrieved from the script via stdin/stdout. The arguments are baked into the script using Freemarker tags.
    Author:
    jwhite
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String RSCRIPT_BINARY
      The Rscript binary used to execute the .R scripts.
      static long SCRIPT_TIMEOUT_MS
      Maximum runtime of the Rscript process in milliseconds before failing and throwing an exception.
    • Constructor Summary

      Constructors 
      Constructor Description
      RScriptExecutor()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      RScriptOutput exec​(java.lang.String script, RScriptInput input)
      Executes by given script by: - Searching both the classpath and the filesystem for the path - Copying the script at the given path to a temporary file and performing variable substitution with the arguments using Freemarker.
      protected static com.google.common.collect.ImmutableTable<java.lang.Long,​java.lang.String,​java.lang.Double> fromCsv​(java.lang.String csv)
      Convert the CSV string to an immutable table.
      protected static java.lang.StringBuilder toCsv​(com.google.common.collect.RowSortedTable<java.lang.Long,​java.lang.String,​java.lang.Double> table)
      Convert the table to a CSV string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • RSCRIPT_BINARY

        public static final java.lang.String RSCRIPT_BINARY
        The Rscript binary used to execute the .R scripts.
      • SCRIPT_TIMEOUT_MS

        public static final long SCRIPT_TIMEOUT_MS
        Maximum runtime of the Rscript process in milliseconds before failing and throwing an exception.
        See Also:
        Constant Field Values
    • Constructor Detail

      • RScriptExecutor

        public RScriptExecutor()
    • Method Detail

      • exec

        public RScriptOutput exec​(java.lang.String script,
                                  RScriptInput input)
                           throws RScriptException
        Executes by given script by: - Searching both the classpath and the filesystem for the path - Copying the script at the given path to a temporary file and performing variable substitution with the arguments using Freemarker. - Invoking the script with commons-exec - Converting the input table to CSV and passing this to the process via stdin - Parsing stdout, expecting CSV output, and converting this to an immutable table
        Throws:
        RScriptException
      • fromCsv

        protected static com.google.common.collect.ImmutableTable<java.lang.Long,​java.lang.String,​java.lang.Double> fromCsv​(java.lang.String csv)
                                                                                                                                 throws java.io.IOException
        Convert the CSV string to an immutable table.
        Throws:
        java.io.IOException
      • toCsv

        protected static java.lang.StringBuilder toCsv​(com.google.common.collect.RowSortedTable<java.lang.Long,​java.lang.String,​java.lang.Double> table)
                                                throws java.io.IOException
        Convert the table to a CSV string.
        Throws:
        java.io.IOException