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

hadley wickham h.wickham at gmail.com
Sun Oct 29 17:28:17 CET 2006


> - 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




More information about the R-devel mailing list