From h.wickham at gmail.com  Sun Oct 29 17:28:17 2006
From: h.wickham at gmail.com (hadley wickham)
Date: Sun, 29 Oct 2006 11:28:17 -0500
Subject: [Rd] R GUI API: suggestion for R function to put in there?
In-Reply-To: <4544DCDB.3060703@umh.ac.be>
References: <4544DCDB.3060703@umh.ac.be>
Message-ID: <f8e6ff050610290828v28339862m7ef735bfca146cba@mail.gmail.com>

> - To execute R code contained in a string and return the result of this
> evaluation in a string (including presentation of error messages and
> warnings) exactly as if this was entered at the prompt. Duncan Murdoch
> is against such a kind of function (he says a GUI should not substitute
> to the R interpreter, and it should manage and present errors or
> warnings in a better way than the teletype approach of the command

This is useful for useful for things like Sweave.  I have a modified
version of capture.output that captures everything:

capture.all <- function(code, envir = globalenv()) {
	printErrors <- function(code) {
		tryCatch(
			eval(parse(text=code), envir = envir),
			error = function(e) o(e)
		)
	}

	paste(capture.output(
	  withCallingHandlers(printErrors(code), warning = function(x) {
		cat("Warning: ", x$message, "\n")
		invokeRestart("muffleWarning")
	})), collapse="\n", sep="")
}

for my (unreleased) sweave-like package.  It's not perfect, but it has
served my needs.

Hadley




[Rd] R GUI API: suggestion for R function to put in there?
hadley wickham