Setup R
The examples in this section make use of the Bayes Server Java API from within R. It may also be possible to use Bayes Server in R via the .NET API.
The r code below should be included in order to use the Bayes Server API from R.
# Last updated on 03 Dec 2023
# TODO update the following path, replacing x.x with the version of Bayes Server you are using
jarPath <- 'C:/bayes-server-10-9-java-api/bayesserver-10.9.jar'
library(rJava) #load the rJava library
.jinit(classpath=jarPath)
Double <- J("java.lang.Double")
Integer <- J("java.lang.Integer")
Boolean <- J("java.lang.Boolean")
String <- J("java.lang.String")
Network <- J("com.bayesserver.Network")
State <- J("com.bayesserver.State")
StateContext <- J("com.bayesserver.StateContext")
Interval <- J("com.bayesserver.Interval")
IntervalEndPoint <- J("com.bayesserver.IntervalEndPoint")
VariableContext <- J("com.bayesserver.VariableContext")
Node <- J("com.bayesserver.Node")
Variable <- J("com.bayesserver.Variable")
Link <- J("com.bayesserver.Link")
VariableValueType <- J("com.bayesserver.VariableValueType")
HeadTail <- J("com.bayesserver.HeadTail")
TemporalType <- J("com.bayesserver.TemporalType")
TimeValueType <- J("com.bayesserver.data.TimeValueType")
Table <- J("com.bayesserver.Table")
CLGaussian <- J("com.bayesserver.CLGaussian")
TableIterator <- J("com.bayesserver.TableIterator")
RelevanceTreeInferenceFactory <- J("com.bayesserver.inference.RelevanceTreeInferenceFactory")
ParameterLearning <- J("com.bayesserver.learning.parameters.ParameterLearning")
ParameterLearningOptions <- J("com.bayesserver.learning.parameters.ParameterLearningOptions")
DatabaseDataReaderCommand <- J("com.bayesserver.data.DatabaseDataReaderCommand")
DataTableDataReaderCommand <- J("com.bayesserver.data.DataTableDataReaderCommand")
ReaderOptions <- J("com.bayesserver.data.ReaderOptions")
TemporalReaderOptions <- J("com.bayesserver.data.TemporalReaderOptions")
VariableReference <- J("com.bayesserver.data.VariableReference")
ColumnValueType <- J("com.bayesserver.data.ColumnValueType")
StateValueType <- J("com.bayesserver.StateValueType")
DefaultEvidenceReaderCommand <- J("com.bayesserver.data.DefaultEvidenceReaderCommand")
DefaultReadOptions <- J("com.bayesserver.data.DefaultReadOptions")
DataTable <- J("com.bayesserver.data.DataTable")
ValidationOptions <- J("com.bayesserver.ValidationOptions")
PCStructuralLearning <- J("com.bayesserver.learning.structure.PCStructuralLearning")
PCStructuralLearningOptions <- J("com.bayesserver.learning.structure.PCStructuralLearningOptions")
LinkConstraint <- J("com.bayesserver.learning.structure.LinkConstraint")
LinkConstraintMethod <- J("com.bayesserver.learning.structure.LinkConstraintMethod")
LinkConstraintFailureMode <- J("com.bayesserver.learning.structure.LinkConstraintFailureMode")
VariableGenerator <- J("com.bayesserver.data.discovery.VariableGenerator")
VariableGeneratorOptions <- J("com.bayesserver.data.discovery.VariableGeneratorOptions")
VariableDefinition <- J("com.bayesserver.data.discovery.VariableDefinition")
TimeSeriesMode <- J("com.bayesserver.learning.parameters.TimeSeriesMode")
toStateArray <- function(...){
return(.jarray(c(...), contents.class = "com.bayesserver.State"))
}
toStateContextArray <- function(...){
return(.jarray(c(...), contents.class = "com.bayesserver.StateContext"))
}
toVariableArray <- function(...){
return(.jarray(c(...), contents.class = "com.bayesserver.Variable"))
}
toList <- function(...){
xs <- new( J("java.util.ArrayList") )
lapply(c(...), function(x) { xs$add(x)})
return(xs)
}
toNodeArray <- function(...){
return(.jarray(c(...), contents.class = "com.bayesserver.Node"))
}
toDouble <- function(x){
return(if(is.na(x)) NULL else new(Double, x))
}
toDoubleArray <- function(...){
return(.jarray(lapply(c(...), function(x) { toDouble(x) }), contents.class = "java.lang.Double"))
}
toVariableReferenceList <- function(...){
xs <- new( J("java.util.ArrayList") )
lapply(..., function(x) { xs$add(x)})
return(xs)
}
licenseBayesServer <- function(licenseKey){
J("com.bayesserver.License")$validate(licenseKey)
}
toDataTable <- function(df) {
dt <- new(DataTable)
dfTypes <- sapply(df, typeof)
dfClasses <- sapply(df, class)
dfNames <- names(df)
columnCount <- length(dfNames)
for (i in 1:columnCount)
{
dt$getColumns()$add(dfNames[i], toJavaClass(dfTypes[i], dfClasses[i]))
}
for (r in 1:nrow(df))
{
values <- lapply(df[r,], function(x) {
return(toJavaObject(x))
})
values <- .jarray(values, contents.class = "java.lang.Object")
dt$getRows()$add(values)
}
return(dt)
}
toJavaClass <- function(rType, rClass) {
if(rType == "double" && rClass == "numeric")
{
return(Double$class)
}
else if(rType == "logical" && rClass == "logical")
{
return(Boolean$class)
}
else if(rType == "integer" && rClass == "factor")
{
return(String$class) # store factor integer values as factor level names
}
else if(rType == "integer" && rClass == "integer")
{
return(Integer$class)
}
else if(rType == "character")
{
return(String$class)
}
else
{
stop(sprintf("type [%s] or class [%s] not supported", rType, rClass))
}
}
toJavaObject <- function(x) {
if(is.na(x))
{
return(NULL)
}
rType <- typeof(x)
rClass <- class(x)
if(rType == "double" && rClass == "numeric")
{
return(new (Double, x))
}
else if(rType == "logical" && rClass == "logical")
{
return(new (Boolean, x))
}
else if(rType == "integer" && rClass == "integer")
{
return(new (Integer, x))
}
else if(rType == "integer" && rClass == "factor")
{
s <- as.character(x)
return(new (String, s))
}
else if(rType == "character")
{
return(new (String, as.character(x)))
}
else
{
stop(sprintf("type [%s] or class [%s] not supported", rType, rClass))
}
}