[R] Sweave & xtable [problem solved/workaround -> bug in xtable or textConnection?]
Hedderik van Rijn
hedderik at van-rijn.org
Sun Dec 22 03:42:02 CET 2002
> 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
More information about the R-help
mailing list