Search code examples
rtableau-apiworkspacerserve

How to put functions into Rserve workspace?


I try to use Rserve from Tableau. I am able to execute the following code:

SCRIPT_REAL(".arg1*.arg2", MAX([Price]), [TestParam])

Basically, I write the R code in Tableau (".arg1*.arg2" in my case) and send it to Rserve. Rserve, in its turn, takes the code, executes it and sends the result back to Tableau.

Now I am trying to do the same but by defining a function within R environment and accessing it from Tableau via Rserve. So, I execute the following in R:

> myfunc <- function(x, y) {x*y}
> library(Rserve)
> Rserve()

Which means that I define a function in R environment and then start Rserve in the same environment with the hope that Rserve will see the function that I have defined before.

Then in Tableau I define a new column using this code:

SCRIPT_REAL("myfunc(.arg1,.arg2)", MAX([Price]), [TestParam])

As you can see, I try to call the function (myfunc) that I have defined in R.

As a result I get an error message that tells me that myfunc is not found.

By exploring web a bit I found that it is to expect since functions defined in R work-space are not in the work-space of Rserve. So, my question is: Is it possible to define a function in R and then make it visible to Rserver so that Rserve can serve these functions?


Solution

  • Is it possible to define a function in R and then make it visible to Rserver so that Rserve can serve these functions?

    Of course it is. You "just" tell Rserve to

    • source a file with functions (not recommended, read on)
    • load one or more libraries as needed (recommended, also for local code)

    just as you would with any other R session because that is what Rserver offers: headless R sessions.