Differences between revisions 12 and 13
Revision 12 as of 2017-11-09 17:17:59
Size: 6876
Editor: maegger
Comment:
Revision 13 as of 2018-08-28 08:48:29
Size: 6906
Editor: davidsch
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#rev 2018-08-28 davidsch

About R

R is an integrated suite of software facilities for data manipulation, calculation and graphical display.

Using R

In our environment you have the possibility to either directly start R from the command line or by using rstudio

By Command Line

  • Open a Terminal
  • type R to start R

    R version 3.4.2 (2017-09-28) -- "Short Summer"
    Copyright (C) 2017 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
      Natural language support but running in an English locale
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    [Previously saved workspace restored]
    
    >
  • You can now use any command you like. To exit R just type q()

with RStudio

RStudio is an integrated development environment (IDE) for R.

  • Start RStudio by typing rstudio in a shell

RStudio.png

Packages

R can be extended by more than 11'000 Packages. The standard (or base) packages are considered part of the R source code. They contain the basic functions that allow R to work, and the datasets and standard statistical and graphical functions that are described in the R manual.

Using a Base Packages

Let us assume you want to do some simple statistics about the height and weight of a family of four. The familly consists of the parents Jane and Joe and their kids Jim and June.

Member

Height

Weight

Jane

164

69

Joe

188

102

Jim

132

38

June

154

52

Now let us see what R can do with that values.

  • Start R (either by command line or by using RStudio)

  • Now define the height and weight of Jane, Joe, Jim and June.
    height = c(164, 188, 132, 154)
    weight = c(69, 102, 38, 52)
  • Interested in some statistics, e.g. who is the tallest?
    summary(height)
    
       Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      132.0   148.5   159.0   159.5   170.0   188.0 
  • How about a simple plot of height and weight?

    plot(weight, height)

    RStudio2.png

  • Now extend the prevoius plot with some colors and some description.
    plot(weight, height, pch = 16, cex = 1.3, col = "blue", main = "A simple PLOT created with R", xlab = "WEIGHT (kg)", ylab = "HEIGHT (cm)")

    RStudio3.png

Using an additional Package

Since we provide only the basic R package, sooner or later you want to extend it with additional packages. This can be done by creating a local package library and install the additional packages there.

Configure R to use your local package library

Let us assume that you would like to create a local package library in ~/R/libraries. The following commands create that library and configure R to always use it as a resource.

  • Open a Terminal
  • Create the directory
    mkdir -p ~/R/libraries
  • Create a configuration File for R which is loaded every time R starts.
    echo "R_LIBS_USER=\"~/R/libraries\"">.Renviron
  • Start R and check if the path is defined.
    .libPaths()
    
    [1] "/home/USERNAME/R/libraries"                             
    [2] "/usr/pack/r-3.4.2-me/amd64-debian-linux8/lib/R/library"

Installing the rJava package

  • Start R
  • Install the rJava package from the ETH mirror into our local library.

    install.packages("rJava", repos="http://stat.ethz.ch/CRAN", lib="~/R/libraries", dependencies=TRUE)
    
    trying URL 'http://stat.ethz.ch/CRAN/src/contrib/rJava_0.9-9.tar.gz'
    Content type 'application/x-gzip' length 660454 bytes (644 KB)
    ==================================================
    downloaded 644 KB
    
    * installing *source* package ‘rJava’ ...
    ** package ‘rJava’ successfully unpacked and MD5 sums checked
    checking for gcc... gcc -std=gnu99
    checking whether the C compiler works... yes
    checking for C compiler default output file name... a.out
    checking for suffix of executables... 
    checking whether we are cross compiling... no
    checking for suffix of object files... o
    checking whether we are using the GNU C compiler... yes
    [...]
  • Check if the package has been installed in your library
    library()
    Packages in library ‘/home/USERNAME/R/libraries’:
    
    rJava                   Low-Level R to Java Interface
    [...]

An Example Usage with rJava

Let us assume you have created a simple Java Class which creates a number of variables to be used and manipulated by R. Below you find the steps to use that class from within R.

  • Create the folder javatest in your homedirectory and create a simple java file.
    mkdir ~/javatest
    cat<<EOF>~/javatest/myExchange.java
    // This Java program creates a number of variables which will 
    // then be sent to R to be manipulated.
     
    public class myExchange {
     
        public String getString() {
            return "Testing";
        }
     
        public String[] getStringArray() {
            return new String[] {"This is just a test...", "string array."};
        }
        public double[] getdoubleArray() {
            return new double[]{0.001, 20.238};
        }
         
        public double[][] getdoubleMatrix() {
            double[][] doubleMatrix = new double[][]{{1.1,2.2,3.3},{4.4,5.5,6.6}};
            return doubleMatrix;
        }
    }
    EOF
  • Change into that directory and compile the java class.
    cd ~/javatest
    javac myExchange.java
  • Start R and make sure R knows about how to find your generated java class (replace USERNAME with your username).
    R
    
    library(rJava)
    .jinit()
    .jaddClassPath("/home/USERNAME/javatest")
  • Check if R knows about the new Class Path
    .jclassPath()
    
    [1] "/home/USERNAME/R/libraries/rJava/java"
    [2] "/home/USERNAME/javatest" 
  • Now define variables in R and fill them with the values from your java class
    myExchange <- .jnew("myExchange")
    stringTest <- .jcall(myExchange, "S", "getString")
    stringArrayTest <- .jcall(myExchange, "[S", "getStringArray")
    doubleArrayTest <- .jcall(myExchange, "[D", "getdoubleArray")
    doubleMatrixTest <- .jcall(myExchange, "[[D", "getdoubleMatrix", simplify=T)
  • Test what you got
    stringTest
    [1] "Testing"
    
    stringArrayTest
    [1] "This is just a test..." "string array."
    
    doubleArrayTest
    [1]  0.001 20.238
    
    doubleMatrixTest
         [,1] [,2] [,3]
    [1,]  1.1  2.2  3.3
    [2,]  4.4  5.5  6.6

Programming/R (last edited 2020-09-03 13:52:40 by davidsch)