Skip to content

Commit 4a4eb0a

Browse files
authored
Adding error catches to the R interface
Currently the interface crashes the R session if any arguments are wrong or if it is called from the wrong directory. This is very disruptive. I have added some minimal error checks. It should probably have more for the other arguments.
1 parent 5e05fad commit 4a4eb0a

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

R/svm.R

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
if(Sys.info()['sysname'] == 'Windows'){
2-
if(!file.exists("../build/bin/Debug/thundersvm.dll")){
3-
print("Please build the library first!")
4-
quit()
1+
check_location <- function(){
2+
if(Sys.info()['sysname'] == 'Windows'){
3+
if(!file.exists("../build/bin/Debug/thundersvm.dll")){
4+
print("Please build the library first!")
5+
quit()
6+
}
7+
dyn.load("../build/bin/Debug/thundersvm.dll")
8+
} else if(Sys.info()['sysname'] == 'Linux'){
9+
if(!file.exists("../build/lib/libthundersvm.so")){
10+
print("Please build the library first!")
11+
quit()
12+
}
13+
dyn.load("../build/lib/libthundersvm.so")
14+
} else if(Sys.info()['sysname'] == 'Darwin'){
15+
if(!file.exists("../build/lib/libthundersvm.dylib")){
16+
print("Please build the library first!")
17+
quit()
18+
}
19+
dyn.load("../build/lib/libthundersvm.dylib")
20+
} else{
21+
print("OS not supported!")
22+
quit()
523
}
6-
dyn.load("../build/bin/Debug/thundersvm.dll")
7-
} else if(Sys.info()['sysname'] == 'Linux'){
8-
if(!file.exists("../build/lib/libthundersvm.so")){
9-
print("Please build the library first!")
10-
quit()
11-
}
12-
dyn.load("../build/lib/libthundersvm.so")
13-
} else if(Sys.info()['sysname'] == 'Darwin'){
14-
if(!file.exists("../build/lib/libthundersvm.dylib")){
15-
print("Please build the library first!")
16-
quit()
17-
}
18-
dyn.load("../build/lib/libthundersvm.dylib")
19-
} else{
20-
print("OS not supported!")
21-
quit()
2224
}
25+
check_location() # Run this when the file is sourced
26+
2327
svm_train_R <-
2428
function(
2529
svm_type = 0, kernel = 2,degree = 3,gamma = 'auto',
@@ -28,6 +32,8 @@ tol = 0.001, probability = FALSE, class_weight = 'None', cv = '-1',
2832
verbose = FALSE, max_iter = -1, n_cores = -1, dataset = 'None', model_file = 'None'
2933
)
3034
{
35+
check_location()
36+
if(!file.exists(dataset)){stop("The file containing the training dataset provided as an argument in 'dataset' does not exist")}
3137
res <- .C("train_R", as.character(dataset), as.integer(kernel), as.integer(svm_type),
3238
as.integer(degree), as.character(gamma), as.double(coef0), as.double(nu),
3339
as.double(cost), as.double(epsilon), as.double(tol), as.integer(probability),
@@ -40,5 +46,8 @@ function(
4046
test_dataset = 'None', model_file = 'None', out_file = 'None'
4147
)
4248
{
49+
check_location()
50+
if(!file.exists(test_dataset)){stop("The file containing the training dataset provided as an argument in 'test_dataset' does not exist")}
51+
if(!file.exists(model_file)){stop("The file containing the model provided as an argument in 'model_file' does not exist")}
4352
res <- .C("predict_R", as.character(test_dataset), as.character(model_file), as.character(out_file))
44-
}
53+
}

0 commit comments

Comments
 (0)