Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Last active March 19, 2018 21:36
Show Gist options
  • Select an option

  • Save virtualstaticvoid/25524a75b370a605b2eacb56a009afa0 to your computer and use it in GitHub Desktop.

Select an option

Save virtualstaticvoid/25524a75b370a605b2eacb56a009afa0 to your computer and use it in GitHub Desktop.
Heroku Buildpack R - rJava Example
MyExchange.class

See virtualstaticvoid/heroku-buildpack-r#108

javac *.java

heroku create --stack heroku-16

heroku buildpacks:add https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16

heroku config:set R_VERSION=3.4.3 BUILD_PACK_VERSION=20180110-2010

git push heroku master

heroku run R --no-save -f /app/prog.R
#
# Example R code to install packages
# See http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages for details
#
###########################################################
# Update this line with the R packages to install:
my_packages = c("rJava")
###########################################################
install_if_missing = function(p) {
if (p %in% rownames(installed.packages()) == FALSE) {
install.packages(p)
}
else {
cat(paste("Skipping already installed package:", p, "\n"))
}
}
invisible(sapply(my_packages, install_if_missing))
//
// This Java program creates a number of variables which will
// then be sent to R to be manipulated.
//
// Example from https://scottdhoover.wordpress.com/2013/03/05/a-basic-rjava-example/
//
public class MyExchange {
public String getString() {
return "Testing";
}
public String[] getStringArray() {
return new String[] {"This is just a test...", "string array."};
}
public double getdouble() {
return 10.2;
}
public double[] getdoubleArray() {
return new double[]{0.001, 20.238};
}
public double[][] getdoubleMatrix() {
double[][] doubleMatrix = new double[][]{{1.1,2.2,3.3},{4.4,5.5,6.6}};
return doubleMatrix;
}
public int getInt() {
return 11;
}
public int[] getIntArray() {
return new int[] {1,2,3,4,5,6};
}
public int[][] getIntMatrix() {
int[][] intMatrix = new int[][]{{1,2,3},{4,5,6}};
return intMatrix;
}
public boolean getboolean() {
return true;
}
public boolean[] getbooleanArray() {
return new boolean[]{true, true, false, false, true};
}
public boolean[][] getbooleanMatrix() {
boolean[][] booleanMatrix = new boolean[][]{{true, true, false},{false, true, true}};
return booleanMatrix;
}
public static void main(String[] args) {
}
}
#
# Example R program
#
# example from https://scottdhoover.wordpress.com/2013/03/05/a-basic-rjava-example/
library('rJava')
.jinit()
.jaddClassPath("/app")
.jclassPath()
myExchange <- .jnew("MyExchange")
# strings #
stringTest <- .jcall(myExchange, "S", "getString")
stringArrayTest <- .jcall(myExchange, "[S", "getStringArray")
stringTest; stringArrayTest
# doubles #
doubleTest <- .jcall(myExchange, "D", "getdouble")
doubleArrayTest <- .jcall(myExchange, "[D", "getdoubleArray")
doubleMatrixTest <- .jcall(myExchange, "[[D", "getdoubleMatrix", simplify=T)
doubleTest; doubleArrayTest; doubleMatrixTest
# integers #
intTest <- .jcall(myExchange, "I", "getInt")
intArrayTest <- .jcall(myExchange, "[I", "getIntArray")
intMatrixTest <- .jcall(myExchange, "[[I", "getIntMatrix", simplify=T)
intTest; intArrayTest; intMatrixTest
# boolean #
booleanTest <- .jcall(myExchange, "Z", "getboolean")
booleanArrayTest <- .jcall(myExchange, "[Z", "getbooleanArray")
booleanMatrixTest <- .jcall(myExchange, "[[Z", "getbooleanMatrix", simplify=T)
booleanTest; booleanArrayTest; booleanMatrixTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment