Différences entre versions de « R »

Ligne 14 : Ligne 14 :
 
=== Démarrage de R ===
 
=== Démarrage de R ===
  
* Pour lancer R en ligne de commande, entrez
+
* Pour lancer R en ligne de commandes, entrez
 
<pre>
 
<pre>
 
R
 
R

Version du 5 juillet 2011 à 12:11


Le logo de R.

R est un langage de programmation et un environnement mathématique issu d'un projet GNU (similaire à S). R est employé pour le traitement de données et l'analyse statistique et dispose également de nombreuses fonctions graphiques.

Guide

Guide de R

Writing R Extensions

Démarrage de R

  • Pour lancer R en ligne de commandes, entrez
R

depuis une fenêtre de terminal.

  • Pour lancer R avec l'interface graphique, entrez plutôt ce qui suit dans un terminal de commandes:
xR

How can add-on packages be installed?

The add-on packages on CRAN come as gzipped tar files named pkg_version.tar.gz, which may in fact be “bundles” containing more than one package. Provided that tar and gzip are available on your system, type

 $ R CMD INSTALL /path/to/pkg_version.tar.gz

To install to another tree (e.g., your private one), use

    $ R CMD INSTALL -l lib /path/to/pkg_version.tar.gz

where lib gives the path to the library tree to install to.

Even more conveniently, you can install and automatically update packages from within R if you have access to repositories such as CRAN. See the help page for available.packages() for more information.

You can use several library trees of add-on packages. The easiest way to tell R to use these is via the environment variable R_LIBS which should be a colon-separated list of directories at which R library trees are rooted. You do not have to specify the default tree in R_LIBS. E.g., to use a private tree in $HOME/lib/R and a public site-wide tree in /usr/local/lib/R-contrib, put

    R_LIBS="$HOME/lib/R:/usr/local/lib/R-contrib"; export R_LIBS

into your (Bourne) shell profile or even preferably, add the line

    R_LIBS="~/lib/R:/usr/local/lib/R-contrib"

your ~/.Renviron file. (Note that no export statement is needed or allowed in this file; see the on-line help for Startup for more information.)

How can add-on packages be used?

To find out which additional packages are available on your system, type

    library()

at the R prompt.

This produces something like

             Packages in `/home/me/lib/R':              
             mystuff       My own R functions, nicely packaged but not documented
             Packages in `/usr/local/lib/R/library':              
             KernSmooth    Functions for kernel smoothing for Wand & Jones (1995)
             MASS          Main Package of Venables and Ripley's MASS
             base          The R Base package
             boot          Bootstrap R (S-Plus) Functions (Canty)
             class         Functions for Classification
             cluster       Functions for clustering (by Rousseeuw et al.)
             datasets      The R datasets Package
             foreign       Read data stored by Minitab, S, SAS, SPSS, Stata, ...
             grDevices     The R Graphics Devices and Support for Colours and Fonts
             graphics      The R Graphics Package
             grid          The Grid Graphics Package
             lattice       Lattice Graphics
             methods       Formal Methods and Classes
             mgcv          GAMs with GCV smoothness estimation and GAMMs by REML/PQ
             nlme          Linear and nonlinear mixed effects models
             nnet          Feed-forward Neural Networks and Multinomial Log-Linear
                           Models
             rpart         Recursive partitioning
             spatial       Functions for Kriging and Point Pattern Analysis
             splines       Regression Spline Functions and Classes
             stats         The R Stats Package
             stats4        Statistical functions using S4 classes
             survival      Survival analysis, including penalised likelihood
             tcltk         Tcl/Tk Interface
             tools         Tools for Package Development
             utils         The R Utils Package      

You can “load” the installed package pkg by

    library(pkg)

You can then find out which functions it provides by typing one of

    library(help = pkg)
    help(package = pkg)

You can unload the loaded package pkg by

    detach("package:pkg")


How can add-on packages be removed?

Use

    $ R CMD REMOVE pkg_1 ... pkg_n

