[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I assume thatā¦
-
- Youāre famillar with R / shinyĀ
- You know basic terminal command.
- Youāve heard about node.js, npm, or Javascript somethingā¦
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
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.
-
- start_shiny.R
- add_cran-binary_pkgs.R
- get-r-mac.sh
- /shiny
- /src
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 console9. 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
. 4. Summary
If Iāve succeeded in my intentions, you should be able to use thetemplate 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
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 ShumacherIā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.