It has been said that I have a tendency to try to do all computing on one of my raspberry-pi single-board-computers from navigation chart-plotter to weather display and data logging to HDTV media server. I love the amazing flexibility and broad range of computing tasks that can be done on this lilliputian device. But alas, I have gone to the dark-side (according to friends) and have acquired a Samsung S-8 cell phone which is a pretty capable device — especially considering its size and portability. One thing that I did on the raspberry-pi but couldn’t do on the S-8 was to use R. Of course, I wanted to know how to use R on Android.
On the raspberry-pi, I usually used Emacs and the Emacs Speaks Statistics (ESS) package. Anyone with Emacs experience knows that Emacs can do anything — sort of the original universal computer interface. ESS allowed me to either run R locally via the installed R-Cran packages or for computational jobs bigger than the little pi was capable of, connect remotely to my VPS and run them there. Installing R locally on the S-8 seems to be a problem — if not impossible unless I root the phone — so I opted for the next best thing — running R remotely on my VPS. I am a big fan of running your own services on your own VPS. Additionally, my S-8 has a great data plan so connectivity isn’t a problem. I did decide to rethink the interface and perhaps use something other than Emacs.
The obvious solution to R on Android was to install the server version of RStudio on my VPS since I already use the normal version of RStudio on both work and home computers. Installing the packages for the free version of RStudio server went without a hitch, but you do need a basic R-Cran install on your VPS before installing RStudio server as documented in the RStudio instructions. One limitation of the free version is that it doesn’t support SSL — passwords when logging on are encrypted, but the session as a whole isn’t encrypted. There is an easy fix to this — run RStudio inside a Nginx web server. This way, Nginx handles the SSL connection and passes the decoded contents to private local port 8787 on the VPS where the RStudio server is listening for a connection. I already had Nginx serving up a personal domain that has an SSL certificate and decided that domain was an ideal location for my personal RStudio server to enable using R on Android. The new location block code to add RStudio server to an existing Nginx website site configuration is as follows:
location /rstudio/ {
rewrite ^/rstudio/(.*)$ /$1 break;
proxy_pass http://localhost:8787;
proxy_redirect http://localhost:8787/ $scheme://$host/rstudio/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
}
Along with the following in the root of the html block of the Nginx configuration:
map $http_upgrade $connection_upgrade {
default upgrade;
” close;
}
Added advantages of using Nginx as the front-end for RStudio server include the ability to easily share the domain with other services. I decided to use the format of example.com/rstudio for accessing my R server as shown in the above code-block. This way I can have example.com/photos for photos and /whatever for whatever else and easy expandability to add all kinds of services I want to host on my VPS.
Another limitation of the free version of RStudio server is that each user can only have one R session at a time, but if you are running this on your own VPS like I am, you can have a different personal login account on the VPS for each type of R session you plan to run concurrently. RStudio server will automatically allow you to log into each of those accounts using the system username and password as soon as they are created, but you will need to install any custom R packages you use in each user account. One thing I really appreciate about having RStudio server on my personal VPS is that I can start using R on Android and then later when I am at home or work, use my regular computer to access my RStudio server and pick up the analysis right where I left off and the computations are secure and private. That is a great example of the convenience possible in the connected world especially if you have your own VPS.