1

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 Answer 1

2

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: _*)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! how will that handle the ' -> ' operator between parameter name and parameter value?
It''s not an operator. Scala has very few operators. -> is simply a method, defined by implicit conversion on Any, that creates a pair out of two values. a -> b means the same thing as (a, b) but looks nicer. A Map[A, B], when turned into a Seq, becomes a bunch of key-value pairs (A, B).
I'll give it a try! Thanks a million. I'll be sure to upvote and accept when working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.