Hola Tengo un problema con Sweave y xtable, concretamente quiero cambiar el color según un criterio, creo que es más fácil explicar con los siguientes ejemplos, uno puede correr directamente porque es R, al siguiente código hay que guardarlo como Rnw, y luego puede ser corrido en R, donde seguramente al correr este se darán cuenta de mis dos problemas porque saltan al abrir el pdf que se obtiene como resultado. #################################3 código R #########################3 library(xtable) da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) ######################################################################### En este Rnw tengo una t más en \textcolor{blue}{", porque quedaba escrito extcolor ... ##################### Código Rnw ##### \documentclass[a4paper]{article} \begin{document} <<echo=false,results=hide>>library(lattice) library(xtable) data(cats, package="MASS") @ \section*{The Cats Data ---- Ejemolo que funciana con xtable} Consider the \texttt{cats} regression example from Venables \& Ripley (1997). The data frame contains measurements of heart and body weight of \Sexpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female, \Sexpr{sum(cats$Sex=="M")} male). A linear regression model of heart weight by sex and gender can be fitted in R using the command <<>>lm1 = lm(Hwt~Bwt*Sex, data=cats) lm1 @ Tests for significance of the coefficients are shown in Table~\ref{tab:coef}, a scatter plot including the regression lines is shown in Figure~\ref{fig:cats}. \SweaveOpts{echo=false} <<results=tex>>xtable(lm1, caption="Linear regression model for cats data.", label="tab:coef") @ \begin{figure} \centering <<fig=TRUE,width=12,height=6>>trellis.par.set(col.whitebg()) print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r"))) @ \caption{The cats data from package MASS.} \label{fig:cats} \end{figure} \begin{center} \end{center} \section{Intento 1} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) require(xtable) xtable(da[,c("id","score.string")]) @ \section{Intento 2} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) @ \section{Intento - latex} <<>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) @ \end{document} ############################################################################## [[alternative HTML version deleted]]
Hola Pude resolver mi problema, el código Rnw es el siguiente \documentclass[a4paper]{article} \usepackage{color} \begin{document} <<echo=false,results=hide>>library(lattice) library(xtable) @ \section{xtable con colores} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\\color{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\\color{green}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) @ \end{document} From: Marcuzzi, Javier Rubén Sent: Tuesday, April 03, 2012 5:05 PM To: r-help-es@r-project.org Subject: Sweave xtable Hola Tengo un problema con Sweave y xtable, concretamente quiero cambiar el color según un criterio, creo que es más fácil explicar con los siguientes ejemplos, uno puede correr directamente porque es R, al siguiente código hay que guardarlo como Rnw, y luego puede ser corrido en R, donde seguramente al correr este se darán cuenta de mis dos problemas porque saltan al abrir el pdf que se obtiene como resultado. #################################3 código R #########################3 library(xtable) da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) ######################################################################### En este Rnw tengo una t más en \textcolor{blue}{", porque quedaba escrito extcolor ... ##################### Código Rnw ##### \documentclass[a4paper]{article} \begin{document} <<echo=false,results=hide>>library(lattice) library(xtable) data(cats, package="MASS") @ \section*{The Cats Data ---- Ejemolo que funciana con xtable} Consider the \texttt{cats} regression example from Venables \& Ripley (1997). The data frame contains measurements of heart and body weight of \Sexpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female, \Sexpr{sum(cats$Sex=="M")} male). A linear regression model of heart weight by sex and gender can be fitted in R using the command <<>>lm1 = lm(Hwt~Bwt*Sex, data=cats) lm1 @ Tests for significance of the coefficients are shown in Table~\ref{tab:coef}, a scatter plot including the regression lines is shown in Figure~\ref{fig:cats}. \SweaveOpts{echo=false} <<results=tex>>xtable(lm1, caption="Linear regression model for cats data.", label="tab:coef") @ \begin{figure} \centering <<fig=TRUE,width=12,height=6>>trellis.par.set(col.whitebg()) print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r"))) @ \caption{The cats data from package MASS.} \label{fig:cats} \end{figure} \begin{center} \end{center} \section{Intento 1} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) require(xtable) xtable(da[,c("id","score.string")]) @ \section{Intento 2} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) @ \section{Intento - latex} <<>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) @ \end{document} ############################################################################## [[alternative HTML version deleted]]
... Sólo por apuntarlo, no sé si conoces el paquete "knitr", que es como Sweave pero más flexible y elegante. Un saludo, Javier On 04/04/12 18:24, Marcuzzi, Javier Rubén wrote:> Hola > > Pude resolver mi problema, el código Rnw es el siguiente > > \documentclass[a4paper]{article} > \usepackage{color} > \begin{document} > <<echo=false,results=hide>>> library(lattice) > library(xtable) > @ > > > > \section{xtable con colores} > <<results=tex>>> da<- data.frame(id=letters[1:5], score=1:5*2) > col<- function(x){ > ifelse(x>7, > paste("\\color{blue}{", formatC(x, dig=2, format="f"), "}"), > paste("\\color{green}{", formatC(x, dig=2, format="f"), "}")) > } > da$score.string<- col(da$score) > > print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) > @ > > > \end{document} > > From: Marcuzzi, Javier Rubén > Sent: Tuesday, April 03, 2012 5:05 PM > To: r-help-es@r-project.org > Subject: Sweave xtable > > Hola > > Tengo un problema con Sweave y xtable, concretamente quiero cambiar el color según un criterio, creo que es más fácil explicar con los siguientes ejemplos, uno puede correr directamente porque es R, al siguiente código hay que guardarlo como Rnw, y luego puede ser corrido en R, donde seguramente al correr este se darán cuenta de mis dos problemas porque saltan al abrir el pdf que se obtiene como resultado. > > #################################3 código R #########################3 > library(xtable) > da<- data.frame(id=letters[1:5], score=1:5*2) > col<- function(x){ > ifelse(x>7, > paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), > paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) > } > da$score.string<- col(da$score) > > xtable(da[,c("id","score.string")]) > > ######################################################################### > > En este Rnw tengo una t más en \textcolor{blue}{", porque quedaba escrito extcolor ... > > ##################### Código Rnw ##### > \documentclass[a4paper]{article} > \begin{document} > <<echo=false,results=hide>>> library(lattice) > library(xtable) > data(cats, package="MASS") > @ > \section*{The Cats Data ---- Ejemolo que funciana con xtable} > Consider the \texttt{cats} regression example from Venables \& Ripley > (1997). The data frame contains measurements of heart and body weight > of \Sexpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female, > \Sexpr{sum(cats$Sex=="M")} male). > A linear regression model of heart weight by sex and gender can be > fitted in R using the command > <<>>> lm1 = lm(Hwt~Bwt*Sex, data=cats) > lm1 > @ > Tests for significance of the coefficients are shown in > Table~\ref{tab:coef}, a scatter plot including the regression lines is > shown in Figure~\ref{fig:cats}. > \SweaveOpts{echo=false} > <<results=tex>>> xtable(lm1, caption="Linear regression model for cats data.", > label="tab:coef") > @ > \begin{figure} > \centering > <<fig=TRUE,width=12,height=6>>> trellis.par.set(col.whitebg()) > print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r"))) > @ > \caption{The cats data from package MASS.} > \label{fig:cats} > \end{figure} > \begin{center} > \end{center} > \section{Intento 1} > <<results=tex>>> da<- data.frame(id=letters[1:5], score=1:5*2) > col<- function(x){ > ifelse(x>7, > paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), > paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) > } > da$score.string<- col(da$score) > require(xtable) > xtable(da[,c("id","score.string")]) > @ > \section{Intento 2} > <<results=tex>>> da<- data.frame(id=letters[1:5], score=1:5*2) > col<- function(x){ > ifelse(x>7, > paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), > paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) > } > da$score.string<- col(da$score) > > print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) > @ > \section{Intento - latex} > <<>>> da<- data.frame(id=letters[1:5], score=1:5*2) > col<- function(x){ > ifelse(x>7, > paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), > paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) > } > da$score.string<- col(da$score) > > xtable(da[,c("id","score.string")]) > @ > > > \end{document} > ############################################################################## > [[alternative HTML version deleted]] > > > > _______________________________________________ > R-help-es mailing list > R-help-es@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es[[alternative HTML version deleted]]
Javier No lo conocía, knitr es algo nuevo, la documentación es algo escasa, pero por sus palabras debe valer la pena probarlo. Javier From: Javier Martinez Lopez Sent: Wednesday, April 04, 2012 2:10 PM To: "Marcuzzi, Javier Rubén" Cc: r-help-es@r-project.org Subject: Re: [R-es] Sweave xtable ... Sólo por apuntarlo, no sé si conoces el paquete "knitr", que es como Sweave pero más flexible y elegante. Un saludo, Javier On 04/04/12 18:24, Marcuzzi, Javier Rubén wrote: Hola Pude resolver mi problema, el código Rnw es el siguiente \documentclass[a4paper]{article} \usepackage{color} \begin{document} <<echo=false,results=hide>>library(lattice) library(xtable) @ \section{xtable con colores} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\\color{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\\color{green}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) @ \end{document} From: Marcuzzi, Javier Rubén Sent: Tuesday, April 03, 2012 5:05 PM To: r-help-es@r-project.org Subject: Sweave xtable Hola Tengo un problema con Sweave y xtable, concretamente quiero cambiar el color según un criterio, creo que es más fácil explicar con los siguientes ejemplos, uno puede correr directamente porque es R, al siguiente código hay que guardarlo como Rnw, y luego puede ser corrido en R, donde seguramente al correr este se darán cuenta de mis dos problemas porque saltan al abrir el pdf que se obtiene como resultado. #################################3 código R #########################3 library(xtable) da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) ######################################################################### En este Rnw tengo una t más en \textcolor{blue}{", porque quedaba escrito extcolor ... ##################### Código Rnw ##### \documentclass[a4paper]{article} \begin{document} <<echo=false,results=hide>>library(lattice) library(xtable) data(cats, package="MASS") @ \section*{The Cats Data ---- Ejemolo que funciana con xtable} Consider the \texttt{cats} regression example from Venables \& Ripley (1997). The data frame contains measurements of heart and body weight of \Sexpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female, \Sexpr{sum(cats$Sex=="M")} male). A linear regression model of heart weight by sex and gender can be fitted in R using the command <<>>lm1 = lm(Hwt~Bwt*Sex, data=cats) lm1 @ Tests for significance of the coefficients are shown in Table~\ref{tab:coef}, a scatter plot including the regression lines is shown in Figure~\ref{fig:cats}. \SweaveOpts{echo=false} <<results=tex>>xtable(lm1, caption="Linear regression model for cats data.", label="tab:coef") @ \begin{figure} \centering <<fig=TRUE,width=12,height=6>>trellis.par.set(col.whitebg()) print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r"))) @ \caption{The cats data from package MASS.} \label{fig:cats} \end{figure} \begin{center} \end{center} \section{Intento 1} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) require(xtable) xtable(da[,c("id","score.string")]) @ \section{Intento 2} <<results=tex>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\ttextcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\ttextcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) print(xtable(da[,c("id","score.string")]), sanitize.text.function = function(x){x}) @ \section{Intento - latex} <<>>da <- data.frame(id=letters[1:5], score=1:5*2) col <- function(x){ ifelse(x>7, paste("\textcolor{blue}{", formatC(x, dig=2, format="f"), "}"), paste("\textcolor{red}{", formatC(x, dig=2, format="f"), "}")) } da$score.string <- col(da$score) xtable(da[,c("id","score.string")]) @ \end{document} ############################################################################## [[alternative HTML version deleted]] _______________________________________________ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]]