Phoning it in with R

Running R and Emacs on my mobile phone

Recently I have been running R from my Android phone. There are some apps on the Google Play Store that seem to let you emulate R, or connect to a remote version. Instead of doing that, I have been running R directly off my phone using the terminal.

I tried a couple things, but I found the Termux app to be the most stable solution. With Termux I’m able to emulate a Linux command line and call programs like R and Emacs.

I told Dr Hua Kiefer about it, but she wasn’t so impressed:

emacs with R
Fig.1 - Making Chartz

Why bother

Why bother with this at all?

Usually I am not far away from my desk and my reliable computer. Do I really need to run it from my phone? What is the added benefit? There are a few reasons

  • It’s cool
  • Having R with you is handy, you can
    • Look up stats on the fly (no dealing with clunky UI if data API available)
    • Create custom plots on the fly
    • Run simulations on the go
  • With Emacs you can
    • Edit text files wherever you are
    • Hone your Emacs text editing skills

Since I’ve got my phone with me most times, now can have R with me most times. That makes me happy.

Keyboards

The default keyboard on my Android Phone is good for making good Tweets or sending text messages but not good for editing files. I found two solutions:

  • The Hacker Keyboard app LINK
  • A Bluetooth keyboard (I got this one Amazon, it seems all right)

Following helpful suggestion online I installed the Hacker Keyboard. This replaced my puny keyboard with a much more robust version.

As good as the Hacker Keyboard is I also bought a little portable Bluetooth keyboard that can pair with my phone. The version I bought is nice and compact. The only downside is the “t” and “v” keys are a little smaller to accommodate the joints where the keyboard folds. That means I tend to miss those keys if I don’t push them hard enough. I think I’ll get used to it and adapt.

Emacs

I usually use the Rstudio IDE, but as far as I know it is not available on mobile. Fortunately Termux comes with Emacs readily available. After installing it I fired it up and in short order had my first program:

emacs with R
Fig.2 - A first program

I used to use Emacs years ago, mostly for SAS code. As I haven’t written much SAS code lately I haven’t used Emacs much.

Messing around over the past few days I have begun to recall the Emacs that I forgot. It’s a joy to work with, reminding me of simpler times.

Plus it has games:

emacs with R
Fig.3 - Tetris on Emacs

How to do it

I am afraid this cannot be a comprehensive guide. The steps I took might not work for your particular setup. I had to use a lot of trial and error to get things working.

However, after some searching (starting with this Reddit post: R on the go) I found that staying pretty close to the steps outlined in the Termux Wiki I was able to get things working. There’s still a few places were things are not working (I have trouble installing certain packages from CRAN). But in general things seem to be working pretty well.

What to do with it

Now that I was up an running, what could we do besides play Tetris?

Fortunately I was able to get the quantmod package working. That means I can easily got to FRED and get economic series.

emacs with R
Fig.4 - Getting going

FRED of course, has a nice application. But I find customizing charts on the phone not the best experience. By loading the data into R we can write our own custom script.

library(tidyverse)
library(lubridate)
quantmod::getSymbols('MORTGAGE30US', src='FRED')
df <-data.frame(date=time(MORTGAGE30US),MORTGAGE30US)
png(filename='mtg2.png', height=500,width=800)
ggplot(data=df%>% filter(year(date)>2009),
       aes(x=date,y=MORTGAGE30US))+
  geom_line(color='dodgerblue',size=1.2)+
  theme_minimal(base_size=18)+
  theme(plot.caption=element_text(hjust=0))+
  geom_hline(linetype=2,
             color='dodgerblue',
             aes(yintercept=last(MORTGAGE30US)))+
  labs(x='',y='rate %',
       title='30-year fixed mortgage rate (%)',
       caption='@lenkiefer Source Freddie Mac')
dev.off()
R mortgage rate chart
Fig.5 - Mortgage Rate Chart

Most of this post was written on my phone via Emacs