Install R in Android, via GNURoot -no root required!

Playing with my tablet some time ago, I wondered if installing R could be possible. You know, a small android device «to the power of R»…

After searching on Google from time to time, I came across some interesting possibilities:

  • R Instructor, created «to bridge the gap between authoritative (but expensive) reference textbooks and free but often technical and difficult to understand help files«.
  • R Console Free. provides the necessary C, C++ and Fortran compilers to build and install R packages.
  • There’s always possible to root your device and install a Linux distribution for Android, which will let you install any repository/package, just like in any linux console.
  • Running R from your dedicated R server or from an external one (see R-fiddle), using your own browser. I see this option as particularly useful for those who want maximum performance.
  • Some additional thoughts on this topic are also stored in these Stack Overflow pages.
  • Without needing to root my device, I found GNURoot, an app that «provides a method for you to install and use GNU/Linux distributions and their associated applications/packages alongside Android«.

Finally, my preferred solution came with GNURoot (see this tutorial), and here’s how I managed to install the newest CRAN repositories! (NOTE: It should work «out of the box» but, as problems might appear, some experience with Linux is always advisable).

1. Install the .apk of GNURoot in your Android device. Don’t forget to donate if you like it! 🙂

2. Following the app instructions, download and install a linux distribution to run. In my case, I chose the .apk GNURoot Wheezy (a Debian Wheezy distro without Xterms). EDIT: Just be sure of having enough memory for it in your device

3. Once installed, just follow the steps to launch the Rootfs (Wheezy) as Fake Root. You will see a bash prompt, from which you can access a complete linux directory tree. This is the same as if you were in a computer (however, if you aren’t root you won’t be able to access the directories via your file browser from Android)

GNURoot1

4. Now, we just have to update and upgrade:

apt-get update
apt-get upgrade

5. Then, update the sources.list file. We don’t have any graphical text editor (like gedit or kate)… but we have nano!:

nano /etc/apt/sources.list

GNURoot2

Using the volume up + «W/S/A/D» you can move between the lines. Or alternatively, you can install a convenient keyboard with arrow buttons, like Hacker’s Keyboard! (thanks to JTT!)

Following instructions from CRAN, I added the following line to sources.list:

deb http://<favorite-cran-mirror>/bin/linux/debian wheezy-cran3/

Exit saving changes. But before «update and upgrade» again, don’t forget to add the key for the repository running the following:

apt-key adv --keyserver keys.gnupg.net --recv-key 381BA480

5. Update and upgrade…. voilà!

apt-get update
apt-get upgrade
apt-get install r-base r-base-dev

6. Now, you only have to run R just like in any bash console:

RinAndroid

R GRAPHS

With this method you only have a prompt, without any graphical interface. ¿How do I make and see plots here?. If R runs from «inside» Android one option is to connect your Linux to an X-server app (thanks, J. Liebig). However, due to memory issues, I couldn’t put in practice this idea and see what happens. Try at your own risk! 🙂

Fortunately, there’s always possible to print R graphs in various formats, with the inconvenient that you have to browse to the plot’s location in Android -every time you need to check the output.

setwd("/sdcard")
data(cars)
pdf("cars.pdf")
plot(cars)
dev.off()

Here I leave a small script to begin playing with R on Android. Hope you enjoy it!


