[R] Printing contents of a variable
William Dunlap
wdunlap at tibco.com
Fri Aug 3 22:57:44 CEST 2012
> Source()-ing turns off the auto-printing that normally happens at the
> command line. This is a feature, because source()ing is often used to
> set up things automatically behind the scenes that most folks don't
> need.
>
> If you want to force a print, put in a print() command explicitly.
Or use the print=TRUE or echo=TRUE arguments to source().
> cat(readLines("y.R"), sep="\n")
y <- 1:10
y
10:1
> source("y.R", print=TRUE) # autoprint
[1] 1 2 3 4 5 6 7 8 9 10
[1] 10 9 8 7 6 5 4 3 2 1
> source("y.R", echo=TRUE, prompt="y.R> ") # echo input and autoprint
y.R> y <- 1:10
y.R> y
[1] 1 2 3 4 5 6 7 8 9 10
y.R> 10:1
[1] 10 9 8 7 6 5 4 3 2 1
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of R. Michael Weylandt
> Sent: Friday, August 03, 2012 1:47 PM
> To: darnold
> Cc: r-help at r-project.org
> Subject: Re: [R] Printing contents of a variable
>
> On Fri, Aug 3, 2012 at 3:39 PM, darnold <dwarnold45 at suddenlink.net> wrote:
> > All,
> >
> > Can someone explain why this does not print the contents of x when I source
> > this file?
> >
> > CoinTosses <- function(n,print=TRUE) {
> > x <- sample(c(0,1), n, replace=TRUE)
> > y <- x
> > y[y==0] <- "T"
> > y[y==1] <- "H"
> > p <- sum(x)/n
> > p
> > }
> >
> > x <- CoinTosses(40)
> > x
> >
> > On the other hand, if I source this file:
> >
> > CoinTosses <- function(n,print=TRUE) {
> > x <- sample(c(0,1), n, replace=TRUE)
> > y <- x
> > y[y==0] <- "T"
> > y[y==1] <- "H"
> > p <- sum(x)/n
> > p
> > }
> >
> > Then run at the command line:
> >
> > x <- CoinTosses(40)
> > x
> >
> > The result is:
> >
> >> x <- CoinTosses(40)
> >> x
> > [1] 0.475
> >
> > Not sure why the former does not display the contents of x.
> >
> > Thanks.
>
> Source()-ing turns off the auto-printing that normally happens at the
> command line. This is a feature, because source()ing is often used to
> set up things automatically behind the scenes that most folks don't
> need.
>
> If you want to force a print, put in a print() command explicitly.
>
> Best,
> Michael
>
> >
> > D.
> >
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Printing-contents-of-a-
> variable-tp4639106.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list