ukoenig at med.uni-marburg.de
2008-Feb-15 15:30 UTC
[R] Transfer Crosstable to Word-Document
# 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
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. >
I usually use the write.table() function (with tab as the delimiter) to write to a text file, then copy/paste into Word, then use Word's convert text to table command. Obviously, if one needs to do this for many, many, tables, the amount of manual manipulation is excessive. That's when products other than Word become better choices. (Framemaker, LaTeX come to mind) -Don At 4:30 PM +0100 2/15/08, 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
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).Hi Udo, Try running the example from delim.table in the prettyR package, then open the file "testdelim.html" in Word, copy and paste one of the tables into your Word document. It works for me in OpenOffice (I don't have Word at home). Jim
If you want to get nicely formatted tables in Word and are familiar with Office tools (I know it's the Evil Empire but some of us work there), I suggest that you use Excel for formatting and then insert the table into your Word document. IMHO, Excel is much superior to Word for table formatting, e.g. modifying number of significant digits, playing around with fonts and number formats, etc. And when you have gotten the formats right you can paste in modified values of the numbers in the table without having to do the formatting again. Including the table in your Word document is easy by cut-paste or creating a live link. As a user of R under Unix I haven't looked into the facilities for writing tables to Excel under Windows but there is something there. Alternatively you can write a fixed-column or tab-delimited file and easily import to Excel.
If your final goal is a word document, then you should look at the odfWeave package. ________________________________ From: r-help-bounces@r-project.org on behalf of ukoenig@med.uni-marburg.de Sent: Fri 2/15/2008 8:30 AM To: r-help@r-project.org Subject: [R] Transfer Crosstable to Word-Document # 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@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. [[alternative HTML version deleted]]
(2. attempt to post this) (Udo) Quoting Greg Snow <Greg.Snow at imail.org>:> If your final goal is a word document, then you should look at the odfWeave > package. >At work my primary goal has to be a word document, because: * we have a Windows-XP network with MS-Office software * my boss and my colleagues are not firm with other software (I have to cooperate with them) My idea is to insert simple tables into Word (what I asked for in this thread) and to make more sohisticated tables -like [1]- with latex and include them as a graphic file. I would NOT like to make such a table with MS-Word! I read an introductory Latex script and asked our admin to install the Miktex package on my computer. At home I can fortunately do what I want (Windows, Linux, OpenOffice,...). Greg: To the odfWeave package: in [2] I found the sentence "The package is currently limited to creating text documents using OpenOffice". So it doesn?t seem work with MS-Word? [1] #Taken from: http://biostat.mc.vanderbilt.edu/twiki/pub/Main/StatReport/summary.pdf, p. 28 library(Hmisc) getHdata(pbc) attach(pbc) s5 <- summary(drug ~ bili + albumin + stage + protime + sex + age + spiders, method="reverse", dta=pbc, test=TRUE) options(digits=1) print(s5, npct="both") options(digits=3) w <- latex(s5, size="smaller", npct="both", npct.size="smaller[2]", Nsize="smaller[2]", msdsize="smaller[2]", middle.bold=TRUE, landscape=TRUE) wd <- dvi(w) detach(pbc) [2] R newsletter, Volume 6/4, October 2006, p.3 -------------------------------------------- Udo K?nig Clinic for Child an Adolescent Psychiatry Philipps University of Marburg / Germany
On Feb 17, 2008 2:49 PM, Udo K?nig <ukoenig at med.uni-marburg.de> wrote:> [...] > Greg: > To the odfWeave package: in [2] I found the sentence "The package is currently > limited to creating text documents using OpenOffice". So it doesn?t seem work > with MS-Word?Udo, I think odfWeave is exactly what you need here. You can also use it with MS-Word via the SUN ODF Plugin for MS Office. It adds the capability to load and save odf docs within MS Office. Get the (free as in beer) plugin from here: http://www.sun.com/software/star/odf_plugin/whats_new.jsp Then write your document in Word, save as ODF, run odfWeave, load the result in Word and save as .doc. HTH, Tobias
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 You can programmatically create content directly in Word from R including tables, lists, paragraphs, etc. via a DCOM connection where R is the client and Word is the server. There are two packages to do this - rcom and RDCOMClient and both allow you to work at much higher levels of specificity than cutting and pasting HTML or CSV content. You'd have to learn a little about the DCOM API for Word, but there are examples. ~ D. 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. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHuGsu9p/Jzwa2QP4RAn9xAJ94KmlBA5Yl1hfT4Z0CsmL7s61qpwCfZNhn BMaw4u4FggRWn3QrKg79PAw=mElZ -----END PGP SIGNATURE-----
Zitat von Greg Snow <Greg.Snow at imail.org>:> If your final goal is a word document, then you should look at the odfWeave > package.Greg, I had a look at the odfWeave package, but it seems that complex tables, for instance produced with latex(....) can?t be produced/included, as can be done with sweave. -------------------------------------------- Udo K?nig Clinic for Child an Adolescent Psychiatry Philipps University of Marburg / Germany
Gabor Grothendieck
2008-Feb-19 18:26 UTC
[R] Extracting original variable list from lm object
Try: all.vars(formula(my.model)) On Feb 19, 2008 1:10 PM, Sung, Iyue <Iyue.Sung at lippincott.com> wrote:> Fellow R users, > > I have an lm object, from which I would like to extract the list of > original variables. > The problem I have is the formula includes functions of the covariates. > > I tried using "attr", but the result stores the transformed variable > name. For example: > > > my.model<-lm(y ~ a + log(b + 1), data=my.data) > > as.character(attr(my.model$terms, "variables"))[-1] > [1] "y" "a" "log(b + 1)" > > But I just want a character vector of the original variables ("y", "a", > "b"), not ("y", "a", "log(b + 1)"). > Does someone have a solution they could kindly share? > > Thanks, > Iyue > ---------------------------------------------------------------------------- > This e-mail and any attachments may be confidential or l...{{dropped:11}} > > ______________________________________________ > 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. >
perfect. many thanks. -----Original Message----- From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] Sent: Tuesday, February 19, 2008 1:26 PM To: Sung, Iyue Cc: r-help at r-project.org Subject: Re: [R] Extracting original variable list from lm object Try: all.vars(formula(my.model)) On Feb 19, 2008 1:10 PM, Sung, Iyue <Iyue.Sung at lippincott.com> wrote:> Fellow R users, > > I have an lm object, from which I would like to extract the list of > original variables. > The problem I have is the formula includes functions of thecovariates.> > I tried using "attr", but the result stores the transformed variable > name. For example: > > > my.model<-lm(y ~ a + log(b + 1), data=my.data) > > as.character(attr(my.model$terms, "variables"))[-1] > [1] "y" "a" "log(b + 1)" > > But I just want a character vector of the original variables ("y", > "a", "b"), not ("y", "a", "log(b + 1)"). > Does someone have a solution they could kindly share? > > Thanks, > Iyue > ---------------------------------------------------------------------- > ------ This e-mail and any attachments may be confidential or > l...{{dropped:11}} > > ______________________________________________ > 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. >------------------------------------------------------------------------ ---- This e-mail and any attachments may be confidential or l...{{dropped:24}}
Check out: https://stat.ethz.ch/pipermail/r-help/2006-March/101812.html On Feb 19, 2008 1:41 PM, Nitin Jain <nitin.jain at bms.com> wrote:> Dear R-help members, > > I am using logistic regression on a high throughput data and would like > to capture warning messages, if generated for a particular probe. The > way I am approaching it currently is: > > myResult <- data.frame(matrix(NA, nrow = 1000, ncol = 4)) > colnames(myResult) <- c("intercept", "coef", "SE", "Warnings") > myResult[, "Warnings"] <- "" ## initially set each warning as blank > > for (i in seq(nrow(myResult))) { > > options(warn = 2) ## Convert warnings to error > probeData <- origData[i, ] ## get current probe data > > fit1 <- try(glm(y ~ x, probeData)) ## fit logistic regression > > if (inherits(fit1, "try-error")) { ## change the warn option to 0 and refit > options(warn = 0) > fit1 <- try(glm(y ~ x, probeData)) > myResult[i, "Warnings"] <- names(last.warning)[1] > } > > ## Fill in the other columns of myResult > ... > } > > > When I run the above code in batch mode, I get the error: > Error in x[[jj]][iseq] <- vjj : incompatible types (from NULL to > character) in subassignment type fix > > I think the error is due to the fact that when the first warning message > is generated, last.warning still does not exist in the function (but in > some other environment), hence R is trying to assign NULL to a character. > > Can anyone tell me how to fix the above problem? > > Thanks. > > Best, > Nitin > > > > ______________________________________________ > 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. > >