Class RScriptExecutor
- java.lang.Object
-
- org.opennms.netmgt.integrations.R.RScriptExecutor
-
public class RScriptExecutor extends 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 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(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<Long,String,Double>
fromCsv(String csv)
Convert the CSV string to an immutable table.protected static StringBuilder
toCsv(com.google.common.collect.RowSortedTable<Long,String,Double> table)
Convert the table to a CSV string.
-
-
-
Field Detail
-
RSCRIPT_BINARY
public static final 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
-
-
Method Detail
-
exec
public RScriptOutput exec(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<Long,String,Double> fromCsv(String csv) throws IOException
Convert the CSV string to an immutable table.- Throws:
IOException
-
toCsv
protected static StringBuilder toCsv(com.google.common.collect.RowSortedTable<Long,String,Double> table) throws IOException
Convert the table to a CSV string.- Throws:
IOException
-
-