Upgrade to Pro — share decks privately, control downloads, hide ads and more …

R programming language and Ruby/Rails integration

R programming language and Ruby/Rails integration

R programming language and Ruby/Rails integration
DRUG #38 meeting
21.01.2013

Rafał Łasocha

January 21, 2013
Tweet

More Decks by Rafał Łasocha

Other Decks in Programming

Transcript

  1. R programming language
    Rafał Łasocha
    @swistak35
    1 year student on II@UWr
    ...and a little bit about Ruby/Rails integration with R

    View full-size slide

  2. R

    GNU GPL

    Functions for statistics

    Easy graphics methods (for charts)

    REPL

    Object oriented

    View full-size slide

  3. Use-cases

    Recommendation engines

    Speech recognition

    Spam detection

    Data mining

    Analyse everything – ecology, human
    genome, trading, economy, diseases,
    cyclons...

    View full-size slide

  4. Objects

    Many types of collections: vectors, factors,
    matrices, arrays, lists, dataframes, tables...

    „All R objects have a class, a type and a
    dimensions.”

    Typical langs use 'for' loops, in ruby we use
    enumerators, in R the most popular are
    „apply” functions

    Dataframes behave like tables in databases,
    they even can be queried

    View full-size slide

  5. Code (in REPL)
    > 2 + 1
    [1] 3
    > library(MASS)
    > data(cats)
    > class(cats)
    [1] "data.frame"
    > names(cats)
    [1] "Sex" "Bwt" "Hwt"
    > cats$Bwt
    [1] 2.0 2.0 2.0 2.1 2.1 …
    > cats$Bwt + 1
    [1] 3.0 3.0 3.0 3.1 3.1 ...
    > mean(cats$Bwt)
    [1] 2.723611
    > rest = cats$Bwt - (cats$Hwt/1000)
    > rest
    [1] 1.9930 1.9926 1.9905 2.0928
    2.0927 ...

    View full-size slide

  6. Packages
    Installing packages directly using REPL:
    > install.packages("DBI") # database communication interface

    > install.packages("RPostgreSQL") # postgresql

    > install.packages("ggplot2") # another charts library
    ...
    Polish mirror R is only in Wroclaw, on UWr : )

    View full-size slide

  7. R + postgresql

    View full-size slide

  8. Exporting charts
    > png("example_name.png", width = 1000, height = 600)
    > ggplot(...)
    > dev.off()
    R + Rails = ?

    View full-size slide

  9. RinRuby

    100% Ruby

    Send and receive data through TCP/IP
    sockets

    Pros: it works.

    Cons: pretty slow, not updated since 2011,
    poor documentation, only 2 datatypes
    available: vectors and matrix

    View full-size slide

  10. RSRuby

    Pros: fast! (100-1000x RinRuby), integration
    with Ruby (methods and objects are objects
    from R)

    Cons: dependent on Ruby/R version,
    operating system; poor documentation, not
    updated since 2011

    View full-size slide

  11. RServe

    100% Ruby (client), connection through TCP/IP

    Require Rserve on server, ruby connects to it with
    Ruby-RServe-Client (RServe came to us from Java)

    Pros: Fast, updated (rarely, but it's better than nothing)

    Cons: Require Rserve on server, poor documentation,
    limited functions on Windows (who cares)

    View full-size slide

  12. Rapache

    Web SERVICE (SOA SOA SOA SOA SOA...)

    Pros: Modular, up-to-date

    Cons: Requires Rapache, specific to Apache
    server only

    View full-size slide

  13. JSON API
    There are also libraries to export/import to CSV, Google
    spreadsheets, XLS (Microsoft Excel), ODS (LibreOffice Calc), and
    more

    View full-size slide

  14. Rkward

    Nice, written using Qt, GUI for R

    Allows to write scripts, big database of
    commonly-used functions, it allows to
    customize chart with ease

    View full-size slide

  15. Sources & useful links:
    http://www.r-project.org/ (site of the GNU R project)
    http://www.slideshare.net/sausheong/rubyand-r (London RUG)
    http://www.mayin.org/ajayshah/KB/R/index.html (R by example, great source of
    knowledge!)
    English & Polish Wikibooks - „Programming R”
    http://rseek.org/ (search for blog posts, tutorials, etc. - great way to find
    something abou rare topics)
    http://www.statystycy.pl/ (polish forum with section about R programming)
    http://docs.ggplot2.org (docs for ggplot2 – most popular R graphics library)
    http://cran.r-project.org (CRAN with R packages)
    http://stat.bell-labs.com/RS-DBI/doc/html/index.html (nice guide for DBI –
    connecting with databases)
    http://cran.r-project.org/manuals.html (official R tutorials & manuals, with great
    introduction guide – simple, but quite comprehensive, about 100 pages)
    http://tryr.codeschool.com (Codeschool course to „Try R”)
    https://github.com/virtualstaticvoid/heroku-buildpack-r (Running R on heroku)
    Thanks!
    Notice: During this presentation, I didn't used any loops in code.

    View full-size slide