You are here

Installing R on a headless server

Installing and running R on a Linux server, gives us the power to generate nice charts and documents but it is not the easiest software to install. Especially, when installing on a headless server, there are some requirements to meet and we need to understand how rendering is done.
For this tutorial, I am running a Centos 7 Minimal.

  1. Anti-Aliasing
    Whatever the data you are going to render, you surely want a beautiful result for a presentable report. This is what your customer or your manager expects from you, so we need to know a little about rendering.
    At the very beginning, we must ensure that we can obtain anti-aliasing. This is the critical point, or any pie chart will have many squares in its borders instead of looking like a nice, perfect circle. For avoiding the ugly aliasing like here, we will rely on Cairo library instead of using the good old X11 (some still suggest this solution).
  2. Understanding font handling
    As important as anti-aliasing, is the use of good-looking fonts. By good-looking, I mean something that is clean and looks like what you expected and not necessarily a style of glyph that you particularly like. For this, we will meet the standards by using the Core fonts for the Web, providing good readability and internationalization.
    One technical point to know, is that font rendering is available on Linux through the FreeType2 library as explained in this Igor's Blog article. Upgrading this library might be one day necessary.
    For the end-user, maybe should you familiarize with the Fontconfig commands to see what fonts are installed on your server, and how to request them. Particularly, as we are talking about font rendering, we must know the meaning of kerning and hinting, since they both have a high impact on results. Fontconfig was introduced in RedHat 5 with the goal of replacing X Font Server (xfs), and it did successfully.
    We consider the Adobe fonts for PostScript support as obsolete, that we still can download from the XFree86 dead project. I have played around some time with this before writing this article, and we can forget about this unless we want a deeper knowledge of font formats. However it's an opportunity here to recall the prolific developer Keith Packard for his work on FreeType2 as well as xfs, Cairo...
    Again, we consider the URW fonts as obsolete, although they were very successful some decades ago.
    In conclusion, fonts and font formats are a whole subject, and detailed information can bring this subject to the nightmare level. For keeping it simple, an overview of the different formats is given by Microsoft : What's the difference between TrueType, PostScript, and OpenType fonts?, while an in-depth investigation of font rendering unsolved problems is given there : Texts Rasterization Exposures.
  3. System prerequisites (yum)
    First we need to install some system packages :
    yum update -y
    yum install -y gcc-c++ gcc-gfortran java-1.7.0-openjdk perl cairo-devel libXt-devel libjpeg-turbo-devel readline-devel glibc-devel rpm-build net-tools rpm-build wget
  4. Installing cabextract
    cabextract will later be used for installing the Web fonts published by Microsoft, so we need this

    # wget http://www.cabextract.org.uk/cabextract-1.6.tar.gz
    # tar zxf cabextract-1.6.tar.gz
    # cd cabextract-1.6.
    # ./configure
    # make
    # make install
  5. Installing Web fonts
    We take some help from the corefonts project rather than doing all the job by ourselves.
    # wget http://corefonts.sourceforge.net/msttcorefonts-2.5-1.spec
    As this project is not often maintained, the list of mirrors can be out of date and we edit the spec file to correct this. Line 37, I start the list of mirrors like this :
    mirrors="freefr+dl-aarnet+cdnetworks-kr-1+... ..
    Line 21, I remove the cabextract requirement
    BuildRequires: cabextract
    We are ready for the install
    # rpmbuild -bb msttcorefonts-2.5-1.spec
    # rpm -ivh $HOME/rpmbuild/RPMS/noarch/msttcorefonts-2.5-1.noarch.rpm

    We check that the fonts have been installed in FontConfig :
    # fc-list
  6. Installing R

    # wget http://cran.univ-paris1.fr/src/base/R-3/R-3.2.2.tar.gz
    # tar zxf R-3.2.2.tar.gz
    # cd R-3.2.2
    # ./configure --with-x=no --enable-R-shlib
    # make
    # make check
    # make install

    Check that R can be started from the command line with 'R' command
    # R
    >
  7. Installing Cairo package
    It is possible to install this from the R shell, or by downloading the source and install it offline. Both give the same result but I like the second method better because it makes scripting easier, thus ensuring installation predictability.

    # wget https://cran.r-project.org/src/contrib/Cairo_1.5-8.tar.gz
    # bin/R CMD INSTALL Cairo_1.5-8.tar.gz
  8. Testing
    Here, we will launch the R program and make a simple plot with our fonts.

    # R
    > # load the library
    > library(Cairo)
    >
    > # check
    > sessionInfo()
    other attached packages:
    [1] Cairo_1.5-8
    >
    > # check Web fonts are installed
    > CairoFontMatch(fontpattern=":", sort=TRUE)
    ..
    3. family: "Verdana", style: "Regular", file: "/usr/share/fonts/msttcorefonts/verdana.ttf"
    4. family: "Arial", style: "Regular", file: "/usr/share/fonts/msttcorefonts/arial.ttf"
    ..
    >
    > # select fonts for our graph
    > CairoFonts(regular="Verdana:style=regular", bold="Verdana:style=bold")
    >
    > # build a pie chart
    > Cairo("example.jpg", type="jpeg", quality=100, width=500, height=500)
    > slices <- c(10, 7, 5, 5, 5)
    > lbls <- c("Real Madrid 10", "Milan 7", "Bayern Munich 5", "Barcelona 5", "Liverpool 5")
    > pie(slices, labels = lbls, main="Champions League top 5", clockwise=TRUE)
    > dev.off()
    null device
    1
    > q()
    Save workspace image? [y/n/c]: n

    Now that you have example.jpg file, you can upload it and check that you have the expected result.
Attachment: 

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer