Variables Overview with xtable in R

user734124 picture user734124 · May 5, 2011 · Viewed 9.1k times · Source

I'm wondering if it's possible to create a xtable from the command str(x) to get an overview from the variables you use. This would be a nice feature to introduce someone to the dataset, but it's annoying to create it by yourself. So whta I tried is to make a xtable like this:

str(cars)
require(xtable)
xtable(str(cars))

the cars dataset is given from R. Unfortunately xtable doesn't give a Latexcode for str(). Is it possible outsmart R here? Here are the main commands that xtable will understand:

methods(xtable)

Any ideas?

Answer

Ramnath picture Ramnath · May 5, 2011

Another package to look at is reporttools. Here is an short piece of code to illustrate its usage on the tips dataset from reshape package. Both the summary statements produce latex code which can be copy pasted into a document, or used for weaving.

library(reporttools)
data(tips, package = 'reshape')

# summarize numeric variables
tableContinuous(tips[,sapply(tips, is.numeric)])

# summarize non-numeric variables
tableNominal(tips[,!sapply(tips, is.numeric)])

EDIT. If you really MUST use str, then here is one way to go about it

str_cars = capture.output(str(cars))
xtable(data.frame(str_cars))

OUTPUT FROM REPORTTOOLS:enter image description hereenter image description here