Data function returns "Error in as.data.frame.default" "cannot coerce class '"function"' into a data.frame"

Data function returns "Error in as.data.frame.default" "cannot coerce class '"function"' into a data.frame"

book

Article ID: KB0080951

calendar_today

Updated On:

Products Versions
Spotfire Analyst All supported versions
Spotfire Web Player All supported versions
Spotfire Enterprise Runtime for R All supported versions

Description

A Spotfire data function that uses a TIBCO Enterprise Runtime for R (TERR) script fails with the following error:
 
Could not execute function call.
TIBCO Enterprise Runtime for R returned an error: 'Error in as.data.frame.default(passed.args[[i]], stringsAsFactors = s : cannot coerce class '"function"' into a data.frame'.


 

Issue/Introduction

Data function returns "Error in as.data.frame.default" "cannot coerce class '"function"' into a data.frame"

Environment

All supported environments

Resolution

This error message says that a Spotfire data function is trying to convert an object of class "function" into an object of class "data.frame".

Conversion into a data frame is a prerequisite for returning an R or TERR object more complex than a single value or vector to Spotfire as a data function output parameter.  Many R/TERR object classes (including objects of class "function" and most objects of class "list") cannot be converted into a data frame.


If a Spotfire data function output parameter has been given the same name as a built-in R or TERR object of class "function" and the data function's script fails to create the new output object with that name, the data function framework will see the built-in function object with that name, and try to return the function as the output.  This is the expected behavior, and it is one way to cause a data function to throw the "cannot coerce class '"function"' into a data.frame" error.


Steps to avoid this issue:
  (1) Write robust scripts that anticipate and handle all possible input values.
  (2) Do not use the names of built-in R or TERR objects as the names for data function input or output parameters.


In a TERR Console session, the exists() and find() functions can be used to test whether a given (case sensitive) name is already in use, as in the following examples:


>
> exists("min")
[1] TRUE
>
> find("min")
[1] "package:base"
>
> exists("Min")
[1] FALSE
>
> find("Min")
character(0)
>



Data function script examples:

(1) The following is an example of an R or TERR script that will fail to create its new output objects named 'max' and 'min':


#-----
country <- "abcdef"

if (country == "USA")
{
  max <- 50
  min <- 13
}
#-----



If the above script is used in a Spotfire data function that expects objects named 'max' and 'min' as output parameters, the lines that define the new objects named 'min' and 'max' will not be run, and the new R or TERR objects named 'min' and 'max' will not be created.

R and TERR both have built-in functions named min() and max().  If the new objects named 'min' and 'max' are not created, the data function framework will find those built-in min() and max() functions and try to convert them into data frames, which is not possible.  This will make the data function throw the following error message:

Could not execute function call.
TIBCO Enterprise Runtime for R returned an error: 'Error in as.data.frame.default(passed.args[[i]], stringsAsFactors = s : cannot coerce class '"function"' into a data.frame'.




(2) The following modified script will also fail to create its new output objects, but those objects are named 'MaxNumberOfStates' and 'MinNumberOfStates':


#-----
country <- "abcdef"

if (country == "USA")
{
  MaxNumberOfStates <- 50
  MinNumberOfStates <- 13
}
#-----


If the above script is used in a Spotfire data function that expects objects named 'MaxNumberOfStates' and 'MinNumberOfStates' as output parameters, the lines that define the new objects named 'MaxNumberOfStates' and 'MinNumberOfStates' still will not be run, and the new R or TERR objects named 'MaxNumberOfStates' and 'MinNumberOfStates' will not be created.

But the data function will throw one of the following error messages, which makes it clear that the problem is an output object that has not been created:

Could not execute function call.
TIBCO Enterprise Runtime for R returned an error: 'Error in marshallSpotfireOutputParameters(outputs, envir = envir) : object 'MinNumberOfStates' not found'.



Could not execute function call.
TIBCO Enterprise Runtime for R returned an error: 'Error in marshallSpotfireOutputParameters(outputs, envir = envir) : object 'MaxNumberOfStates' not found'.





(3) The following robust version of the script will always create its new output objects, and a data function that uses it will not throw either of the errors described above:


#-----
country <- "abcdef"

if (country == "USA")
{
  MaxNumberOfStates <- 50
  MinNumberOfStates <- 13
} else {
  MaxNumberOfStates <- 155
  MinNumberOfStates <- 0
}
#-----