[R] eval(expr) without printing to screen?

John Sorkin jsorkin at grecc.umaryland.edu
Sun Sep 20 00:21:10 CEST 2009


David,

You can used the sink function to direct the output to a file. When you do this, nothing is printed on the screen.

e.g. 
sink("c:\\RResu.txt")   #Defines location to which output is to be written
#R code goes here
sink()                           #Turns off redirection, any code after this  will print to the screen

John

John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

>>> David Winsemius <dwinsemius at comcast.net> 9/19/2009 5:01 PM >>>

On Sep 19, 2009, at 4:48 PM, Nick Matzke wrote:

> Hi,
>
> I have a script which I source, which evaluates a changing  
> expression call hundreds of times.  It works, but it prints to  
> screen each time, which is annoying.  There must be simple way to  
> suppress this, or to use a slightly different set of commands, which  
> will be obvious to those wiser than I...
>
>
> Here is a simpler mockup which shows the issue:
>
> x = data.frame(rbind(c(1,2,3),c(1,2,3)))
> xnames = c("a", "b", "c")
> names(x) = xnames
>
> for(i in 1:length(x))
> {
> # Create a varying string expression
> expr = paste("y = x$", xnames[i], "[1]", sep="")
>
> # evaluate expression
> eval(parse(text=print(expr)))

Why are you printing expr? Seems that you are making it difficult to  
achieve your goal of quiet execution if you print the expressions  
inside the parse function.

>
> # This command prints the expression to screen even when embedded in  
> a function in a sourced script.  I would prefer it didn't!
> }
>
>
> PS: I have to go through this rigamarole:
>
> expr = "y1 = x$c[1]"
> eval(parse(text=print(expr)))
>
> Because the following doesn't work, even though it seems like it  
> should:
> expr = "y = x$c[2]"
> eval(expr)
>
-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help 
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
and provide commented, minimal, self-contained, reproducible code.

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}




More information about the R-help mailing list