Type coercion error

R sometimes performs type coercion, which you might not expect. Type coercion can cause problems because the R Execute operator expects R to return a data frame if you assign the alpine_output variable in the R script.

One common case of type coercion is when you select a single column from the data frame. The column is coerced from a data frame to a "numeric" type, which is returned to Team Studio as an array of doubles. This causes a Java ClassCastException because data frames are returned as java.util.Map, but the R coercion results in a numeric vector being returned, rather than a data frame. You must do type casting in R to undo the type coercion. However, calling as.data.frame on a numeric vector from a projected column associates it with a wrong name, so you must rename the column after you create the data frame, as in the following example.

> x <- rnorm(1:10)
> y <- rnorm(1:10)
> xy <- data.frame(x=x, y=y)
> head(xy)
            x          y
1  0.43765921 -0.5160450
2  0.69197730  0.1938183
3  0.08869384 -1.2843015
4  0.99896046 -1.2151482
5 -1.08242907  0.1109868
6 -0.55645055 -1.3973622
> class(xy)
[1] "data.frame"
> xOnly <- xy$x
> head(xOnly)
[1]  0.43765921  0.69197730  0.08869384  0.99896046 -1.08242907 -0.55645055
> class(xOnly)
[1] "numeric"
> xOnlyAsDataFrame <- as.data.frame(xy$x)
> head(xOnlyAsDataFrame)
         xy$x
1  0.43765921
2  0.69197730
3  0.08869384
4  0.99896046
5 -1.08242907
6 -0.55645055
> names(xOnlyAsDataFrame) <- "x"
> head(xOnlyAsDataFrame)
            x
1  0.43765921
2  0.69197730
3  0.08869384
4  0.99896046
5 -1.08242907
6 -0.55645055
> class(xOnlyAsDataFrame)
[1] "data.frame"
>