Skip to content

Commit

Permalink
Merge pull request #163 from NathanSkene/patch-1
Browse files Browse the repository at this point in the history
Added basic error catches to the R interface
  • Loading branch information
QinbinLi committed Sep 9, 2019
2 parents 5218852 + 3f16ebf commit 9b499a6
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions R/svm.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
if(Sys.info()['sysname'] == 'Windows'){
if(!file.exists("../build/bin/Debug/thundersvm.dll")){
print("Please build the library first!")
quit()
}
dyn.load("../build/bin/Debug/thundersvm.dll")
} else if(Sys.info()['sysname'] == 'Linux'){
if(!file.exists("../build/lib/libthundersvm.so")){
print("Please build the library first!")
quit()
}
dyn.load("../build/lib/libthundersvm.so")
} else if(Sys.info()['sysname'] == 'Darwin'){
if(!file.exists("../build/lib/libthundersvm.dylib")){
print("Please build the library first!")
quit()
}
dyn.load("../build/lib/libthundersvm.dylib")
} else{
print("OS not supported!")
quit()
check_location <- function(){
if(Sys.info()['sysname'] == 'Windows'){
if(!file.exists("../build/bin/Debug/thundersvm.dll")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/bin/Debug/thundersvm.dll")
} else if(Sys.info()['sysname'] == 'Linux'){
if(!file.exists("../build/lib/libthundersvm.so")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/lib/libthundersvm.so")
} else if(Sys.info()['sysname'] == 'Darwin'){
if(!file.exists("../build/lib/libthundersvm.dylib")){
stop("Please build the library first (or check you called this while your workspace is set to the thundersvm/R/ directory)!")
}
dyn.load("../build/lib/libthundersvm.dylib")
} else{
stop("OS not supported!")
}
}
check_location() # Run this when the file is sourced

svm_train_R <-
function(
svm_type = 0, kernel = 2,degree = 3,gamma = 'auto',
Expand All @@ -28,6 +28,8 @@ tol = 0.001, probability = FALSE, class_weight = 'None', cv = '-1',
verbose = FALSE, max_iter = -1, n_cores = -1, dataset = 'None', model_file = 'None'
)
{
check_location()
if(!file.exists(dataset)){stop("The file containing the training dataset provided as an argument in 'dataset' does not exist")}
res <- .C("train_R", as.character(dataset), as.integer(kernel), as.integer(svm_type),
as.integer(degree), as.character(gamma), as.double(coef0), as.double(nu),
as.double(cost), as.double(epsilon), as.double(tol), as.integer(probability),
Expand All @@ -40,5 +42,8 @@ function(
test_dataset = 'None', model_file = 'None', out_file = 'None'
)
{
check_location()
if(!file.exists(test_dataset)){stop("The file containing the training dataset provided as an argument in 'test_dataset' does not exist")}
if(!file.exists(model_file)){stop("The file containing the model provided as an argument in 'model_file' does not exist")}
res <- .C("predict_R", as.character(test_dataset), as.character(model_file), as.character(out_file))
}
}

0 comments on commit 9b499a6

Please sign in to comment.