You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spotfire自定义列R表达式调用stringdist库报错及解决

Spotfire自定义R列表达式维度不匹配错误解决

背景

以下代码在R Studio中可正常运行:

library(stringdist)
result <- afind(input1, input2, method="cosine")
distance <- result[2]
real_distance <- distance[[1]][1]
output <- real_distance

将其作为Spotfire自定义列表达式(input1为数据列,input2为文档属性)使用时,出现如下报错:

TIBCO Enterprise Runtime for R returned an error (4)
表达式函数'SearchDistance'无法执行。
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()

原列公式为:

SearchDistance([medical_term],"${MedicalTerm}")

问题原因

开启TERR调试后确认,Spotfire并未按预期逐行传入单行值,而是将input1和input2都传入了整列向量(长度1136000),导致afind函数计算时出现维度不匹配的问题。

解决代码

修改后的R代码适配了Spotfire的向量传入逻辑,可正常生成自定义列:

library(stringdist)

calculate_match_score <- function(target, pattern) {    
    result <- afind(target, pattern, method="cosine")
    distance <- result[2]
    return(distance[[1]][,1])
}

output <- calculate_match_score(target = input1, pattern = input2[1])

内容的提问来源于stack exchange,提问作者smackenzie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 16:24:20