[R] Printing contents of a variable

R. Michael Weylandt michael.weylandt at gmail.com
Fri Aug 3 22:46:56 CEST 2012


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.



More information about the R-help mailing list