I assume that…
- You’re famillar with R / shiny
- You know basic terminal command.
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 experienceRegardless 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 securityShiny 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
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
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 /this5. Run
npx create-electron-app helloworldin 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.
versionto check the version of R installed on your PC. Then run the shell script
sh ./get-r-mac.shin 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
automagicR 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
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.Rin the terminal.
12. in a terminal, update the
package.jsonfor electron with
npm install(this is a continuation of 9)
13. in a terminal, verify that the standalone app is working by running
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
Voila, you have successfully made shiny a standalone app using electron.
4. SummaryIf 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
- the power of R / Shiny available to people with little experience
- without installing or using R.
- Or even in a “closed environment” with no network connection
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 ShumacherI’ll end this post with a template that I followed up with that I hope you’ll find useful.
(Translated with DeepL )
Creating Standalone Apps from Shiny with Electron [2023, macOS M1] was first posted on March 21, 2023 at 7:36 pm.