I built Docker like executable .exe for RShiny!

Analytics and Automation
  • Pulkit Saraf
  • 23-11-2020
I built Docker like executable .exe for RShiny!

In 2018, I started programming in R. We were building a network optimization solution for an African telecom client. There were few hiccups, few glitches but all in all, we successfully deployed version 1 of the tool on their internal servers for use.

Back in India, bolstered by the success of our deployment, we decided to upgrade the front end/ user dashboard from tcltk package to RShiny package in R. It’s much more appealing visually.

5 of us build it bit by bit and it came together beautifully. We informed the client that we would be deploying version 2 with an upgraded dashboard; they were happy about it too. One out of the 5 of us, Yashodhan, decided to take it a step further and build this entire thing as an executable .exe file.

Before he left the organization, he got it to work once on his system but it wasn’t replicating on other systems. This was fine because we only needed to install this once on their central servers for all client employees to use.


In April 2019, we got an addendum to the project. The client wanted us to build another tool which was leaner, had a new layer of data to optimize and the biggest of all, should be operable across personal laptops of all clients without us manually installing, taking 2- 3 days per system.

While our collective network knowledge was sufficient to build this addendum, no one in the team knew how to make this operable on personal laptops executable by itself. With our given knowledge, we would have sent 40 code files and a 100 page PDF on how to install and still ended up answering phone calls from almost all client employees for errors and failures.

We had a good time cushion before this was to go live so I decided to build an executable file which worked across all client systems without failure by itself. What I ended up building was an executable Docker like layer within which the tool ran irrespective of whether the system had required packages, softwares or other settings.

This was one of the most technologically advanced stuff I’d ever built and the most satisfying by far. The results? Excellent. Zero failures across all client systems and I’m fairly certain it’s being used for other projects.

How it works

There’s an outer layer of a bash script that prepares the system. The script checks for environment variables, whether or not R is installed on the system and pastes .Renviron and .Rprofile files among other dependent files on the client system.

It pastes latest packages and executes around 50 lines of code before launching the tool in this artificial environment. With 100% success rate.

There’s odd curveballs such as incompatibility between plotly and Edge browser for first launch instance and typical R issues but otherwise, it did what docker does exactly in 40–50 client laptops in 4–5 African countries.


CODE

Main File
@echo off
set rpath=empty
set flag1=0
set flag2=0
set foundr=no
set rispresent=no
set filesize=0
set writepermission=no
set foundinuserprofile=no
set chance2filesize=0
set endtool=0
echo x=msgbox(“The tool will configure system settings. Click OK to proceed.”,0,”Tool”) >> msgbox1.vbs
echo x=msgbox(“R was not found on your system. Please contact your IT administrator for installing R. The tool will close now.”,0,”Tool”) >> msgbox2.vbs
echo Starting Tool
where /R “C:\Program Files\R” Rscript > chance1.txt
for %%I in (chance1.txt) do set chance1filesize=%%~zI
if exist “%USERPROFILE%(do not delete)\do_not_edit.txt” set chance2filesize=1
if “%chance1filesize%” GTR “0” set rispresent=yes
if “%chance1filesize%” GTR “0” set filesize=1
if “%chance1filesize%” GTR “0” set /p rpath=<chance1.txt
if “%chance1filesize%” GTR “0” set /a flag1=1
if “%chance2filesize%” GTR “0” set rispresent=yes
if “%chance2filesize%” GTR “0” set filesize=1
if “%chance2filesize%” GTR “0” set /p rpath=<“%USERPROFILE%(do not delete)\do_not_edit.txt”
if “%chance2filesize%” GTR “0” set /a flag2=1
set c=%flag1%+%flag2%
Cls
echo Preparing to launch the Tool
if %c% EQU 0 msgbox1.vbs
Cls
if “%rispresent%” EQU “no” echo Searching for R on your system. If not found, you will be prompted to install the same
if “%rispresent%” EQU “no” echo This may take some time. Do not close this window
if “%rispresent%” EQU “no” echo Processing…
if “%rispresent%” EQU “no” ((for /f “skip=1” %%x in (‘wmic logicaldisk get caption ^| findstr /r /v “^$”‘) do where /R %%x\ Rscript) > rscriptpath.txt)
if “%rispresent%” EQU “no” ((findstr Rscript.exe rscriptpath.txt) > cleanrscriptpath.txt)
if “%rispresent%” EQU “no” for %%I in (cleanrscriptpath.txt) do set filesize=%%~zI
if “%rispresent%” EQU “no” if “%filesize%” GTR “0” set /p rpath=<cleanrscriptpath.txt
if “%rispresent%” EQU “no” if “%filesize%” GTR “0” if not exist “%USERPROFILE%(do not delete)\do_not_edit.txt” copy cleanrscriptpath.txt “%USERPROFILE%(do not delete)\do_not_edit.txt”
Cls
if “%filesize%” EQU “0” echo R was not found on your system. The Tool will attempt to perform R installation. This may take several minutes.
if “%filesize%” EQU “0” echo Please choose default settings as far as possible in the setup options
if “%filesize%” EQU “0” pause
if “%filesize%” EQU “0” rsetup.exe
if “%filesize%” EQU “0” where /R “C:\Program Files\R” Rscript > rchance.txt
if “%filesize%” EQU “0” for %%I in (rchance.txt) do set rchancefilesize=%%~zI
if “%filesize%” EQU “0” Cls
if “%filesize%” EQU “0” if “%rchancefilesize%” GTR “0” set foundr=yes
if “%filesize%” EQU “0” if “%rchancefilesize%” GTR “0” set /p rpath= rscriptpath.txt)
if “%filesize%” EQU “0” if “%foundr%” EQU “no” ((findstr Rscript.exe rscriptpath.txt) >rcleanrscriptpath.txt)
if “%filesize%” EQU “0” if “%foundr%” EQU “no” set /p rpath=<rcleanrscriptpath.txt
if “%filesize%” EQU “0” if “%foundr%” EQU “no” if not exist “%USERPROFILE%(do not delete)\do_not_edit.txt” copy rcleanrscriptpath.txt “%USERPROFILE%(do not delete)\do_not_edit.txt”
Cls
If “%rpath%” EQU “empty” msgbox2.vbs
If “%rpath%” EQU “empty” set endtool=1
If “%rpath%” EQU “empty” pause
If “%endtool%” EQU “0” set command1=”%rpath%” Install.R
If “%endtool%” EQU “0” set command2=”%rpath%” Tool.R
if exist chance1.txt del chance1.txt
if exist chance2.txt del chance2.txt
if exist rchance.txt del rchance.txt
if exist rscriptpath.txt del rscriptpath.txt
if exist cleanrscriptpath.txt del cleanrscriptpath.txt
if exist rcleanrscriptpath.txt del rcleanrscriptpath.txt
if exist .RHistory del .RHistory
if exist msgbox1.vbs del msgbox1.vbs
if exist msgbox2.vbs del msgbox2.vbs
if exist .Rprofile del .Rprofile
Cls
If “%endtool%” EQU “0” echo Adding Packages to R. This may take some time. Do not close this window
If “%endtool%” EQU “0” echo Processing…
If “%endtool%” EQU “0” %command1%
Cls
If “%endtool%” EQU “0” echo The Tool works in your browser. Do NOT close this window for the duration of tool use
If “%endtool%” EQU “0” %command2%


Contact me for integration with your code: pulkit.saraf332@gmail.com

Thanks for reading!