I'm trying to get Sweave running for automatic report generation, and it seems to run fine when just using verbatim output. However, I've ran into a problem with xtable. I would like to print the following matrix using xtable: > dim(counts) [1] 19 15 All columns are filled with real/integer numbers > 0 and < 1000. Just typing xtable(counts) gives correct LaTeX output, but running Sweave on: <<results=tex>>library(xtable) xtable(counts) @ Generates truncated TeX output: % latex table generated in R 1.6.1 by xtable 1.0-10 package % Fri Dec 20 14:50:08 2002 \begin{table} [...] 5 & 105.00 & 400.00 & 0.00 & 0.00 & 0.00 & 0.00 & 1000.00 & 542.00 & 181.00 & 858.00 & 4 62.00 & 103.00 & 744.00 & 449.00 & 93.00 \\ 6 & 201.00 & 400.00 & 0.00 & xtable(counts[1:4,]) does work. Do I have to flush the output of xtables in some way? Or is my table just too large? - Hedderik.
>>>>> "hedderik" == Hedderik van Rijn <hedderik at cmu.edu> writes:hedderik> I'm trying to get Sweave running for automatic report generation, and hedderik> it seems to run fine when just using verbatim output. However, I've hedderik> ran into a problem with xtable. I would like to print the following hedderik> matrix using xtable: I've had problems with large matrices, but never got around to figuring out why (the cheap hack solution was to split matrices and present in different tables). (matrices representing components of a model, not raw data, so it made some sense, but wasn't optimal). i.e. I think it might be a bug. best, -tony -- A.J. Rossini Rsrch. Asst. Prof. of Biostatistics U. of Washington Biostatistics rossini at u.washington.edu FHCRC/SCHARP/HIV Vaccine Trials Net rossini at scharp.org -------------- http://software.biostat.washington.edu/ ---------------- FHCRC: M: 206-667-7025 (fax=4812)|Voicemail is pretty sketchy/use Email UW: Th: 206-543-1044 (fax=3286)|Change last 4 digits of phone to FAX (my tuesday/wednesday/friday locations are completely unpredictable.)
Hedderik van Rijn
2002-Dec-22 03:42 UTC
[R] Sweave & xtable [problem solved/workaround -> bug in xtable or textConnection?]
> I'm trying to get Sweave running for automatic report generation, and > it seems to run fine when just using verbatim output. However, I've > ran into a problem with xtable.A quick solution (as long as the table is not too wide) is to include the following code after library(xtable), replacing the original print.string print.string <- function(x,...) { lapply(strsplit(x$text,"\n")[[1]],cat,"\n",file=x$file,append=x$append) return(invisible()) } The problem seems to be that textConnection() (used in the Sweave code) is not able to process the long strings that sometimes get sink'ed to it. When sending a string directly to it, a warning is triggered: ## Error/truncation with warning: con <- textConnection("output","w"); sink(file=con); paste(rep("123456789!",1000),collapse=""); ## Warning message: ## line truncated in output text connection sink() rm(last.warning) However, when a long string is send to textConnection via xtable, no warning is shown: ## Error/truncation without warning: library("xtable") con <- textConnection("output","w") sink(file=con) xtable(matrix(rnorm(1000),100)) sink() ## print(output) would show the truncated LaTeX table, warnings() doesn't show any warning I'm not sure whether textConnection or xtable should be blamed, but changing the single cat() with the lapply/strsplit/cat combination in print.string solved the/my problem. If the truncation of long strings is official/known behavior of textConnection, the following text in textConnection's help page might need some revision, i.e., some more explicit statement that long strings might get truncated. (And, maybe also a definition of what a "completed line of output" is, i.e., ending in a "\n".) An output text connection is opened and creates an R character vector of the given name in the user's workspace. This object will at all times hold the completed lines of output to the connection, and `isIncomplete' will indicate if there is an incomplete final line. Closing the connection will output the final line, complete or not. - Hedderik. P.S. I've seen a lot of bus errors and segmentation faults while trying to find the cause of the problem. If anyone is interested, I can try to see if I can reproduce those. I'm using: R : Copyright 2002, The R Development Core Team Version 1.6.1 (2002-11-01) % latex table generated in R 1.6.1 by xtable 1.0-11 package
ripley@stats.ox.ac.uk
2002-Dec-23 23:38 UTC
[R] Sweave & xtable [problem solved/workaround -> bug in xtable or textConnection?]
This should be fixed in R-patched and R-devel (and has been since Saturday). Certainly it has gone away for me. On Mon, 23 Dec 2002, Hedderik van Rijn wrote:> >>> cat(paste(rep("123456789!",1000),collapse="")) > >> Program received signal SIGSEGV, Segmentation fault. > >> 0x4207a4cb in strlen () from /lib/i686/libc.so.6 > >> > >> (The cat(...) call being obtained with up-arrow command recall) > > > > To be precise, it happens *next* time I press "up" > > This is the same behavior I encountered. The first time _always_ goes > fine, the next time (also by pressing "up", "enter") sometimes(?) > results in a segv and sometimes in a bus error. (Using R 1.6.1, Mac OS > X, latest OS X update, 10.2.3? and just released fink installed.) > > As I was not sure if I could replicate it always, I only referred to it > in a "P.S." in the second email I sent on this topic. > > - Hedderik. > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595