Skip to content

zarathucorp/shiny-electron-template-m1

Repository files navigation

shiny-electron-template-m1

How to Make an R Shiny Electron App

Note

A setup guide by L. Abigail Walter
Instructions adapted from Travis Hinkelman
R Shiny Electron template created by Dirk Shumacher
Template & Instruction updated by Jinhwan Kim

For more info, see previous repository

Tip

For Windows, see this repository

Guide article in R-bloggers (based on 2023.03, may not work in 2024-)

Creating Standalone Apps from Shiny with Electron [2023, macOS M1]

Versions info

NodeJS NPM RStudio R Electron.js macOS


Getting started

A. Environment setup

Note

All of the following steps can be run exclusively in the RStudio Terminal (right to console).

so Terminal means Rstudio's terminal using Option + Shift + m

  1. Install R, Rstudio.
  2. Install Node.js: from offical page
  3. Install Electron Forge using npm (npm is installed with Node.js)
  4. In the Terminal, run sudo npm i -g @electron-forge/cli, (sudo requires password)

Note

Check your versions of node and npm in Terminal with node -v, npm -v.

  1. Give Star ⭐, Fork this repository to your own account. Then clone it to your local PC

B. Elecron project

  1. Open an R project with cloned repository's .Rproj file.
  2. In your project directory (may Github/shiny-electron-template-m1), install Electron locally by running npx create-electron-app <APPNAME>.

Warning

You can not use app as <APPNAME>
Assume using myapp in this tutorial

  1. In your myapp folder, delete src directory
  2. Copy (or move) below files to your myapp folder:
  • get-r-mac.sh: For install local R for electron app.

  • add-cran-binary-pkgs.R: For install R packages into your project locally

  • start-shiny.R: Let electron call your shiny app

  • Folder shiny from this template, containing:

    • shiny/app.R: THIS IS YOUR SHINY APP'S CODE
  • Folder src from this template, containing:

    • src/helpers.js
    • src/index.css
    • src/index.js
    • src/loading.css
    • src/loading.html
    • src/main.js: configure shiny electron app (like width, height)
  1. Change your directory to new app folder cd myapp with Terminal

C. Setup R

  1. Install local R with sh ./get-r-mac.sh
    • First, check the version of R on your machine. In the R Console, run version.

Warning

Your PC's R version and electron's R version must same

  1. Build app.R as your application's code.

Note

not only app.R also other required files for shiny application (like /www)

  1. If you don't have the automagic package installed, run install.packages("automagic") in the console.

  2. In the Rstudio terminal, run Rscript add-cran-binary-pkgs.R to get packages for R.

Note

If your shiny application uses not-CRAN packages (like github / bioconductor), See Add not-CRAN packages below.

D. Setup electron

  1. Change package.json as [fix] packages-json's content. And modify author information.

Note

Node package's version confirmed in 2024.01

  1. Change forge.config.js as [fix] forge.config.js's content.

  2. Run sudo npm install in Terminal to add new dependencies you listed in package.json to the node_modules folder.

E. Build shiny.exe

Note

You need to run step after this, whenever you want to update shiny application.

  1. Test your shiny application work by electron-forge start in Terminal.

  2. If error occurs while electron-forge make with conf.d Delete r-mac/fontconfig/fonts/conf.d. see this issue

  3. If the app runs successfully, congratulations! Create the .exe(as ZIP) on the electron-forge make in Terminal. Your app can be found in the /out folder. (Strongly recommend to change read-only property with mac's Finder)


Additionals

  • Raise an issue, please.

Error with require() of ES Module

It doesn't affect to run shiny.

Add not-CRAN packages

  • manually copy library from your Local's R library to r-mac/library, You can check Local's R library with .libPaths() in R console.

Infinite loading in Electron-forge start / make

  • First, change app.R code as basic example.
  • If this works well, your app.R code has problem. In my case, I updated R package in shiny application but not in library of R-win directory.
  • So update them with recent version, and try again will work.