Skip to content

Instantly share code, notes, and snippets.

private val path = Main.getClass.getResource("fun_MagicHappensHere.R").getPath
Try(rNoBindingsWeatherForecasts(path)) match {
case Failure(f) => print(f)
case Success(s) =>
// turning the Java Map into an immutable Scala Map, same for the Java List.
val resultAsScala: Map[String, List[_]] =
s.asScala.toMap.map(entry => entry._1 -> entry._2.asScala.toList)
// We need to do a bunch of nasty casting, because the returntype is not uniform
val humidities: List[Int] = resultAsScala("humidity").asInstanceOf[List[Int]]
// We need to initialise a GraalContext that will do the mediation between
// the JVM languages and R
val context: Context = Context.newBuilder("R").allowAllAccess(true).build()
// Next, we need to create a Source which needs to know what language it features
// and where to find the code.
val sourceNoBindings: Source =
Source
.newBuilder("R", Main.getClass.getResource("fun_NoBindingsWeatherForecasts.R"))
.build()
generateWeatherForecasts <- function(pathToMagicFile) {
# We're bringing the function contained in the file at the given location into scope
source(paste(pathToMagicFile))
# This returns a dataframe, a way for R to store large quantities of data
# in an ordered manner (kind of like a Database Table...)
weatherForecast <- magicHappensHere()
# Like in Scala, the result of the last statement in an R function is its return.
# Some very impressive R stuff happens here.
# (Well, we're pretending it does, anyway! It's really just a mock...)
# This function returns a data.frame containing a number of
# weather forecasts that we need to bring back to the JVM
magicHappensHere <- function() {
# Omitting mocking code here
(...)
# Like in Scala, the result of the last statement in an R function is its return.
weatherForecasts
@NRBPerdijk
NRBPerdijk / Main.scala
Last active March 22, 2019 13:31
This Main object uses GraalVM to run an R file from Scala
import org.graalvm.polyglot.{Context, Source}
object Main extends App {
// We need to initialise a GraalContext that will do the mediation between the JVM languages and R
val context: Context = Context.newBuilder("R").allowAllAccess(true).build()
// Next, we need to create a Source, which needs to know what language it features and where to find the code.
val source: Source = Source.newBuilder("R", Main.getClass.getResource("funHelloWorld.R")).build()
printHelloWorld <- function() {
# assigning a string to the variable
helloWorld <- "Hello, World!"
#printing the variable
print(helloWorld)
# here we concatenate the created variable with some extra information
# R automatically returns the last statement, so we're done here!
paste(helloWorld, "executed in R")
<dependencies>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>1.0.0-rc14</version>
</dependency>
</dependencies>
@NRBPerdijk
NRBPerdijk / EventSerializerRegistry.scala
Last active September 29, 2017 13:07
EventUpcasting in Scala with Akka Persistence and Lagon Play-Json
object EventSerializerRegistry extends JsonSerializerRegistry {
override def serializers: immutable.Seq[JsonSerializer[_]] = Vector(
JsonSerializer(Json.format[ActualItemPurchasedEvent])
)
private val addDefaultDiscount = JsPath.json.update((JsPath \ "item" \ "discount").json.put(JsNumber(0)))
private val convertCustomerToClient =
JsPath.json.update(
//add the path client \ clientId to the json object, then copy the value of customerId into it
(JsPath \ "client" \ "clientId").json.copyFrom((JsPath \ "customerId").json.pick)