Skip to content

build a standalone distributable shiny-app with electron

License

Notifications You must be signed in to change notification settings

lsteinmann/shiny-electron

 
 

Repository files navigation

shiny-electron: build a standalone distributable shiny-app with electron

This is a template to build a standalone shiny-app with electron-quick-start. The main part (main.js) is based off COVAIL and slightly adjusted. This template will only work for windows.

In this repository, I merged the updates from electron-quick-start into the fork from COVAIL/electron-quick-start. As long as I use it myself, I will be fetching updates from electron-quick-start.

Some tutorials and a warning

See some other tutorials and approaches to deploy shiny with electron:

I just put this together as a reminer for myself and in the hopes that someone might be able to use it. But I don't actually know javascript, so while this all seems to work just fine, please take it with a grain of salt.

How to make it work

Get a repository with the template and clone it, e.g. into your-app-dir. Download R-Portable from SourceForge and extract the .exe somewhere temporary. From the resulting folder, move the contents of ./App/R-Portable/ into your cloned repositories R-win-port-directory so that it contains: bin, etc, doc, include, library, modules, share, src, Tcl, tests and some files. To safe a moderate amount of space, you can remove the directories doc and tests. Now, you need to install all the packages you will need for your shiny app into ./R-win-port/library. As an example, I tried to do that with ./R-win-port/load_packages.R. You need to run this manually with the R.exe located at ./R-win-port/bin/:

setwd("../")
# check to see if the working directory is 'your-app-dir/R-win-port/'
getwd()
source("load_packages.R")

Choose a mirror. This will install a bunch of packages into the local library. Check if they are actually in the right place, i.e. in ./R-win-port/library/. R should now work. You can try it by setting the working directory to your app directory and running the app, a browser windows with the sample app should open:

setwd("../shiny")
# check to see if the working directory is 'your-app-dir/'
getwd()
source("app.R")
runApp()

If this works, you can now try to make electron run it! To do this, you need Node.js with npm installed. You can find some more general instructions and helpful learning resources at electron-quick-start.

Open your your-app-dir/ in your IDE or a terminal and run:

# While in 'your-app-dir/', install dependencies:
npm install
# Run the app
npm start

If you want to produce a setup and other executables for you app, you need to run:

# Produce executables etc. of the app
npm make

It is configured for windows only, but changing some things in main.js again, you should be able to easily make this work for other operating systems. Just make sure to have the correct R-Portable versions available for every OS and have main.js point at the correct R-executable. electron-squirrel-startup, when making the distributables, will ensure that a desktop shortcut and proper icons are made for the app (on windows; see the relevant documentation for options in forge.config.js).

Using your own app

If you want to include your own app, list your apps dependencies in ./R-win-port/load_packages.R and run it again as explained above. Replace app.R in the shiny-folder with your own app. You can have app.R point to other directories within your-app-dir/shiny/.

Also, though main.js kills the R-process on exit: Remember to include

session$onSessionEnded(function() {
      stopApp()
    })

in the server function of your app, or R/shiny might not stop correctly and block resources untill you force it to quit. For your own custom icon, replace images/quant-icon.ico.

License

Same as electron-quick-start: CC0 1.0 (Public Domain) Icon from icon-icons.com

Languages

  • JavaScript 68.4%
  • R 17.8%
  • HTML 12.8%
  • CSS 1.0%