Re: [R] Exporting tables to RTF?

From: Aaron Solomon Adelman <>
Date: Thu 06 Jul 2006 - 00:25:09 EST


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.


# 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/"
	{	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="")

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

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


Aaron Solomon (ben Saul Joseph) Adelman
E-mail:, Web-sites: , http://
AOL Instant Messenger & Yahoo! Messenger: Hiergargo ICQ: 258691118
Jabber: mailing list PLEASE do read the posting guide! 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 Please read the posting guide before posting to the list.