majed
majed

Reputation: 33

call R function directly from R script in Qt

I would like to know if I could call R function already created in R script inside Qt push button.

As Qt is based on C++ language. I know that I can use "RInside" and "Rcpp" libraries to run R inside C++ code. However, this require changing the R function to be compatible with C++ format. In my case, my R functions are already in R script and I do not want to change them or re-write them in R-C++ format.

Is there a way to call the function directly without rewrite it in C++ format?

Upvotes: 1

Views: 226

Answers (1)

Milan Š.
Milan Š.

Reputation: 1638

Usually you already know the answer yourself, you just need to ask yourself the right question:

"How would this work, without changing the C++ compiler?"

C++ is a compiled language, meaning that any application that you write in C++ needs to be compiled into a binary file. The language itself is nothing but instructions for the compiler on what you expect to be the final result.

Looking at rinside on github it is pretty straightforward how to implement your function in C++ using it. And this is to an extent your only option. Here's why:

Scripting languages like Python and R work in a way that they have a main application called an interpreter which you instruct using the aforementioned scripting language on how to call existing functions from (usually) C/C++ libraries. That means that if you write a function in R then you just define a "recipe" for the R interpreter on how it should (sequentially) call these library functions. But your function itself IS NOT PART of said library.

That being said I do not think it is possible to do what you ask, because most likely rinside just takes advantage of the already existing libraries used by the R interpreter and makes them accessible via an interface in C++.

Your best option is reconstructing the function you made in C++ using rinside which looks very user friendly.

There is another option and that is that you would wrap the entire R application and make it part of the C++ application (i.e. you open it from your application and stream your commands into it) but that is much more complicated than just rewriting your function.

EDIT: Just a small note, the rinside library is co-authored by @Dirk Eddelbuettel with whom I had a chance to have a pleasant discussion on Stackoverflow. If you would've add rcpp, rinside or cran as a tag to your question. You would've most likely get an answer straight from an expert in this area.

Upvotes: 3

Related Questions