to remove the packages pkg_1, ..., pkg_n from the library tree rooted at the first directory given in R_LIBS if this is set and non-null, and from the default library otherwise. (Versions of R prior to 1.3.0 removed from the default library by default.)

To remove from library lib, do

    $ R CMD REMOVE -l lib pkg_1 ... pkg_n

How can I clean up my workspace?

To remove all objects in the currently active environment (typically .GlobalEnv), you can do

    rm(list = ls(all = TRUE))

(Without all = TRUE, only the objects with names not starting with a `.' are removed.)

How do I produce PNG graphics in batch mode?

Under Unix, the png() device uses the X11 driver, which is a problem in batch mode or for remote operation. If you have Ghostscript you can use bitmap(), which produces a PostScript file then converts it to any bitmap format supported by Ghostscript. On some installations this produces ugly output, on others it is perfectly satisfactory. In theory one could also use Xvfb from X.Org, which is an X11 server that does not require a screen; and the GDD package from CRAN, which produces PNG, JPEG and GIF bitmaps without X11.

Batch Execution of R

Run R non-interactively with input from infile and send output (stdout/stderr) to another file. Usage

   R CMD BATCH [options] infile [outfile]

Arguments

  • infile the name of a file with R code to be executed.
  • options a list of R command line options, e.g., for setting the amount of memory available and controlling the load/save process. If infile starts with a -, use -- as the final option. The default options are --restore --save --no-readline.
  • outfile the name of a file to which to write output. If not given, the name used is that of infile, with a possible ‘.R’ extension stripped, and ‘.Rout’ appended.

Fortran

Le logiciel R intègre l'utilisation de fichiers sources Fortran ( .f ). Ainsi, avec le fichier source Fortran "fichier.f" :

  • Compiler le fichier : dans un terminal tapez : " R CMD SHLIB fichier.f " (sans les guillemets).
  • Ceci devrait créer dans votre répertoire deux fichiers : "fichier.so " et "fichier.o"
  • Pour charger le fichier sous R, tapez sous R : dyn.load("fichier.so")
  • Pour les débutants, un exemple fait par Jennifer Poulin : le fichier source transpose.f et le fichier de commandes sous R transpose.txt .

transpose.f

******7**10********20********30********40********50********60********7072
** Sous-routine calculant la transposée de A (par Jennifer Poulin):
**
** A: MxN
** B = t(A): N:M
     SUBROUTINE TRANSPOSE(A,B,m,n)
     INTEGER i,j,m,n
     DOUBLE PRECISION A(m,n), B(n,m)
     DO i=1, n
       DO j=1, m
         B(i,j) = A(j,i)
       ENDDO
     ENDDO
     RETURN 
     END


Exemple simple pour calculer la transposée d'une matrice sous R en utilisant le fichier source Fortran transpose.f (par Jennifer Poulin):
y <- matrix(1:30, nrow=3)
dyn.load("transpose.so")
transpo2 <- function(A){
  n <- nrow(A)
  m <- ncol(A)
  B <- matrix(0,nrow=m,ncol=n)
  storage.mode(B) <- "double"
  resultat <- .Fortran("transpose",
    as.double(A),
    B=B,
    as.integer(n),
    as.integer(m))
  B <- resultat"B"
  B
} 
> y
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    4    7   10   13   16   19   22   25    28
[2,]    2    5    8   11   14   17   20   23   26    29
[3,]    3    6    9   12   15   18   21   24   27    30 
> transpo2(y)
      [,1] [,2] [,3]
 [1,]    1    2    3
 [2,]    4    5    6
 [3,]    7    8    9
 [4,]   10   11   12
 [5,]   13   14   15
 [6,]   16   17   18
 [7,]   19   20   21
 [8,]   22   23   24
 [9,]   25   26   27
[10,]   28   29   30

Voir aussi

Bibliographie

  • J. Adler, R in a Nutshell: A Desktop Quick Reference, O'Reilly, Sebastopol, CA 95472, 2010.

Articles connexes

Références externes


La dernière modification de cette page a été faite le 5 juillet 2011 à 12:11.