Spotfire TERR代码报错:Invalid subscript type 'list'求助
Hey JDC, sorry to hear you're hitting this frustrating snag with TERR in Spotfire after your code worked flawlessly in RStudio! Let's break down what's likely going on and fix that tricky apply line.
The Root Cause
TERR (Tibco Enterprise Runtime for R) has subtle differences from standard R in how it handles certain function return values—especially with apply on matrices. The "Invalid subscript type 'list'" error happens because, in some cases (like when your resultGT matrix has only one column, i.e., a well with a single perforation), TERR might return a list from apply instead of an atomic vector. You can't use a list to subset a numeric vector (your as.numeric(rownames(resultGT))), hence the error.
This explains why your code worked a few times: you probably processed wells with multiple perforations first, where apply returned a vector, but hit a single-perforation well later that triggered the list return.
Fixes to Try
Here are three robust solutions, ordered by reliability:
1. Use vapply (Most Predictable Across Environments)
vapply lets you explicitly define the return type of your apply function, eliminating ambiguity between R and TERR. Replace your problematic line with:
# Force return of an integer vector (matches which.min's output type) minGT <- as.numeric(rownames(resultGT))[vapply(resultGT, which.min, FUN.VALUE = integer(1))]
2. Convert apply Result to a Vector
If you prefer sticking with apply, wrap its output in as.vector() to force it into a non-list type:
minGT <- as.numeric(rownames(resultGT))[as.vector(apply(resultGT, 2, which.min))]
3. Simplify by Skipping Row Name Conversion
Since your rownames(resultGT) are just paste0(S), converting them back to numeric is redundant—you can use S directly, which avoids any conversion quirks:
# Use S directly instead of rownames conversion minGT <- S[vapply(resultGT, which.min, FUN.VALUE = integer(1))]
Bonus: Add Guard Rails for Edge Cases
To avoid other unexpected errors (like empty S or P for some wells), add a quick check at the start of your lapply function:
df_lists <- lapply(seq_along(WellName), function(i) { S <- DS$MD[DS$WellName == WellName[i]] P <- Perf$MidPerfMD[Perf$WellName == WellName[i]] # Skip empty cases to avoid broken matrices if (length(S) == 0 || length(P) == 0) { message(paste("Skipping well", WellName[i], "- missing MD or perforation data")) return(NULL) } # Rest of your code here... })
Test the Behavior
To confirm the issue, you can print the class of your apply output in TERR for a problematic well:
test_apply <- apply(resultGT, 2, which.min) print(class(test_apply)) # If this returns "list", that's exactly the problem
These changes should make your code consistent across both RStudio and Spotfire's TERR environment. Feel free to share your full dataset/code if you run into any other kinks!
内容的提问来源于stack exchange,提问作者JDC

