Using R on Android
I just discovered a way to get R running on my smartphone, with full support for packages, graphics and R Markdown, and no need to connect to an external server. This is really handy for quickly checking R code, trying out ideas and writing blog posts on the go. It works quite well!
Here I will show you how to do the same on your Android device. This post was inspired by this answer on StackOverflow.
Initial setup
Install GNUroot Debian from the Google Play Store. This application effectively gives you a full Linux environment within Android, without rooting your device. It just works.
GNUroot Debian emulates a command line interface or “X Terminal” like on a regular desktop PC. You can work in multiple windows, and you have access to all the files on your Android system.
You can use it not only to run R on your phone, but also Python or any other command-line tools supported by GNU/Linux.
Installing R
To install R, type the following commands in GNUroot Debian, hitting Enter after each line and waiting for the respective processes to finish.
apt-get update
apt-get upgrade
apt-get install r-base r-base-dev
Now you have R installed on your phone.
Interactive R commands
Starting an R session is as simple as typing
R
into the terminal.
You will know R is running because the start of the command prompt will change from a hash, #
, to a greater-than sign, >
.
This is just like the usual interactive R user interface (because it is the usual interface).
Run commands and load and install packages to your heart’s content.
To quit R and return to the Linux command line, type
q()
and hit Enter.
You can run quick R one-liners without diving into a full R session, by using the following syntax.
Rscript -e "rnorm(5)" # five random numbers
Rscript -e "print('Hello, world')"
Be careful about matching quotation marks.
You can also use the syntax
R -e "rnorm(5)"
R -e "print('Hello, world')"
which does the same thing but prints more verbose output to the console.
Pressing Up and Down on your keyboard lets you cycle through recently used commands, saving you from needing to retype repeated or very similar commands. Usually touch-screen keyboards don’t have arrow keys, but you can install the Hacker’s Keyboard for Android, which does (at least in landscape mode).
Running scripts
To write nontrivial programmes and not lose your work, you will want to write and run R scripts from files. While you could open a Linux text editor within GNUroot, this is probably more trouble than it’s worth. Dedicated Android text editors are better optimised for use with a small touch screen and don’t need arrow keys or special control commands to work. QuickEdit is a free text editor with syntax highlighting for R and Markdown, line numbering and other useful features.
In your text editor, write a basic R script, like the following.
x <- "Hello"
y <- "world!"
cat(x, y, sep = ', ')
Save the file as test.R
and put it somewhere easy to find, like your Documents
folder.
Reopen GNUroot Debian. Navigate to the directory where you saved your R script file. In my case, I had saved it to Documents, which I reached using
cd sdcard/Documents
The command cd
means “change directory”. Type ls
for a list of files and folders in the current directory. If you make a wrong turn, simply type
cd ..
to go up (back) one folder.
Once you’ve found your R script file, evaluate it with
Rscript test.R
or
R -f test.R
for more detailed output.
If you prefer working in R to in a Linux terminal, then you can equivalently do everything within an R session:
getwd() # show current directory
list.files() # equivalent to ls
setwd('sdcard/Documents') # equivalent to cd
source('test.R') # run the script
Saving output
If your scripts produce output, like plots saved as image files or data in csv files, you can open these with ordinary Android apps and share them. Just keep track of where the files are saved.
For example, here is a short script that produces a plot and saves it as a PNG.
n <- 1000
g <- 16
cx <- rep(1:4, each = 4) * n * 2
cy <- rep(1:4, times = 4) * n * 2
t <- 1:1000
x <- rep(t * cos(t), each = g) + cx
y <- rep(t * sin(t), each = g) + cy
png("test.png", width = 600, height = 600)
par(mar = rep(0, 4))
plot(x, y,
col = rep(c(2, 4), each = g),
asp = 1,
axes = FALSE)
dev.off()
Run the script and use ls
or list.files()
to verify that a new file, test.png
has been created.
In your Android launcher, go to Files > Local > Internal storage > Documents (the names might vary depending on your device) and you should see your new plot, which you can open and share like any other image.
R Markdown on Android
Rendering/knitting R Markdown documents requires the R package knitr and the external application pandoc. Install the former using
install.packages('knitr', dependencies = TRUE)
If you accidentally choose a CRAN mirror that doesn’t work, you can select another one with
chooseCRANmirror()
You also need pandoc. Install it from the command line with
apt-get install pandoc
After installing, you might need to restart your terminal to ensure pandoc has been properly added to the PATH (and so can be found by R).
Write a minimal R Markdown document in your Android text editor, for example
# Hello, world!
I am an *R Markdown* document.
A horse has `r 2+2` legs.
Here is some random noise.
```{r}
plot(runif(100), runif(100))
```
Convert it using
Rscript -e "rmarkdown::render('test.rmd')"
And using ls
or list.files()
or your Android file explorer, you should see the output Markdown, HTML, Word, or PDF document, which you can open in your Android text editor, browser, word processor or PDF reader, respectively.
Once again, share or upload it like any other file.
So there we have it: R and R Markdown running on Android! I hope you found this useful. This blog post was written on a Huawei Honor 8 smartphone using QuickEdit text editor.