###########################################
### COMMANDS TO START WITH R ON ANDROID ###
###########################################
# Under GNU-GPL license
# R in Android installed via the apps "GNURoot" and "GNURoot Wheezy", and sources.list updated with the newest CRAN repositories.
# I strongly recommend to set the working directory of R to a folder that can be accessed from Android such as /sdcard
setwd("/sdcard/R")
# Just copy and paste the following commands, and press ENTER to see the results.
# You can recycle these commands to make new ones suitable for your own data!
# Note: the lines of text preceeded by "#" are considered comments, and they are not executed even if they are pasted.
# Let's begin with real data:
setwd("/sdcard/R")
var0 <- c(64,76,66,47,72,82,66,58,64,69,80,57,66,63,71,55,57,71,45,77,69,61,47,55,59,45,61,56,68,74,55,71,72,64,52,62,69,53,76,62,65,54,73,79,74,38,61,73,75,71,74,52,48,45,65,59,79,78,82,61,70,73,68,76,71,65,78,65,80,69,73,70,80,50,67,70,63,68,70,37,73,68,53,73,79,76,68,80,79,78,75,82,72,77,75,66,72,66,66,58,70,71,69,81,74,45,73,81,71,76,79,80,72,64,81,79,74,69,75,56,62,60,83,55,74,47,70,59,47,81,73,59,74,70,69,81,29,77,72,78,68,60,65,83,66,76,68,80,82,79,74,74,22,62,81,75,75,44,72,75,72,77,76,67,70,76,42,56,76,84,75,76,83,73,63,50,78,70,79,74,59,54,74,68,72,82,69,48,64,48,78,62,87,79,65,70,85,80,79,72,71,77,76,79,76,81,82,70,65,79,76,83,75,63,52,80,86,63,68,69,78,82,74,86,69,85,72,64,57,74,78,22,77,64,63,51,74,71,71,47,63,66,73,43,72,75,81,40,56,71,77,77,71,69,68,58,64,76,73,81,73,81,59,68,57,62,66,76,66,47,72,82,66,58,64,69,80,57,66,63,71,55,57,71,45,77,69,61,47,55,59,45,61,56,68,74,52,71,72,64,52,62,69,53,76,62,65,54,73,79,74,38,61,73,75,71,75,82,72,77,75,66,72,66,66,58,81,74,45,73,81,71,76,79,80,72,64,81,
79,74,69,75,56,62,60,83,55,74,47,70,59,47,81,73,59,74,70,69,81,29,77,72,78,68,60,65,83,66,76,68,80,82,79,74,74,22,62,81,75,75,44,72,75,72,77,76,67,70,76,42,56,76,84,75,76,83,73,63)
var1 <- c("1","2","1","2","1","1","1","1","1","1","1","1","1","1","1","2","2","1","1","1","2","1","2","1","1","1","1","1","1","1","2","2","1","2","1","1","1","1","1","1","2","1","1","1","1","1","1","2","2","1","2","1","2","2","1","1","1","2","1","2","2","2","2","2","1","1","1","1","1","1","1","1","2","1","1","1","2","1","1","1","1","2","1","1","1","1","1","2","1","2","1","1","1","1","1","1","1","2","1","1","1","2","1","1","2","1","1","2","2","2","1","2","1","2","1","1","1","1","2","2","2","2","2","1","1","1","1","2","2","1","1","1","1","1","1","2","1","1","2","1","2","1","1","1","1","1","2","1","1","1","1","1","2","1","1","2","1","2","1","1","1","1","1","2","1","2","2","1","2","2","1","1","1","1","1","2","2","1","1","1","2","1","2","1","1","1","1","1","1","1","1","1","1","2","1","1","1","2","2","1","2","1","1","1","1","2","1","1","1","1","1","1","2","2","1","1","1","1","2","1","1","1","1","2","1","2","1","1","2","1","1","1","1","1","2","2","1","1","1","1","1","1","1","1","1","2","1","2","1","1","1","2","1",
"2","2","1","1","1","1","1","1","1","1","1","1","2","1","2","1","1","1","1","2","1","1","1","2","1","2","2","1","1","1","1","2","1","1","1","1","1","1","1","1","1","1","1","2","1","1","1","2","1","1","1","2","2","1","1","2","2","1","1","1","2","1","1","1","1","1","2","1","1","1","1","1","2","1","2","2","1","1","1","1","1","2","1","2","1","1","1","1","1","1","2","1","2","1","1","1","1","1","1","2","2","1","1","2","1","2","2","1","2","2","1","2","2","2","1","2","1","2","2","1","1","1","1","1","1","1","1","1","2","2","1","1","2","2","2","1","2","2","2","2","1","2","1","1","2")
var2 <- c("0","1","0","0","0","0","0","0","0","0","1","0","0","0","0","0","0","0","1","0","1","0","0","0","1","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0","1","1","0","0","1","1","0","0","0","0","1","0","1","1","0","0","0","0","0","0","0","0","0","1","0","0","1","0","0","1","1","0","0","0","0","1","1","1","1","0","0","1","0","0","0","0","1","0","1","0","0","0","0","0","0","0","1","0","1","0","0","1","1","1","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1","1","1","1","1","0","0","1","1","0","0","0","0","1","0","1","0","0","0","0","1","0","1","1","0","0","0","0","0","1","0","0","0","1","0","0","0","0","0","0","1","0","1","0","0","0","0","0","1","0","0","0","0","1","0","0","1","0","0","0","0","0","0","0","1","1","1","0","0","1","0","1","0","1","0","0","1","0","0","0","1","0","0","1","1","0","0","0","0","1","0","1","1","0","1","1","1","0","0","0","0","0","1","0","1","0","0","0","0","1","0","0",
"0","0","0","0","0","1","0","0","0","1","0","1","0","0","0","1","0","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1","0","0","0","0","0","1","0","0","0","0","0","0","0","0","0","0","0","0","1","0","0","1","0","0","0","0","0","1","1","0","1","0","0","1","0","0","0","0","0","0","0","0","1","0","0","0","1","0","0","0","0","1","0","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0")
mydatab <- data.frame(var0,var1,var2) # joins the vectors into a data frame
# Global summary of my data.
summary(mydatab)
# Save the data.frame:
save(mydatab, file="mydatab.RData")
# Now we will print a plot using the pdf() function!
pdf("myplotr.pdf")
hist(mydatab$var0, breaks=30)
dev.off()

view raw

RStartAndroid.R

hosted with ❤ by GitHub