1
votes

I am very new to scala.

I am using rscala to integrate with R functions. For example, I am using the following method to call an R function and retreive a String value.

Method Signature:

def invokeS0(function: Reference, args: Any*): String

Method Implementation

val v= rc.invokeS0("doPred","lemmas"->"Communicate,Design,Java,","frequency"->"3,3,1")

My Problem

However, rc.invokeS0 can call ANY function and thus have ANY number of arguments.

I'm thinking of building a wrapper method that takes the function name as a String and arguments as a Map. Perhaps something like this:

private def invokeRStringFunction(functionName: String = "testFunction", args: Map[String,String]): Unit =
{
    /**Iterate through map to generate following code*/
    val v= rc.invokeS0(functionName,mapKey1->mapValue1,mapKey2->mapValue2 etc etc)
}

But I am not sure how to write the code given that the number of arguments is dynamic. Frankly, I am not sure if it is possible, but thought I would check, just in case.

Any hints would be greatly appreciated.

1

1 Answers

2
votes

Convert the Map[String, String] into a Seq[(String, String)] with args.toSeq, and then use the _* ascription to pass it as a sequence of arguments instead of just one.

def invokeRStringFunction(functionName: String, args: Map[String, String]): Unit
= rc.invokeS0(functionName, args.toSeq: _*)