Re: [R] Exporting tables to RTF?

From: Aaron Solomon Adelman <aaron.solomon.adelman_at_gmail.com>
Date: Thu 06 Jul 2006 - 00:25:09 EST

Greetings.

I'd like to thank everyone who responded to my original request. I got something up and working very quickly. I'm including the resulting function below in case anyone else wants to try something like this in the future so they can find it in the archives.

Aaron


# nice.table
# This is a function which takes a table x, creates an RTF file
containing the table, and opens it in Nisus Writer Express.
# Dependencies: The R package xtable, textutil (command-line utility
in Mac OS X 10.4)
# Also required: Nisus Writer Express

# Arguments:
# x, caption, align, digits, display: As in xtable.
# file: The name of the output RTF file. If not provided, then the
caption is used for the name of the file, and the file is created on the Desktop. If no caption is provided either, then the file will have the mysterious name "x.rtf".
# NA.string: As in print.xtable.

# Bugs:
# Spaces in filenames don't come through properly. A lot of this
function is R passing commands to the shell. If you want spaces in the file name, you must prefix them with "\\" to get them through the R interpreter and the shell.

nice.table <- function(x, caption=NULL, align=NULL, digits=NULL, display=NULL, file="", NA.string="N/A")
{ library(xtable)

        xx <- xtable(x, caption=caption, align=align, digits=digits, display=display)

	x_file_location <- "~/Desktop/"
	if(is.null(caption))
	{	x_file_name <- "x"
	} else
	{	x_file_name <- gsub(" ", "_", caption, fixed=TRUE)
		x_file_name <- gsub(":", "-", x_file_name)
		x_file_name <- gsub("/", "_", x_file_name)
	}
	html_x_path <- paste(x_file_location, x_file_name, ".html", sep="")
	print.xtable(xx, type="html", file=html_x_path,  
caption.placement="top", NA.string=NA.string)

        if(file == "") file <- paste(x_file_location, x_file_name, ".rtf", sep="")

        incantation <- paste("textutil -convert rtf -output ", file, " ", html_x_path, sep="")

	system(incantation)
	incantation <- paste("rm ", html_x_path, sep="")
	system(incantation)

	incantation <- paste("open -b com.nisus.NisusWriter ", file, sep="")
	system(incantation)

}

Aaron Solomon (ben Saul Joseph) Adelman
E-mail: Aaron.Solomon.Adelman@gmail.com, adelmaas@musc.edu Web-sites: http://weirdthingoftheday.blogspot.com/ , http:// people.musc.edu/~adelmaas/
AOL Instant Messenger & Yahoo! Messenger: Hiergargo ICQ: 258691118
Jabber: Aaron.Solomon.Adelman@gmail.com

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Jul 06 01:39:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 06 Jul 2006 - 04:17:28 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.