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

42 comentarios en “Install R in Android, via GNURoot -no root required!

  1. Have you considered installing Hacker’s keyboard from the Google Play, since it introduces arrow keys, which makes editing and moving around in command line a bit easier than the standard keyboard?

    • yes! I knew about this great app, and if I continue to mess around with R in my tablet, I will definitely have to install it! Thanks (will do a small edit in the post according to this idea)

  2. Very, very cool. Thanks for that post. I’ll bookmark it an will read it, at some point, more thoroughly.

    I see a future where during a scientific conference all x64 enabled Android devices will be connected by ad-hoc WiFi, running R instances on their free cores, contributing their computing power to solve particularly nasty equations in global datasets, gridcell by gridcell…

    Well, probably not. But it’s still cool to be able to run R on your mobile. ^_^

    • Hehe…yes, and maybe someday will be useful for solving small problems as you go…anyway, it’s cool and fun!
      Maybe R in a tablet will be better for users who would sacrifice performance for some portability.. not to mention the possibility of accessing an R server from your browser.. it seems that we will have plenty of options in the future!

  3. Cool, will have to give this a go. Maybe you can install rstudio server and then access the graphical interface through the browser? Then you’ll have easy access to plots etc.

  4. Nice, works for me on a cheap tablet! Managed to install ggplot2, dplyr and devtools as well (this latter required libcurl4-gnutls-dev), though it took some time… 🙂 A suggestion: maybe you should extend Step 5 with `apt-get install r-base`.

    • My tablet is also a low-end one, and it works fine. However I currently have memory issues, so I could only install a few packages. -including ggplot2 and gdata to import excel files-.
      Thanks, I will update the post with your suggestion!

    • How do you get dplyr and devtools installed? When I try to install it via install.packages, I get the error message «package … is not available (for R version 2.15.1)».
      Is there a way to upgrade R from within this Debian distribution to R 3.x?

      • I have currently little disk space to reinstall and test this, but I think that you may have the version of R (2.15) that comes native 6with Wheezy.
        The point of this tutorial is to add an updated CRAN repository to the sources.list file, to get the last version of R.
        As this Debian distro doesn’t have X (window/graphical system), one solution is to use Nano or Pico, both text editors that work from the console.
        Once sources.list is ready, the process is similar to any Linux installation of R, it should update and upgrade itself using the update/upgrade commands.
        If sources.list updating doesn’t work (for whatever reason), you can always download a previous version of the dplyr package manually and try to install from the R console: install.packages(path_to_file, repos = NULL, type=»source»)
        And the old sources of dplyr that probably work with R 2.15: https://cran.r-project.org/src/contrib/Archive/dplyr/
        Hth

  5. Hi, it sounds great!
    However I got stuck at point 3. I was able to launch wheezy as fake root, but then it says
    bash: dircolors: command not found
    root@localhost:/#
    when I write ‘apt-get update’ and press enter, it again says ‘command not found’.
    I have probably missed something obvious, but I have no experience with Linux. Will be greatful for any advice!
    Vit

    • Hi,
      I don’t really know what can be happening here, but I would do the following to try to solve it:
      1. Lack of memory in your phone -I’d suggest to check this.
      2. Some other unknown problem during installation -I would uninstall both the Wheezy rootfs and the GNURoot app, and reinstall it again.
      Then I would repeat the steps 1, 2, 3 and 4
      3. If the same issue happens again, maybe the problem is in the PATH environment. Without a proper PATH, Wheezy cannot locate where are the commands that you want to run.
      I would try to re-set it with the command:
      export PATH=/usr/bin:/bin
      (not sure if this PATH setting is the most suitable).
      Hope this helps!

      • Hi,
        Many thanks for your help!
        The line of code export PATH=/usr/bin:/bin really helped.
        I was able to proceed through steps 4 and 5s (there are two), but when I run apt-get upgrade I receive the following error:
        Can not write log, openpty() failed (/dev/pts not mounted?)
        dpkg: warning: ‘ldconfig’ not found in PATH or not executable
        dpkg: warning: ‘start-stop-daemon’ not found in PATH or not executable
        dpkg: error: 2 expected programs not found in PATH or not executable
        Note: root’s PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
        E: Sub-process /usr/bin/dpkg returned an error code (2)

        I tried to ignore the error and to install R. The instalation seemed to proceed, but at the end I get the same error and R does not run:
        root@localhost:/# R
        bash: R: command not found

        It seems to me there is some problem with the path, but I have no idea how to solve it, I can’t even understand what is bash…
        (my tablet is samsung note 8 with 4 giga of free memory)

        Will be very greatful for any advice!
        Vit

  6. Hi again,
    I forgot to say that my device has Android Lollipop. Just un case anybody asks, GNUroot and GNU Wheezy instalación worked right out of the box. And installing R was pretty easy. Thanks again.
    Gerardo

  7. I have finally managed to install GNURoot! It is wonderful to have Debian programs inside my tablet without having to root it… By the way, is it safe to login as a root user? I mean, even if the tablet is not rooted, could I (accidentally) delete any important file of the Android operating system because of having logged in Debian as root@localhost?

    • It seems like a great question I’d ask to Corbin Champion itself! you could easily delete any file from the Debian distro (hence the extreme caution when logging in as root in GNU/Linux).
      But I don’t really know if you can escalate permissions inside the Android filesystem and delete files. I’d just be careful about this. Good luck!

  8. After opening the terminal and starting up R, I like to call a script form within R that is saved in the working directory. How do I do this? What I find with e.g. Rscript seems to work from e.g. bash command line, but not from within R.

  9. So, GNURoot doesn’t work on Android 5.1+, the only version that works is GNURoot Debian. I’ve been able to get GNURoot Debian up and running however it’s missing nano. I’ve tried (sudo) apt-get update & install nano however it keeps hitting an error. (Here’s the surround lines to)

    Get:1 http://http.debian.net/debian/ jessie/main nano armhf 2.2.6-3 [353 kB]
    Fetched 353 kB in 0s (918 kB/s)
    debconf: delaying package configuration, since apt-utils is not installed
    E: Granting access to slave /dev/pts/1 based on master fd 32 failed! – grantpt (13: Permission denied)
    dpkg: error processing archive /var/cache/apt/archives/nano_2.2.6-3_armhf.deb (–unpack):
    cannot get security labeling handle: No such file or directory
    Errors were encountered while processing:
    /var/cache/apt/archives/nano_2.2.6-3_armhf.deb

    Do you have any ideas or anything?

    • I cannot reproduce your installation process since my newest Android version is 4.4.4. I also found that GNURoot was taking too much sdcard memory so I had to temporarily uninstall it.
      However, i do have a few ideas to try -to at least get nano-:

      Is ‘pico’ installed instead of ‘nano’?

      Do you have enough memory in your device?

      The first error was that apt-utils was not there. Could you install it? then you can type ‘dpkg-reconfigure -au‘ to try to reconfigure nano.

      By the way, I installed R in a Raspberry Pi (ARM processor, with a Debian-like distro), and I found that the latest R versions were still not available for ARM…

  10. Hi,

    I’m facing a small issue here. I’ve completed all the steps but it says me that bash R command not found. I have also tried to export PATH=/usr/bin:/bin as well as export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, but with no luck.
    Any ideas? Thank you

  11. Hi,
    Do you, perchance, know where would I find the root location of R, through Android file system (Solid Explorer with root access)? I accidentally saved a session there, and I can’t find it anywhere.

    • I’d suggest, from the GNURoot terminal, opening R and typing:
      > getwd()
      to see the default path where R will store files and sessions (your session may be there). Then you can navigate to the file using Solid Explorer in Android. HTH

Replica a mareviv Cancelar la respuesta