Learn R Programming

r2lh (version 0.7)

r2lu: ~ Function: R to LaTeX, Univariate analysis ~

~ Function: R to HTML, Univariate analysis ~

Description

r2latexUniv (r2lu in short) performs some basic analyses, then generates code to be included in a LaTeX document to print the analyses in a (so nice!) LaTeX way.

Usage

r2lu(x,fileOut="",textBefore="",textAfter="",graphDir="graphUniv",graphName="V",type="png",limDiscrete=10) r2latexUniv(x,fileOut="",textBefore="",textAfter="",graphDir="graphUniv",graphName="V",type="png",limDiscrete=10)

Arguments

x
[data.frame] or [vector] : contains the data to analyse.
fileOut
[character]; name of the output file in which the LaTeX summary will be saved. If empty, the code is printed on screen.
textBefore
[character] or [vector(character)] : before printing a variable analysis, some text can be inserted. If (r2lu is applied on a single variable, textBefore should be of length 1. If the function is applied on a data.frame, textBefore can have same length as the number of columns of the data.frame (this lets the user write a specific introduction for each variable), or can be of length 1. In this case, it is printed before each variable analysis. See examples for details.
textAfter
[character] or [vector(character)] : after printing a variable analysis, some text can be added. See textBefore and examples for details.
graphDir
[character] : directory used to save the graphs generated by the analyses.
graphName
[character] or [vector(character)] : prefix for the graph names. If empty, the graph names are V1 to V length(data.frame)
type
[character] : type of plotting device used to export the graphics. Can be Windows metafile, PNG, JPEG, BMP (Windows bitmap format), TIFF, PostScript or PDF.
limDiscrete
r2lu distinguish two kinds of numeric : discrete designates numeric variables with only a few modalities, continuous designates numeric variables with many modalities. The limit between 'few' and 'many' can be fixed by the user via the limDiscrete argument. By setting limDiscrete to 5, the user will ask r2lu to consider all the numeric variables with 5 modalities or more as continuous and all the variables with less than 5 modalities as discrete.

The default value for limDiscrete is 10.

Value

r2lu generate LaTeX code and either print it on the screen, or save it in a file. It also generate several graphs, optionally in a different directory.

Classical usage

The use of r2lu goes through the following steps:
Step 1.
Load the data (usually, a data.frame).
Step 2.
Optionally, set some variables as ordered.
Step 3.
Run r2lu(dataFrame,"fileOut.tex").
See examples of application.

Author

Christophe Genolini christophe.genolini@free.fr PSIGIAM: Paris Sud Innovation Group in Adolescent Mental Health INSERM U669 / Maison de Solenn / Paris Bernard Desgraupes bernard.desgraupes@u-paris10.fr University of Paris Ouest - Nanterre

Details

r2lu performs some basic analyses, then generates a code to be included in a LaTeX document to print the analyses in a (so nice!) LaTeX way.

r2lu performs the analyses automatically according to the data class. They consider four classes. The analysis of the variable depends on the class:

  1. factor, character and logical: Frequency and barplot
  2. ordered: Frequency, quartile, barplot
  3. numeric discrete: Frequency, mean, variance, quartile, boxplot and barplot
  4. numeric continuous: Mean, variance, quartile, boxplot and histogram.

On a data.frame, r2lu runs the analyses on every column.

See /library/r2lh/doc/r2lhOutput.pdf for display details.

References

LaTeX web site http://www.latex-project.org/ Data are available on line: http://christophe.genolini.free.fr/EPO/EPO2007-Fraude.php

See Also

r2lMainFile, r2latexBiv, r2latexUniv-package, examCheating, Sweave, latex

Examples

Run this code
 # # # # # # # # # # # # # # # # # # #
#   R to LaTeX, Univariate Analyses   #
 #        Artificial examples        #
  #         Single variable         #
   # # # # # # # # # # # # # # # # #

### Create some data
V1 <- factor(LETTERS[floor(runif(50,1,4))])
V2 <- rnorm(50,1,1)<0
V3 <- ordered(LETTERS[floor(runif(50,1,4))])

### Create a directory for the output
if(!file.exists("tmp/r2luExample",recursive=TRUE)){dir.create("tmp/r2luExample",recursive=TRUE)}else{}
setwd("tmp/r2luExample")

### Execute r2lu
r2lu(V1,fileOut="first.tex",textBefore="\\section{Variable 1 to 3}",graphName="V1")
r2lu(V2,fileOut="second.tex",graphName="V2")
r2lu(V3,fileOut="third.tex",textBefore="This is variable 3",graphDir="P")
r2lMainFile(text="\\input{first.tex}\n\\input{second.tex}\n\\input{third.tex}")



 # # # # # # # # # # # # # # # # # # #
#   R to LaTeX, Univariate Analyses   #
 #          Real examples            #
  #        r2lu data.frame          #
   # # # # # # # # # # # # # # # # #

########################
###### Step 1: Create the data

data(examCheating)
str(examCheating)

########################
###### Step 2: ordering variable

examCheating$YearOfStudy <- ordered(examCheating$YearOfStudy,levels=c("L1","L2","L3","M1","M2"))
examCheating$Bac <- ordered(examCheating$Bac,levels=c("Remedial exam","Pass","Fairly good","Good","Very good","Summa cum laude"))
for(iColumn in 8:17){
    examCheating[,iColumn] <- ordered(examCheating[,iColumn],levels=c("Never","Rarely","Sometimes","Often","Always"))
}
str(examCheating)


########################
###### Step 3: running r2lu

### Preparation of textBefore, for transition between variables

textBefore <- paste("\\subsection{",names(examCheating)[c(2:5,18:20)],"}",sep="")

text <- "\\maketitle
\\tableofcontents
\\section{Survey}
  \\begin{enumerate}
    \\item What is your age?
    \\item What is your gender?
    \\item What is your level?
    \\item What is your field?
    \\item Did you cheat at Bac?
    \\item Did you cheat high scool?
    \\item Cheating score
  \\end{enumerate}
\\section{Univariate analysis}
  \\input{ExamCheat-univ.tex}

\\section{More information?}
For a detailled analysis, see
http://christophe.genolini.free.fr/EPO/2007 Fraude/EPO2007-Fraude-Rapport.pdf"

### We can run r2lu
r2lu(examCheating[,c(2:5,18:20)],fileOut="ExamCheat-univ.tex",textBefore=textBefore)
r2lMainFile("ExamCheat-main.tex",text=text)
setwd("../..")

### Then compile ExamCheat-main.tex twice. It is ready !

Run the code above in your browser using DataLab