rspotfireterr

"Subscript out of bounds" when running apply() in TERR, but works in plain R


When I try running the following piece of code in Spotfire Professional as a "R Script - Tibco Enterprise Runtime for R":

mydata_broken <- structure(
  list(
    Var1 = list(3.99083333270391, 3.99083333270391, 3.99083333270391, 3.99083333270391), 
    Var2 = list(3.99083333270391, 3.99083333270391, 3.99083333270391, 3.99083333270391)), 
  row.names = c("1", "2", "3", "4"), 
  class = "data.frame", 
  out.attrs = list(dim = c(2L, 2L), 
                   dimnames = list(
                     Var1 = c("Var1=3.99083333270391", "Var1=3.99083333270391"), 
                     Var2 = c("Var2=3.99083333270391", "Var2=3.99083333270391")
                     )
                   )
  )

mydata_ok <- structure(
  list(
    Var1 = list(3.99083333270391), 
    Var2 = list(3.99083333270391)), 
  row.names = "1", 
  class = "data.frame", 
  out.attrs = list(dim = c(1L, 1L), 
                   dimnames = list(
                     Var1 = "Var1=3.99083333270391", 
                     Var2 = "Var2=3.99083333270391")
                   )
  )

out <- apply(mydata_broken, 1, function(y) mean(as.numeric(y)))

I get the following error message:

TIBCO Enterprise Runtime for R returned an error: 'Error in expand.names(x) : subscript out of bounds'. at Spotfire.Dxp.Data.DataFunctions.Executors.LocalFunctionClient.OnExecuting(FunctionClient funcClient)

(rest of stack trace omitted)

However, the same code works flawlessly in plain R. If I replace mydata_broken with mydata_ok in the call to apply(), everything works as expected (both in TERR and plain R).

Things I've tried so far:

Version & configuration information

So, my question is: Am I making some stupid mistake here? Or is this a bug in the Spotfire R runtime?

UPDATE I'd like to reopen the question, because I got a viable workaround from Spotfire support, and I'd like to add it as an answer.


Solution

  • Here's a short summary of the response I got from Spotfire support:

    Modified definition of data that works in TERR

    mydata_working <- structure(
        list(
          Var1 = c(3.99083333270391, 3.99083333270391, 3.99083333270391, 3.99083333270391), 
          Var2 = c(3.99083333270391, 3.99083333270391, 3.99083333270391, 3.99083333270391)), 
        row.names = c("1", "2", "3", "4"), 
        class = "data.frame", 
        out.attrs = list(dim = c(2L, 2L), 
                         dimnames = list(
                           Var1 = c("Var1=3.99083333270391", "Var1=3.99083333270391"), 
                           Var2 = c("Var2=3.99083333270391", "Var2=3.99083333270391")
                           )
                         )
        )