Convert R code to Kotlin

Hi Team,
Could you help me how we can convert R code Kotlin as one of project we are working on data science project which is written in R and it is taking more time.some of audit team suggested to go with Kotlin language.Could you please suggest us how we can go head with converting R to Kotlin

R is dynamically typed, interpreted, and lazily evaluated. Kotlin isn’t any of those things. It has also probably got a lot of features that Kotlin doesn’t have, so sadly there isn’t a 1 to 1 conversion button that will just magically fix all the performance problems. I’d suggest that you take a look either at Kotlin scripting or Kotlin Native and learn how Kotlin itself works, and then migrate your codebase by rewriting the code in Kotlin.

It also seems that R has some interop capabilities with Java, and so you might be able to migrate only the performance critical parts into Kotlin JVM and call that from your R code.

Maybe also take a look at pqR?

1 Like

what is pqR?

1 Like

Thank you so much.I looked into R code and they are using some library dplayer, dataframe etc.you mean to say we need to rewrite these library in Kotlin.

1 Like

Probably yeah. I’d say that you should try to figure out which of these libraries is causing performance issues and then rewrite it. If the library is open source, you could even fork it and then replace only the code that causes performance issues with Kotlin code. In other words, basically try to rewrite the parts that are either run a lot (usually called hotspots), or that do really intensive tasks with Kotlin code and call that Kotlin code from R

1 Like

http://www.pqr-project.org/

1 Like

Can we call R code run in JVM Kotlin using pqR?

That’s cool to hear Kotlin was recommended for your project!

Kotlin is a language that combines many of the best features from other modern languages in a cohesive and pragmatic mix. (it’s been said that Kotlin doesn’t invent many new ideas but combines the best ideas proven from other languages).
If your team hasn’t yet programmed in a similar language to Kotlin then you’ll learn a lot adding Kotlin to your skillset.

The idea that you may be able to only change some of the R code to Kotlin is an idea most of us are familiar with on the Java/Kotlin side. Kotlin was made to live along side Java–allowing projects to write new or only some code in Kotlin (which for large applications is a much easier sell to management).


When you say “the project is taking more time”, do you mean the program’s performance is slow? It might be worth finding out which part of program has the most impact on performance before making optimizations. If something must be O(n^2) it’ll also be O(n^2) in any other language.

Performance assumptions are often wrong. Even ahead-of-time-compiling is on average real-world use cases slower compared to JIT. Kind of like the myth that “C is fast so if we write in C our code will be fast”, don’t assume your code will run faster in Kotlin without first considering if the main reason for slow execution is an algorithmic one.


How large is the project? Is your team highly experienced in R already?
If the cost to change to Kotlin is low enough, the benefits to developer productivity and performance may be enough use Kotlin even if there are other algorithmic issues.

2 Likes

Performance-wise Kotlin is in general will be faster. R is highly optimized for some specific statistics tasks, so some instruments could be better there, but if we are talking about actual software development, kotlin has several benefits both in terms of development instruments and performance. Of course, kotlin/java ecosystem requires more experience to work with than python/R ecosystem.

If you are interested in learning kotlin, the starting point for this kind of work is this page. There are some materials about migration to kotlin from Python (which mostly shares the ecosystem with R). Also you can ask your questions in slack in #science and #datascience channels.

3 Likes

In R it is taking 6 to 8 minutes to complete one task so we are planning to chunk the data and process it parallel in Kotlin using co routines but still find same amount of time in Kotlin.is it we are missing something in Kotlin

1 Like

Parallel programming is not a simple thing to master in any language (Java/Kotlin parallel computing is much easier than any other languages I know, but it still requires some expertise). You won’t get parallel code execution just by running something in coroutine. You need to perform asynchronous start and then join the results. The parallel flow processing is coming in future and it will simplify some things. You can use it now, but you still need to understand what are you doing. The simplest way to perform parallel processing right now is to use Java parallel stream API. You can use it directly from kotlin.

2 Likes