Site icon R-bloggers

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

[This article was first published on R-posts.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I assume thatā€¦

1. Why standalone shiny app?

First, letā€™s talk about the definition of a standalone app.

Iā€™m going to define it this way

An app that can run independently without any external help.

ā€œExternalā€ here is probably a web browser, which means software that can be installed and run without an internet connection.


Rstudio can also be seen as a kind of standalone, as you canā€™t install packages or update them if youā€™re not connected to a network, but you can still use them.

Creating a standalone app with shiny is really unfamiliar and quite complicated. What are the benefits, and why should you develop it anyway?

I can think of at least two.


1. better user experience
Regardless of the deployment method, when using Shiny as a web app, you have to turn on your web browser, enter a URL, and run it.

The process of sending and receiving data over the network can affect the performance of your app.


However, a standalone app can run without a browser and use the OSā€™s resources efficiently, resulting in a slightly faster and more stable execution.

Of course, the advantage is that it can be used without an internet connection.

2. Improved security
Shiny apps run through a web browser anyway, so if a ā€œlegendaryā€ hacker had their way, they could pose a threat to the security of Shiny apps.

However, standalone is a bit immune to this problem, as long as they donā€™t physically break into your PC.

2. Very short introduction of electron

Electron (or more precisely, electron.js) is a technology that allows you to embed chromium and node.js in binary form to utilize the (shiny!) technologies used in web development: html, css, and javascript, to quote a bit from the official page.

Itā€™s a story I still donā€™t fully understand, but fortunately, there have been numerous attempts by people to make shiny standalone with electron.js before, and their dedication has led to the sharing of templates that remove the ā€œrelatively complexā€ process.

The article I referenced was ā€œturn a shiny application into a tablet or desktop appā€ by r-bloggers, written in 2020, but times have changed so quickly that the stuff from then doesnā€™t work (at least not on my M1 MAC).

After a considerable amount of wandering, I found a github repository that I could at least understand. Unfortunately, the repository was archived in April 2022. There were some things that needed to be updated for March 23.

Eventually, I was able to make the shiny app work as a standalone app.

And Iā€™m going to leave some footprints for anyone else who might wander in the future.

3. Packaging shiny app with Electron

Itā€™s finally time to get serious and package shiny as an electron.

Iā€™ll describe the steps in a detailed, follow-along way where possible, but if you run into any problems, please let me know by raising an issue in the repository.

(Iā€™ve actually seen it package as a standalone app utilizing the template by following the steps below)


1. the first thing you need to do is install npm.js, npm, and electron forge using Rstudioā€™s terminal. (Iā€™ll skip these)

2. fork/clone (maybe even star ) the template below

https://github.com/zarathucorp/shiny-electron-template-m1-2023


3. open the cloned project in Rstudio (.Rproj)

4. if you get something like below, except for the version, you are good to go.



Now start at line 6 of readmd (of template).


Letā€™s name the standalone app we want to create (obviously) ā€œhelloworldā€

Iā€™ll format directory like /this

5. Run npx create-electron-app helloworld in the terminal to create the standalone app package. This will create a directory called /helloworld, delete /helloworld/src.

6. move the templateā€™s files below to /helloworld and set the working directory to /helloworld.

7. in the console, use version to check the version of R installed on your PC. Then run the shell script sh ./get-r-mac.shĀ in the terminal to install R for electron. (The version on your PC and the version of R in sh should be the same)

8. Once you see that the /r-mac directory exists, install the automagic R package from the console

9. modify the package.json (change the author name of course) The parts that should look like the image are the dependencies, repository, and devDependencies parts.



10. develop a shiny app (assuming youā€™re familiar with shiny, Iā€™ll skip this part)

11. install the R package for electron by running Rscript add-cran-binary-pkgs.R in the terminal.

12. in a terminal, update the package.json for electron with npm install (this is a continuation of 9)

13. in a terminal, verify that the standalone app is working by running electron-forge start

If, like me in the past, the electron app still wonā€™t run, exit the app, restart your R session in Rstudio, and then run the standalone app again. (It seems to be an environment variable issue, such as Rā€™s shiny port.



14. once youā€™ve verified that start is running fine, create a working app with electron-forge make.


Voila, you have successfully made shiny a standalone app using electron.

4. Summary

If Iā€™ve succeeded in my intentions, you should be able to use the
template to make shiny a standalone app using electron in 2023 on an m1 mac.

That app (delivered as a zip file) now makes
Since electron is technically electron.js, my biggest challenge in creating a standalone app with electron was utilizing Javascript (which I have limited skills in compared to R).

Fortunately, I was able to do so by making some improvements to the templates that the pioneers had painstakingly created.

Thank you L. Abigail Walter, Travis Hinkelman, and Dirk Shumacher
Iā€™ll end this post with a template that I followed up with that I hope youā€™ll find useful.

Thank you.

(Translated with DeepL )


Creating Standalone Apps from Shiny with Electron [2023, macOS M1] was first posted on March 21, 2023 at 7:36 pm.
To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version