将 R 脚本编译成独立的.exe 文件?

有没有一种简单的方法可以像 matlab 那样将我的 R脚本编译成独立的 .exe文件?

100047 次浏览

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.

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.

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.

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.

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.

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.