[R] Transfer Crosstable to Word-Document

Gabor Grothendieck ggrothendieck at gmail.com
Sat Feb 16 15:02:52 CET 2008


Here is a partial solution.  It still requires some manual intervention.

# run this in R
data(infert); attach(infert)
Lines <- capture.output(CrossTable(education, induced))
ix <- grep(" | ", Lines, fixed = TRUE)
writeLines(Lines[ix], "clipboard")

Now paste the clipboard into Word, select the table in Word and use
Word's text to table command as Don had suggested.  (In Excel 2007
its on Insert > Table > Text to Table.)  When I tried this it automatically
figured out that | is the separator and created the table properly.

Note that this depends on the fact that the tabular portion of the output, which
is presumably what you want to transfer, has a space-bar-space
on every line and no other lines do.   There are many arguments to CrossTable
and if some of them do not obey that then it will have to modified accordingly
for those argument combinations.


On Feb 15, 2008 12:31 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> In that case you shouldn't be using CrossTable in the first place.
>
> Suggest you look into the functions mentioned under See Also in
> ?CrossTable or look at the source of CrossTable and modify it for
> your purposes.
>
>
> On Fri, Feb 15, 2008 at 11:52 AM,  <ukoenig at med.uni-marburg.de> wrote:
> >
> > Thank you Gabor,
> > but with 1. or 2. I don´t get
> > a table, which can be changed and formatted using the WORD table functions
> > (like changing column width and formatting text in rows).
> >
> > The result of 1. ist an bitmap and
> > the result of 2. an ASCII-Table
> >
> > A rich formatted (*.rtf) table/object would be better.
> > I tried this with the view(....) (see below).
> > Udo
> >
> >
> >
> >
> > Quoting Gabor Grothendieck <ggrothendieck at gmail.com>:
> >
> > > Here are 2 ways:
> > >
> > > 1. Display it on the screen and hit the PrintScreen button.
> > > Then paste that into Word and use Word's image editor
> > > to crop it and expand or shrink it appropriately.
> > >
> > > 2. Run this in R:
> > >
> > > capture.output(CrossTable(...whatever...), file = "clipboard")
> > >
> > > Paste the clipboard into Word and change the font of the
> > > pasted text to Courier or other fixed space font and change its
> > > size too if need be.
> > >
> > > On Fri, Feb 15, 2008 at 10:30 AM,  <ukoenig at med.uni-marburg.de> wrote:
> > > > # Dear list,
> > > > # I am an R-beginner and
> > > > # spent the last days looking for a method to insert tables produced
> > > > # with R into a word document. I thought about SPPS:  copy a table from
> > > > # an SPO-file and paste it into a word document
> > > > # (if needed do some formatting with that table).
> > > > # Annother idea was, to produce a TEX-file,
> > > > # insert it and make it a word-table.
> > > >
> > > > # I found the following libraries, which seemed to be promising:
> > > > # xtable
> > > > # prettyR
> > > > # R2HTML
> > > > # Hmisc
> > > > # SciViews / svViews
> > > >
> > > >
> > > >
> > > > #######################################################################
> > > > ## My example: a crosstable (made with CrossTable from lib gmodels ####
> > > > #######################################################################
> > > >
> > > > library(gmodels)
> > > > library(xtable)
> > > > library(svViews)
> > > >
> > > > # Data for crosstabulation
> > > > set.seed(1)
> > > > n <- 200
> > > > sex <- sample(c("f","m"),n,T)
> > > > state <- sample(c("AL","AK","CA"),n,T)
> > > >
> > > > #Create crosstab
> > > > ct <- CrossTable(state, sex, expected=TRUE, format="SPSS", digits=1)
> > > > ct #display crosstab on screen
> > > >
> > > > #Trie to produce a html file
> > > > xtable(ct) #Error message: No method!
> > > > methods(xtable)
> > > >
> > > >
> > > >
> > > > #Try to create a rich formatted table and insert it into Word
> > > > #with svViws, but only a little part of crosstab it inserted
> > > >
> > > > docdir <- "r:\\r"
> > > > WordOpen(file.path(docdir, "cross.doc"))
> > > > viewfile <- view(ct, type = "summary", browse = FALSE)
> > > > WordGoto("ctview")
> > > > WordInsertFile(viewfile, TRUE) #only a little part of crosstab inserted
> > > > WordActivate(async = TRUE)
> > > >
> > > >
> > > > #How could I create the latex-code for the crosstable?
> > > > w <- latex(ct) #would that work?
> > > >
> > > > # I could not test this, because I didn´t install Miktex yet
> > > > # (I will do this soon)
> > > >
> > > > Can someone help me?
> > > >
> > > > Thanks in advance
> > > > Udo
> > > >
> > > >
> > > > ------------------------------
> > > > Udo Koenig
> > > > Clinic for child and adolescent psychiatry
> > > > University of Marburg / Germany
> > > >
> > > > ______________________________________________
> > > > R-help at r-project.org mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> > > >
> > >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>



More information about the R-help mailing list