100

Is there an easy way to compile my R script into standalone .exe file just like what matlab does?

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Joyce
  • 2,517
  • 8
  • 21
  • 21
  • 19
    Very short answer: No. Never has been. – Dirk Eddelbuettel Dec 31 '12 at 02:03
  • 2
    Actually I would like to distribe it but keeping the scripts and algorithm secret, is there a way to encrypt that or any other way to achieve this purpose? – Joyce Dec 31 '12 at 02:40
  • 2
    Lots of discussion at http://stackoverflow.com/questions/4707276/encrypting-r-script-under-ms-windows – Ben Bolker Dec 31 '12 at 02:44
  • 1
    @Joyce, to hide the code, a standard solution is to distribute the application on a server. So, your users would connect via server. Not only would you be able to hide the code, but your users would also always have access to the most updated version. However, you would need to establish server infrastructure (probably best to use an R cloud provider). – Tripartio Mar 23 '17 at 09:32
  • 1
    Creating a package would help to distribute the common code. I too have been looking into creating an exe for mundane tasks/functions, calcs that repeats over again and again. And to share an exe with some who do not use coding/prefer a UI based app. Unfortunately still looking for ways. – bonCodigo May 08 '17 at 22:11
  • 2 I don't see why R can't be compiled, if Python can do it. Python is technically interpreted, but their are compilers that bundle the interpreter with the code and compile into an exe. – CK7 Dec 20 '22 at 11:11

5 Answers5

40

As a matter of fact there is a way to achieve solution that would meet your requirements. Have a look at the article on Deploying Desktop Apps with R on R-Bloggers. As detailed in the article, you will end up using a few more things than a single exe file.

Also I would like to draw your attention to the RGtk2 with use of the RGtk2 you could attempt to develop your own interface in R. If push comes to shove, I trust that you could pack your R code together with a portable version of R and dependencies into one installer and make and app from that, that would create an illusion of a single exe file.

In your question you asked whether it's easy to develop a standalone executable file interpreting R code. I wouldn't say it's easy. If you have a strong desire to run a R code from an application, you could do it in a simpler manner using RCaller for Java or R.NET.

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • 1
    The linked article on deplying desktop apps with R is really good (uses portable apps and shiny!), Thx! – R Yoda Nov 07 '15 at 08:53
  • 2
    @RYoda I'm glad to read that. There is interest in this subject. If you want to stick with Shiny, [this article](http://blog.analytixware.com/2014/03/packaging-your-shiny-app-as-windows.html) provides interesting advice on packaging a Shiny App as a Windows Desktop App. – Konrad Nov 07 '15 at 17:11
  • it is also possible to use RCaller scripting engine throughout the javax.script API, see http://mhsatman.com/rcaller-3-0 – jbytecode May 11 '16 at 12:01
  • 1
    note that the [CRAN listing for RGtk2 is gone](https://cran.r-project.org/web/packages/RGtk2/index.html), so that solution is probably deprecated – Kevin L. Keys Feb 24 '23 at 00:50
12

In response to your comment:

Actually I would like to distribe it but keeping the scripts and algorithm secret, is there a way to encrypt that or any other way to achieve this purpose?

You can (sort of) do this by saving functions using save(). For example, here's a function f() you want to keep secret:

f <- function(x, y) {
  return(x + y)
}

Save it wherever:

save(f, file = 'C:\\Users\\Joyce\\Documents\\R\\Secret.rda')

And when you want to use the function:

load("C:\\Users\\Joyce\\Documents\\R\\Secret.rda")

I would save all my functions in separate files, put them in a folder and have one plain old .R script loading them all in and executing whatever. Zip the whole thing up and distribute it to whoever. Maybe even compile it into a package. Effectively the whole thing would be read-only then.

This solution isn't that great though. You can still see the function in R by typing the name of the function so it's not hidden in that sense. But if you open the .rda files their contents are all garbled. It all depends really on how experienced the recipients of your code are with R.

Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45
6

I have done some research on this issue and a few solutions were implemented in the last few years.

  1. One option is to wrap your app along with a portable R into a container application like Electron. The electron-quick-start project tries this.
  2. The RInno package provides functions to bundle your app and R portable into an installer app. Every user runs the installer on their system once which will install your app, the packages and the code. But in the end users may not see the difference to other apps. They get a link in the start menu and that's it. I did that successfully. But it did not work out of the box. I had to adjust the output manually in several places.
  3. A second container solution works with docker. That is what ShinyProxy does. See also this blog.
  4. The package shinyShortcut (I quote) "will produce an executable file that runs the shiny app directly in the user's default browser".

Important to note: I haven't tested most of them. From reviewing the solutions I often get the feeling that these solutions might make releases somewhat complicated because there are always manual steps involved.

M--
  • 25,431
  • 8
  • 61
  • 93
Jan
  • 4,974
  • 3
  • 26
  • 43
5

One form of having encrypted code is implemented in the petals function in the TeachingDemos package.

Note that it would only take intermediate level programing skills to find the hidden code, however it does take deliberate effort and the user would not be able to claim having seen the code by accident. You would then need some type of license agreement in place to enforce any no peeking agreements.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Thank you Greg, i just look into the function you mentioned, but cannot figure out how to use that to hide the codes, would you kindly elaborate more?Thanks much. – Joyce Jan 02 '13 at 10:16
  • @Joyce, the `petals` function does not hide code, it is one example of having a key piece of code hidden (can you read the key piece?). Other tools are needed to do the encrypting(bleaching). A simpler approach is discussed at: https://stat.ethz.ch/pipermail/r-devel/2011-October/062236.html. Byte compiling may help as well, but you really need some type of license agreement. – Greg Snow Jan 02 '13 at 22:55
4

Well you are going to need R installed on the deployment machine. As for making an executable, I'm not sure that's possible. But you can create another program that invokes your R script. R is an interpreted language. It is not possible.

turnt
  • 3,235
  • 5
  • 23
  • 39
  • 5
    Do we have any news on any R compiler? I started using python and there are easy ways to produce exe files (py2exe and cx_freeze for example). Is there any chance to see something like this in R soon? – Tony Jan 03 '14 at 12:35