Errors in TERR run in Spotfire, not seen run in RStudio

Hi Everyone,

I am trying to connect a data function in Spotfire that I have written and successfully run in RStudio (using TERR). My TERR version is 4.1.1 (64-bit on my local machine), and my Spotfire version is 7.6.0.48. I have searched on these forums for answers but I have haven't been able to apply them to my case.

The function I have written in R does a least-squares fit on a subset of data selected in Spotfire, and returns the fitting parameters. The script is organized in the R function (which I've called "fitdata"), and the section of code I have called "Run Script", which executes the data function from Spotfire. This takes the inputs from Spotfire and runs it through fitdata, and returns the outputs for Spotfire.

Here is the source code for reference ( have removed some portions to aid the clarity):

##########################################################################
#           fitdata 
#                     
#
# Arguments:
#           wellname <- well name from spotfire
#           date <- production date
#           normtime <- normalized time
#           flowrate <-   daily rate (in stb/d or Mscf/d)
#           crate <- cumulative production (in Mstb or MMscf)
#           fitenum <- enumeration tells which chart to fit
#                        (1 = rate time, 2 = cum time, 3 = rate cum)
#           productenum <- enumeration tells which product
#                           (1 = oil, 2 = gas)
#########################################################################


fitdata <- function(wellname, date, normtime, flowrate, crate,fitenum, productenum)
{
  # Clean up data
  Trainset <- data.frame(wn=wellname,dt=date,nt=normtime,fr=flowrate,cr=crate)
  Trainset <- Trainset[complete.cases(Trainset),] #remove NA values
  Trainset <- Trainset[!duplicated(Trainset$nt),] #remove duplicates
  
  #initialize variables
  qi <- median(Trainset[order(Trainset$fr,na.last = TRUE, decreasing = TRUE),][1:10,4])
  d1 <- 77
  b1 <- 1.65
  telf <- 12 #months
  tmax <- 40 #years
  rampup <- Trainset$nt[1]
  if (productenum == 1) { b2 <- 0.3} 
  else {b2 <- 0.6}
  
  #use nls to get fit of Qi, d1
    fitfunction <- function(IP, dcln, time) genRateC(IP, b1, b2, dcln, telf, tmax, time, 0, 1)
    fit <- nls(fr ~ fitfunction(IP,dcln, nt), data = Trainset, start = list(IP=qi, dcln = d1), 
                 algorithm = "port", lower = c(10, 5), upper = c(10000000,97))
  
  results <- data.frame("IP"=coef(fit)[1],
                        "Di"=coef(fit)[2],
                        "rampup"=rampup
                       )
  return(results)
}

##########################################################################
#
#           Run Script
#
##########################################################################

inputs <- data.frame(WELL_NAME,
                     PRODUCTION_DATE,
                     NORMALIZED_TIME,
                     FLOWRATE,
                     CRATE
                     )

outputs <- fitdata(inputs$WELL_NAME,
                   inputs$PRODUCTION_DATE,
                   inputs$NORMALIZED_TIME,
                   inputs$FLOWRATE,
                   inputs$CRATE,
                   1, #fit enumeration for rate time
                   1  #product enumeration for oil
                   )

ip <- outputs$IP[1]
di <- outputs$Di[1]
rampup <- outputs$rampup

When I run this script in Spotfire I get two main errors.

Error 1:

TIBCO Enterprise Runtime for R returned an error: 'Error in FUN(X[[i]]) : values must be type 'logical', but FUN(X[[1]]) result is type 'integer''.
   at Spotfire.Dxp.Data.DataFunctions.Executors.LocalFunctionClient.OnExecuting(FunctionClient funcClient)
   at Spotfire.Dxp.Data.DataFunctions.Executors.AbstractFunctionClient.<RunFunction>d__0.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.Executors.SPlusFunctionExecutor.<ExecuteFunction>d__0.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.<ExecuteFunction>d__6.MoveNext()

This error is coming from this line of code (ie when I comment it out it, error 2, below, is raised):

Trainset <- Trainset[complete.cases(Trainset),] #remove NA values

This code removes rows that contain NA values. Is there a better way to do this?

Error 2:

TIBCO Enterprise Runtime for R returned an error: 'Error in nlsfit.default(start, RHS, response, mEnv, weights = weights : missing value where TRUE/FALSE needed'.
   at Spotfire.Dxp.Data.DataFunctions.Executors.LocalFunctionClient.OnExecuting(FunctionClient funcClient)
   at Spotfire.Dxp.Data.DataFunctions.Executors.AbstractFunctionClient.<RunFunction>d__0.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.Executors.SPlusFunctionExecutor.<ExecuteFunction>d__0.MoveNext()
   at Spotfire.Dxp.Data.DataFunctions.DataFunctionExecutorService.<ExecuteFunction>d__6.MoveNext()

This error is coming from the code:

      fit <- nls(fr ~ fitfunction(IP,dcln, nt), data = Trainset, start = list(IP=qi, dcln = d1), 
                 algorithm = "port", lower = c(10, 5), upper = c(10000000,97))

 

These are the two main errors that I am getting from Spotfire, and I don't get them when I run the code in RStudio. Any suggestions are greatly appreciated!

 

Thanks,

Jeff

Attachments

AttachmentSize
Image icon terr_data_function_errors.png28.17 KB

1 Comment

One reason that code works in TERR from the command line but not from Spotfire is that the types of the inputs are not the same.  E.g., a Spotfire "Date" object becomes a TERR/R "POSIXct" object and you may have tested things with a TERR "Date" object.   You can get the data that TERR is getting from Spotfire by putting the line

save.image(file="/tmp/myData.RData", RFormat=TRUE)

at the top of your script.  (The RFormat=TRUE is not needed in recent versions of TERR, but is harmless.)  You can read that data into TERR or R with

load("/tmp/myData.RData") 

and then try running the code from the script using the resulting data.

When you get an error, run the traceback() function to see more information about it. 

wdunlap - Oct 01, 2016 - 10:44am ::
+ Add a Comment

(2) Answers

Login