Differences between revisions 6 and 7
Revision 6 as of 2017-11-09 16:42:51
Size: 2878
Editor: maegger
Comment:
Revision 7 as of 2017-11-09 16:59:13
Size: 4881
Editor: maegger
Comment:
Deletions are marked like this. Additions are marked like this.
Line 77: Line 77:
= Using an additional Package (rJava) =
= Using an additional Package =
Since in our environemnt we do not add additional packages to the R Package, you have to install needed packages by yourself. This can be done by creating a local package library and install the additional packages in 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/maegger/R/libraries’:

rJava Low-Level R to Java Interface
[...]
}}}

== An Example Usage with rJava ==

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 in our environemnt we do not add additional packages to the R Package, you have to install needed packages by yourself. This can be done by creating a local package library and install the additional packages in 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/maegger/R/libraries’:
    
    rJava                   Low-Level R to Java Interface
    [...]

An Example Usage with rJava

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