Mediante el paquete "xtable" podemos convertir algunas salidas de R a formato Latex (aplicable a Lyx), y así facilitarnos la redacción de nuestros trabajos.
install.packages("xtable") #instalamos el paquete library("stable") #lo activates o cargamos
[5] xtable.data.frame* xtable.glm* xtable.lm* xtable.matrix*
[9] xtable.prcomp* xtable.summary.aov* xtable.summary.aovlist* xtable.summary.glm*
[13] xtable.summary.lm* xtable.summary.prcomp* xtable.table* xtable.ts*
[17] xtable.zoo*
Non-visible functions are asterisked
Si queremos utilizar el paquete con objetos de nls, basta con aplicar el siguiente script.
## Script para crear tablas a partir de objetos nls
xtable.nls= function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.summary.nls(summary(x),caption=caption,label=label, align=align, digits=digits,display=display)) } xtable.summary.nls= function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { x = data.frame(x$coef,check.names=F) class(x) = c("xtable","data.frame") caption(x) = caption label(x) = label align(x) = switch(1+is.null(align),align,c("r","r","r","r","r")) digits(x) = switch(1+is.null(digits),digits,c(0,4,4,2,4)) display(x) = switch(1+is.null(display),display,c("s","f","f","f","f")) return(x) }
# ejemplo de aplicación
Formula: density ~ Asym/(1 + exp((xmid - log(conc))/scal)) Parameters: Estimate Std. Error t value Pr(>|t|) Asym 2.34518 0.07815 30.01 2.17e-13 *** xmid 1.48309 0.08135 18.23 1.22e-10 *** scal 1.04145 0.03227 32.27 8.51e-14 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.01919 on 13 degrees of freedom Number of iterations to convergence: 6 Achieved convergence tolerance: 2.026e-06
xtable(fm3DNase1)
% latex table generated in R 2.15.0 by xtable 1.7-0 package % Mon May 14 11:21:04 2012 \begin{table}[ht] \begin{center} \begin{tabular}{rrrrr} \hline & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ \hline Asym & 2.3452 & 0.0782 & 30.01 & 0.0000 \\ xmid & 1.4831 & 0.0814 & 18.23 & 0.0000 \\ scal & 1.0415 & 0.0323 & 32.27 & 0.0000 \\ \hline \end{tabular} \end{center}
\end{table}Yo uso Lyx. En este programa basta con abrir un archivo nuevo, seleccionar "Insertar" en la barra de herramientas, luego "Código TEX" y pegamos la salida de R.
El resultado es el siguiente:
También existen otros paquetes similares que te pueden interesar:
- hwriter (Gregoire Pau, 2010)
- R2HTML (Eric Lecoutre, 2010)
- memisc : tool for management of survey data, contains some tools for latex tables of (basic) regression model estimates.
- Hmisc contains a function
latex()
that creates a tex file containing the object of choice. It is pretty flexible, and can also outputlongtable
latex tables. There's a lot of info in the help file?latex
- miscFuncs has a neat function 'latextable' that converts matrix data with mixed alphabetic and numeric entries into a LaTeX table and prints them to the console, so they can be copied and pasted into a LaTeX document.
- apsrtable which formats latex tables from one or more model objects
- p2lh which exports R to LaTeX and HTML
- RcmdrPlugin.Export which graphically exports output to LaTeX or HTML
- reporttools which generates LaTeX tables of descriptive statistics
- xtable : for standard tables of most simple objects. A nice gallery with examples can be found here.
Para ver más de generación de informes con R y Latex, puedes mirarte estos archivos:
- How to use xtable() with a data.frame which contains special LATEX characters J.R. Lobry. 2007. http://pbil.univ-lyon1.fr/R_old/querep/qrn.pdf
- Generación automática de informes con Sweave y LATEX. Francesc Carmona Departamento de Estadística Universidad de Barcelona. 2012. http://www.ub.edu/stat/docencia/EADB/Manual_Sweave.pdf
- An Sweave Demo Charles J. Geyer. 2010. http://users.stat.umn.edu/~geyer/Sweave/foo.pdf
Para los curiosos. Para todo aquel que quiera manipular las funciones del paquete xtable. Aquí van los scripts (link).
### xtable package ### ### Produce LaTeX and HTML tables from R objects. ### ### Copyright 2000-2007 David B. Dahl <dahl@stat.tamu.edu> ### ### This file is part of the `xtable' library for R and related languages. ### It is made available under the terms of the GNU General Public ### License, version 2, or at your option, any later version, ### incorporated herein by reference. ### ### This program is distributed in the hope that it will be ### useful, but WITHOUT ANY WARRANTY; without even the implied ### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ### PURPOSE. See the GNU General Public License for more ### details. ### ### You should have received a copy of the GNU General Public ### License along with this program; if not, write to the Free ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ### MA 02111-1307, USA xtable <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { UseMethod("xtable") } ## data.frame and matrix objects xtable.data.frame <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { logicals <- unlist(lapply(x,is.logical)) x[,logicals] <- lapply(x[,logicals], as.character) characters <- unlist(lapply(x,is.character)) factors <- unlist(lapply(x,is.factor)) ints <- sapply(x, is.integer) class(x) <- c("xtable","data.frame") caption(x) <- caption label(x) <- label align(x) <- switch(1+is.null(align), align, c("r",c("r","l")[(characters|factors)+1])) digits(x) <- switch(1+is.null(digits),digits,c(0,rep(2,ncol(x)))) # Patch from Seth Falcon <sfalcon@fhcrc.org>, 18-May-2007 if (is.null(display)) { display <- rep("f", ncol(x)) display[ints] <- "d" display[characters | factors] <- "s" display <- c("s", display) } display(x) <- display return(x) } xtable.matrix <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.data.frame(data.frame(x,check.names=FALSE), caption=caption,label=label,align=align, digits=digits,display=display)) } ## table objects (of 1 or 2 dimensions) by Guido Gay, 9 Feb 2007 ## Fixed to pass R checks by DBD, 9 May 2007 xtable.table<-function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { if (length(dim(x))==1) { return(xtable.matrix(matrix(x,dimnames=list(rownames(x),names(dimnames(x)))),caption=caption,label=label,align=align,digits=digits,display=display)) } else if (length(dim(x))==2) { return(xtable.matrix(matrix(x,ncol=dim(x)[2],nrow=dim(x)[1],dimnames=list(rownames(x),colnames(x))),caption=caption,label=label,align=align,digits=digits,display=display)) } else { stop("xtable.table is not implemented for tables of > 2 dimensions") } } ## anova objects xtable.anova <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { suggested.digits <- c(0,rep(2,ncol(x))) suggested.digits[grep("Pr\\(>",names(x))+1] <- 4 suggested.digits[grep("P\\(>",names(x))+1] <- 4 suggested.digits[grep("Df",names(x))+1] <- 0 class(x) <- c("xtable","data.frame") caption(x) <- caption label(x) <- label align(x) <- switch(1+is.null(align),align,c("l",rep("r",ncol(x)))) digits(x) <- switch(1+is.null(digits),digits,suggested.digits) display(x) <- switch(1+is.null(display),display,c("s",rep("f",ncol(x)))) return(x) } ## aov objects xtable.aov <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.anova(anova(x,...),caption=caption,label=label, align=align, digits=digits,display=display)) } xtable.summary.aov <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.anova(x[[1]],caption=caption,label=label, align=align, digits=digits,display=display)) } xtable.summary.aovlist <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { for(i in 1:length(x)) { if (i==1) result <- xtable.summary.aov(x[[i]],caption=caption,label=label, align=align, digits=digits,display=display) else result <- rbind(result,xtable.anova(x[[i]][[1]],caption=caption, label=label, align=align, digits=digits,display=display)) } return(result) } xtable.aovlist <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.summary.aovlist(summary(x),caption=caption,label=label, align=align, digits=digits,display=display)) } ## lm objects xtable.lm <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.summary.lm(summary(x),caption=caption,label=label, align=align, digits=digits,display=display)) } xtable.summary.lm <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { x <- data.frame(x$coef,check.names=FALSE) class(x) <- c("xtable","data.frame") caption(x) <- caption label(x) <- label align(x) <- switch(1+is.null(align),align,c("r","r","r","r","r")) digits(x) <- switch(1+is.null(digits),digits,c(0,4,4,2,4)) display(x) <- switch(1+is.null(display),display,c("s","f","f","f","f")) return(x) } ## glm objects xtable.glm <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.summary.glm(summary(x),caption=caption,label=label,align=align, digits=digits,display=display)) } xtable.summary.glm <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { return(xtable.summary.lm(x,caption=caption,label=label, align=align, digits=digits,display=display)) } ## prcomp objects xtable.prcomp <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { x <- data.frame(x$rotation,check.names=FALSE) class(x) <- c("xtable","data.frame") caption(x) <- caption label(x) <- label align(x) <- switch(1+is.null(align),align,c("r",rep("r",ncol(x)))) digits(x) <- switch(1+is.null(digits),digits,c(0,rep(4,ncol(x)))) display(x) <- switch(1+is.null(display),display,c("s",rep("f",ncol(x)))) return(x) } xtable.summary.prcomp <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { x <- data.frame(x$importance,check.names=FALSE) class(x) <- c("xtable","data.frame") caption(x) <- caption label(x) <- label align(x) <- switch(1+is.null(align),align,c("r",rep("r",ncol(x)))) digits(x) <- switch(1+is.null(digits),digits,c(0,rep(4,ncol(x)))) display(x) <- switch(1+is.null(display),display,c("s",rep("f",ncol(x)))) return(x) } # Slightly modified version of xtable.coxph contributed on r-help by # Date: Wed, 2 Oct 2002 17:47:56 -0500 (CDT) # From: Jun Yan <jyan@stat.wisc.edu> # Subject: Re: [R] xtable for Cox model output xtable.coxph <- function (x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { cox <- x beta <- cox$coef se <- sqrt(diag(cox$var)) if (is.null(cox$naive.var)) { tmp <- cbind(beta, exp(beta), se, beta/se, 1 - pchisq((beta/se)^2, 1)) dimnames(tmp) <- list(names(beta), c("coef", "exp(coef)", "se(coef)", "z", "p")) } else { tmp <- cbind( beta, exp(beta), se, beta/se, signif(1 - pchisq((beta/se)^2, 1), digits - 1)) dimnames(tmp) <- list(names(beta), c("coef", "exp(coef)", "robust se", "z", "p")) } return(xtable(tmp, caption = caption, label = label, align = align, digits = digits, display = display)) } # Additional method: xtable.ts # Contributed by David Mitchell (davidm@netspeed.com.au) # Date: July 2003 xtable.ts <- function(x,caption=NULL,label=NULL,align=NULL, digits=NULL,display=NULL,...) { if (inherits(x, "ts") && !is.null(ncol(x))) { # COLNAMES <- paste(colnames(x)); tp.1 <- trunc(time(x)) tp.2 <- trunc(cycle(x)) day.abb <- c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat") ROWNAMES <- switch(frequency(x), tp.1, "Arg2", "Arg3", ## Dummy arguments paste(tp.1, c("Q1", "Q2", "Q3", "Q4")[tp.2], sep=" "), "Arg5", "Arg6", paste("Wk.", tp.1, " ", day.abb[tp.2], sep=""), "Arg8", "Arg9", "Arg10", "Arg11", paste(tp.1, month.abb[tp.2], sep=" ")) tmp <- data.frame(x, row.names=ROWNAMES); } else if (inherits(x, "ts") && is.null(ncol(x))) { COLNAMES <- switch(frequency(x), "Value", "Arg2", "Arg3", ## Dummy arguments c("Q1", "Q2", "Q3", "Q4"), "Arg5", "Arg6", day.abb, "Arg8", "Arg9", "Arg10", "Arg11", month.abb) ROWNAMES <- seq(from=start(x)[1], to=end(x)[1]) tmp <- data.frame(matrix(c(rep(NA, start(x)[2] - 1), x, rep(NA, frequency(x) - end(x)[2])), ncol=frequency(x), byrow=TRUE), row.names=ROWNAMES) names(tmp) <- COLNAMES } return(xtable(tmp, caption = caption, label = label, align = align, digits = digits, display = display)) } # Suggested by Ajay Narottam Shah <ajayshah@mayin.org> in e-mail 2006/07/22 xtable.zoo <- function(x,...) { return(xtable(as.ts(x),...)) }
Comentarios
Publicar un comentario