[R] Printing contents of a variable
(Ted Harding)
Ted.Harding at wlandres.net
Fri Aug 3 23:24:16 CEST 2012
On 03-Aug-2012 20:46:56 R. Michael Weylandt wrote:
> 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
And to the above I would add the explicit quote from the help shown
by '?source':
Note that running code via 'source' differs in a few respects
from entering it at the R command line. Since expressions
are not executed at the top level, auto-printing is not done.
So you will need to include explicit 'print' calls for things
you want to be printed (and remember that this includes plotting
by 'lattice', FAQ Q7.22).
So, if you want your file to display the value of 'x' when it is
sourced, its last line should be as in:
x <- CoinTosses(40)
print(x)
However, after you have source()'d the code you have written it will
have created the variable 'x' in your environment.
Hence, after running source("whatever you filename is"), if you
then enter
x
you will get the value of 'x'.
Note in addition that the function source() does return a value,
though it is "invisible", and so you again have to explicitly
print() it. For example, when I put your code in file "temp.R",
simply executing 'source("temp.R")' yield nothing visible, as
you observed (though 'x' is there with its value). But when I
execute:
print(source("temp.R"))
I get:
# $value
# [1] 0.725
# $visible
# [1] TRUE
(0.725 being the value of x).
The fact that source() returns a value (here a list) does not
seem to be in the documentation!
The issue of when a value is printed to the screen, or not,
in R can be a bit subtle, but the above quote from '?source'
is a useful general rule: If an expression is executed at the
top level (e.g. you execute 'x' at the command line) then in
general its value will be printed (unless it has been defined
to be "invisible" -- see '?invisible' -- which which case its
value will be available within R but will not print automatically).
However, if executed "below" the top level, i.e. inside a
function, or within a loop, then it will not be printed
automatically -- that then has to be forced by print(), cat(),
or the like.
Hoping this helps,
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 03-Aug-2012 Time: 22:24:08
This message was sent by XFMail
More information about the R-help
mailing list