2

I'm trying to return a vector through a Tableau calculated field. According to link below, Tableau can either receive a single value or a vector using connections to R through a calculated field.

http://community.tableau.com/docs/DOC-5313

see:

"...If the R code returns a matrix of values, Tableau won’t know what to do, we have to return a single value or a vector,..."

This is what I have in my calculated field, where Parameter 1 and Parameter 2 are just arbitrary values (just for demonstration purposes):

SCRIPT_REAL ('testvector <- c(.arg1, .arg2); testvector',min([Parameter 1]), min([Parameter 2]))

I was expected a single vector to be returned, but I'm getting the following error message:

## Error: Unexpected number of results returned by SCRIPT function.  Function expected 1 values; 2 values were returned. 
Ryan Chase
  • 2,384
  • 4
  • 24
  • 33

2 Answers2

2

You are not actually returning a value; just assigning 3 vectors in Rserve environment. You need to return testvector after the assignment:

SCRIPT_REAL('column1 <- c(.arg1, .arg2); 
            column2 <- c(.arg3, .arg4); 
            testvector <- c(column1,column2);
            testvector', 
            min([Parameter 1]), min([Parameter 2]),min([Parameter 3]), min([Parameter 4]))
Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
  • I'm confused. Why were there 3 vectors being created? It seems like there should only be one- `testvector <- c(column1, column2)`. Also, I added a single line `testvector` to my script but it didn't solve the problem. I'm still getting the same error message. – Ryan Chase Jul 28 '15 at 02:22
  • I think you edited the title of my original posting- unexpected number of results returned by script function – Ryan Chase Jul 28 '15 at 02:30
  • 1
    You need to explicitly specify which vector to return to Tableau. Without specifying `... ;testvector`, `Rserve` doesn't know which of the 3 vectors (i.e: `column1`, `column2` or `testvector`) to return. – Steven Beaupré Jul 28 '15 at 02:45
  • in my first comment I mentioned "Also, I added a single line testvector to my script but it didn't solve the problem. I'm still getting the same error message". I think it has something to do with the view I'm trying to put it on. For example, this site: [link](http://onlinehelp.tableau.com/current/pro/online/mac/en-us/r_connection_troubleshoot.html) says this: _The R script result must be either a scalar or vector of length one that is replicated for all rows, or a vector of length equal to the number of rows in the Tableau result table._ – Ryan Chase Jul 28 '15 at 02:56
  • Try it on a sheet with no other pills on the shelves – Steven Beaupré Jul 28 '15 at 03:04
  • @RyanChase Try to reduce your example with only one argument. Something like `'testvector <- .arg1; testvector, MIN([Parameter 1])'` – Steven Beaupré Jul 28 '15 at 11:54
  • If I simplify it to what you're suggesting it will no longer be a vector. It would be a single value. – Ryan Chase Jul 28 '15 at 14:23
  • ...I've simplified the example nonetheless. – Ryan Chase Jul 28 '15 at 14:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84485/discussion-between-steven-beaupre-and-ryan-chase). – Steven Beaupré Jul 28 '15 at 15:55
0

If the table in which you're using this calculated field has 1 row and Parameter1 value is 9 and Parameter2 values is 5, with code example you posted, you are creating a vector that looks like (9,5) which has 2 rows.

So the table 1 row but calculated field returns 2 rows. Hence the error message

"Function expected 1 values; 2 values were returned."

If you'd like to get a vector back, you will need to pass a vector. You can take a look at the k-means clustering example on R integration introductory blog post which passes several vectors that comprise the Iris dataset and gets a vector back with cluster assignments.

Bora
  • 66
  • 4