This works fine in R Studio.
library(stringdist)
result <- afind(input1, input2, method="cosine")
distance <- result[2]
real_distance <- distance[[1]][1]
output <- real_distance
When I add it as a column expression in Spotfire, where input1 is a column and input 2 is a document property, I get a cryptic error message.
I am not sure if I can use external libraries in custom column R expressions in Spotfire, and am not sure what the issue is?
Error is:
TIBCO Enterprise Runtime for R returned an error (4)
The expression function 'SearchDistance' could not be executed.
Error in .Call() : dims [product 1290496000000] do not match the length of object [2005811200] eval(script, envir = .GlobalEnv) eval(script, envir = .GlobalEnv) withCallingHandlers({ afind(input1, input2, method = "cosine") .Call()
UPDATE***
If I enable TERR debugging I get this:
Data function 'CalculateSearchScore' debug output (3)
Unmarshalling 2 input parameters. Input 'input1', sent by inline XML chr [1:1136000] "MYALGIA FEVER" "FATIGUE" "ASTHENIA" "CYSTITIS" ...
- attr(*, "SpotfireColumnMetaData")=List of 1 .. $ Description: chr "" Input 'input2', sent by inline XML chr [1:1136000] "NAUSEA" "NAUSEA" "NAUSEA" "NAUSEA" ...
- attr(*, "SpotfireColumnMetaData")=List of 1 .. $ Description: chr "" Done unmarshalling input parameters. Loading required package: stringdist
My column formula is as below:
SearchDistance([medical_term],"${MedicalTerm}")
I had assumed when you use this in a calculated column that it would run once per row, passing in each column value independently...perhaps what it is doing is passing in a vector containing the entire set of column values?
SOLUTION*****
This works:
library(stringdist)
calculate_match_score <- function(target, pattern) {
print(target[1])
result <- afind(target, pattern, method="cosine")
distance <- result[2]
return(distance[[1]][,1])
}
output <- calculate_match_score(target = input1, pattern = input2[1])