Spotfire R 代码 (TERR) 出现问题 - 无效的下标类型 'list' 错误

Trouble with Spotfire R code (TERR) - Invalid subscript type 'list' error

我编写的代码可以在 R Studio (3.4.3) 中完美运行。我将代码复制到一个 Spotfire 项目 (TERR) 中,它 运行 一两次都很好,但现在出现了 "Invalid subscript type 'list'" 错误。我已经确定它来自 apply 函数中的下面一行。

minGT <- as.numeric(rownames(resultGT)[apply(resultGT , 2, which.min)])

我花了几天时间尝试在 SO 和其他地方找到的潜在解决方案,但无济于事。下面是一些代码块,经过精简以供阅读:

###Code revised previously after SO (@Parfait) assistance - different problem##
library(plyr)

DS <- DirectionalSurveys[, c("IDWELL", "API", "WELL NAME", "DIVISON", "MD", "INCL", "AZIM", "NS", "EW", "TVD", "DLS", "Northing", "Easting")]
Perf <- Perforation[, c("IDWELL", "Well API Number", "Well Name", "County", "Mid Perf MD", "Mid Perf TVD")]
colnames(DS) <- c("IDWELL", "API", "WellName", "Division", "MD", "INCL", "AZIM", "NS", "EW", "TVD", "DLS", "Northing", "Easting")
colnames(Perf) <- c("IDWELL", "API", "WellName", "County", "MidPerfMD", "MidPerfTVD")

df_lists <- lapply(seq_along(WellName), function(i) {

    S <- DS$MD[DS$WellName == WellName[i]]
    P <- Perf$MidPerfMD[Perf$WellName == WellName[i]]

    resultGT <- outer(S, P, '>=')
    resultGT[resultGT == FALSE] <- 50
    rownames(resultGT) <- paste0(S)
    colnames(resultGT) <- paste0(P)

#This is the line that throws the error - specifically the `apply` function

    minGT <- as.numeric(rownames(resultGT))[apply(resultGT , 2, which.min)]

#Calculations begin below after some additional subsetting (not shown)
    deep <- S[match(minGT, S)]
    shallow <- S[match(minGT, S) - 1]
#............etc.
#Remainder of code removed to prevent recipient boredom....

我真的很感谢一些(更多)帮助 - @Parfait 之前已经帮助我获得了完整的代码 运行,所以提前感谢您提供的任何额外帮助。我可以根据需要提供数据集和完整编码。

以下是完整的错误消息: Error message generated

在 Tibco 社区网站上的 DougJ 的帮助下,我能够获得解决我的问题的方法。下面是 DougJ 执行的一个测试集,用于确定问题的根源来自包含空单元格的 data.frame(P) - 请注意,data.frame(P) 每次迭代大约包含 120 行,而 data.frame(S) 超过每次迭代 300 行。我对数据帧大小差异的解决方案是语句 resultGT <- outer(S, P, ">=") 和后续语句 minGT <- rownames(resultGT)[apply(resultGT, 2, which.min)] 来为 "P" 的每个单独值找到 "S" 的最小值。这在 RStudio 中完美运行,但在带有 TERR 的 Spotfire 中抛出了错误。

#Test case below as performed by DougJ (Tibco Community) that identified problem area.
> xNA <- c(1:2, NA, 3:4)
> yNA <- c(3:5, NA, 6)
> 
> xNA
[1]  1  2 NA  3  4
> 
> yNA
[1]  3  4  5 NA  6
> 
> resultGT <- outer( xNA, yNA, ">=" )
> 
> resultGT[resultGT == FALSE] <- 50
> 
> rownames( resultGT ) <- paste0(xNA)
> colnames( resultGT ) <- paste0(yNA)
> 
> resultGT
  3  4  5 NA  6
1  50 50 50 NA 50
2  50 50 50 NA 50
NA NA NA NA NA NA
3   1 50 50 NA 50
4   1  1 50 NA 50
> 
> 
> apply(resultGT, 2, which.min)
$`3`
3 
4 

$`4`
4 
5 

$`5`
1 
1 

$`NA`
integer(0)

$`6`
1 
1 

> class( apply(resultGT, 2, which.min) )
[1] "list"
> 
> 
> as.numeric(rownames(resultGT))
[1]  1  2 NA  3  4
Warning message:
NAs introduced by coercion 
> 
> as.numeric(rownames(resultGT))[ apply(resultGT, 2, which.min) ]
Error in as.numeric(rownames(resultGT))[apply(resultGT, 2, which.min)] : 
  invalid subscript type 'list'
In addition: Warning message:
NAs introduced by coercion 
> 

下面是 DougJ 作为解决方案开发的最终代码:

#original code:
minGT <- rownames(resultGT)[apply(resultGT , 2, which.min)]

#new code (with slight modification):
minGT <- rownames(resultGT)[apply(resultGT, 2, function(X) {tmp <- which.min(X); ifelse(length(tmp), tmp, NA)} )]

现在可以在 Spotfire 中使用 TERR。感谢 DougJ 如果你在 SO!