[R] Exporting tables to RTF?

Aaron Solomon Adelman aaron.solomon.adelman at gmail.com
Wed Jul 5 16:25:09 CEST 2006


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 at gmail.com, adelmaas at 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 at gmail.com



More information about the R-help mailing list