Vector of Numbers Not Output to Screen

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Vector of Numbers Not Output to Screen

Dario Strbenac-2
Hello,

I have a block of code that has two head calls at the end, but only the second is shown on screen. If I manually execute the statement which is not showing, it works. I thought that if statements are not functions. It is behaving as one.

> if(1 < 2)
+ {
+   x<-rnorm(100)
+   y <- rpois(10, 5)
+   head(x)
+   head(y)
+ }
[1] 4 4 5 4 8 3
>
> head(x)
[1] -1.89083874  0.42442102  0.96114276  0.48004716  1.94358108 -0.02654324

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

--------------------------------------
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Sarah Goslee
Hi,

You can get the behaviour you want using the print() command:

if(1 < 2)
{
  x<-rnorm(100)
  y <- rpois(10, 5)
  print(head(x))
  print(head(y))
}


Sarah

On Thu, Jul 17, 2014 at 2:00 AM, Dario Strbenac
<[hidden email]> wrote:

> Hello,
>
> I have a block of code that has two head calls at the end, but only the second is shown on screen. If I manually execute the statement which is not showing, it works. I thought that if statements are not functions. It is behaving as one.
>
>> if(1 < 2)
> + {
> +   x<-rnorm(100)
> +   y <- rpois(10, 5)
> +   head(x)
> +   head(y)
> + }
> [1] 4 4 5 4 8 3
>>
>> head(x)
> [1] -1.89083874  0.42442102  0.96114276  0.48004716  1.94358108 -0.02654324
>
>> sessionInfo()
> R version 3.1.1 (2014-07-10)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Rui Barradas
Hello,

Also, unlike what the op says, if statements are functions, explaining
the behavior he got.

Hope this helps,

Rui Barradas

Em 17-07-2014 16:53, Sarah Goslee escreveu:

> Hi,
>
> You can get the behaviour you want using the print() command:
>
> if(1 < 2)
> {
>    x<-rnorm(100)
>    y <- rpois(10, 5)
>    print(head(x))
>    print(head(y))
> }
>
>
> Sarah
>
> On Thu, Jul 17, 2014 at 2:00 AM, Dario Strbenac
> <[hidden email]> wrote:
>> Hello,
>>
>> I have a block of code that has two head calls at the end, but only the second is shown on screen. If I manually execute the statement which is not showing, it works. I thought that if statements are not functions. It is behaving as one.
>>
>>> if(1 < 2)
>> + {
>> +   x<-rnorm(100)
>> +   y <- rpois(10, 5)
>> +   head(x)
>> +   head(y)
>> + }
>> [1] 4 4 5 4 8 3
>>>
>>> head(x)
>> [1] -1.89083874  0.42442102  0.96114276  0.48004716  1.94358108 -0.02654324
>>
>>> sessionInfo()
>> R version 3.1.1 (2014-07-10)
>> Platform: i386-w64-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Jeff Newmiller
In reply to this post by Dario Strbenac-2
This question is related to FAQ 7.16, which you should look up.

In general, whenever you just type a variable or function directly at the
command line, R prints the result for you. Inside code blocks of any kind,
it does not do that, so you need to use the print function yourself.

On Thu, 17 Jul 2014, Dario Strbenac wrote:

> Hello,
>
> I have a block of code that has two head calls at the end, but only the second is shown on screen. If I manually execute the statement which is not showing, it works. I thought that if statements are not functions. It is behaving as one.
>
>> if(1 < 2)
> + {
> +   x<-rnorm(100)
> +   y <- rpois(10, 5)
> +   head(x)
> +   head(y)
> + }
> [1] 4 4 5 4 8 3
>>
>> head(x)
> [1] -1.89083874  0.42442102  0.96114276  0.48004716  1.94358108 -0.02654324
>
>> sessionInfo()
> R version 3.1.1 (2014-07-10)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> --------------------------------------
> Dario Strbenac
> PhD Student
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> [hidden email] 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.
>

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

David Winsemius
In reply to this post by Rui Barradas

On Jul 17, 2014, at 9:27 AM, Rui Barradas wrote:

> Hello,
>
> Also, unlike what the op says, if statements are functions, explaining the behavior he got.

I'm not sure that is correct. The help page says if() is a control-construct. I think the function is actually "{"

> {rnorm(10); rpois(10, 3)}
 [1] 5 3 5 5 7 3 4 4 0 5

See

?Paren

--
David.

>
> Hope this helps,
>
> Rui Barradas
>
> Em 17-07-2014 16:53, Sarah Goslee escreveu:
>> Hi,
>>
>> You can get the behaviour you want using the print() command:
>>
>> if(1 < 2)
>> {
>>   x<-rnorm(100)
>>   y <- rpois(10, 5)
>>   print(head(x))
>>   print(head(y))
>> }
>>
>>
>> Sarah
>>
>> On Thu, Jul 17, 2014 at 2:00 AM, Dario Strbenac
>> <[hidden email]> wrote:
>>> Hello,
>>>
>>> I have a block of code that has two head calls at the end, but only the second is shown on screen. If I manually execute the statement which is not showing, it works. I thought that if statements are not functions. It is behaving as one.
>>>
>>>> if(1 < 2)
>>> + {
>>> +   x<-rnorm(100)
>>> +   y <- rpois(10, 5)
>>> +   head(x)
>>> +   head(y)
>>> + }
>>> [1] 4 4 5 4 8 3
>>>>
>>>> head(x)
>>> [1] -1.89083874  0.42442102  0.96114276  0.48004716  1.94358108 -0.02654324
>>>
>>>> sessionInfo()
>>> R version 3.1.1 (2014-07-10)
>>> Platform: i386-w64-mingw32/i386 (32-bit)
>>>
>>> locale:
>>> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
>>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>> [5] LC_TIME=English_United States.1252
>>>
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>
>>
>
> ______________________________________________
> [hidden email] 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.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Rolf Turner
On 18/07/14 11:32, David Winsemius wrote:

>
> On Jul 17, 2014, at 9:27 AM, Rui Barradas wrote:
>
>> Hello,
>>
>> Also, unlike what the op says, if statements are functions, explaining the behavior he got.
>
> I'm not sure that is correct. The help page says if() is a control-construct. I think the function is actually "{"
>
>> {rnorm(10); rpois(10, 3)}
>   [1] 5 3 5 5 7 3 4 4 0 5
>
> See
>
> ?Paren

It has nothing to do either with if() or Paren.  It is the ***user's***
function that is suppressing the output.  Consider:

foo <- function(){
x <- 17
x
y <- 42
y
}

If you type foo() you get

[1] 42

which is the *value returned by the function.  The value of the "x"
statement inside foo() is made invisible (and would have to be enclosed
by print() to be made visible).

cheers,

Rolf


--
Rolf Turner
Technical Editor ANZJS

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Bert Gunter
Rolf et.al

I have not followed this thread closely and so have nothing to say
about whose or what explanation is correct.

However, the following statement is misleading, if not wrong:

---------------------------
foo <- function(){
x <- 17
x
y <- 42
y
}

If you type foo() you get

[1] 42

which is the *value returned by the function.  *****The value of the
"x" statement inside foo() is made invisible *****(and would have to
be enclosed by print() to be made visible).

-------------
The reason I consider the asterisked passage misleading is that this
is a matter of scope. "made invisible" implies some sort of
legerdemain.  The "x" defined within the function only exists within
the function environment and the binding of the symbol "x" to the
value there disappears when the function returns (as you well know).

ergo:

> x <- 1
>
> foo<- function() {
+   x <- 2
+   x
+ }
>
> foo()
[1] 2
> x
[1] 1

Also, in general, printing from within a function is a side effect and
is generally discouraged in functional style program in favor of
explicitly returning the results. However, this is honored as much in
the breach as the observance as, for example, that;s the way most of
traditional R graphics works.

Anyway, the horse is now well and truly dead, I think.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Thu, Jul 17, 2014 at 6:48 PM, Rolf Turner <[hidden email]> wrote:

> On 18/07/14 11:32, David Winsemius wrote:
>>
>>
>> On Jul 17, 2014, at 9:27 AM, Rui Barradas wrote:
>>
>>> Hello,
>>>
>>> Also, unlike what the op says, if statements are functions, explaining
>>> the behavior he got.
>>
>>
>> I'm not sure that is correct. The help page says if() is a
>> control-construct. I think the function is actually "{"
>>
>>> {rnorm(10); rpois(10, 3)}
>>
>>   [1] 5 3 5 5 7 3 4 4 0 5
>>
>> See
>>
>> ?Paren
>
>
> It has nothing to do either with if() or Paren.  It is the ***user's***
> function that is suppressing the output.  Consider:
>
> foo <- function(){
> x <- 17
> x
> y <- 42
> y
> }
>
> If you type foo() you get
>
> [1] 42
>
> which is the *value returned by the function.  The value of the "x"
> statement inside foo() is made invisible (and would have to be enclosed by
> print() to be made visible).
>
> cheers,
>
> Rolf
>
>
> --
> Rolf Turner
> Technical Editor ANZJS
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Dario Strbenac-2
In reply to this post by Rolf Turner
The example in the question was not inside a user function.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Rolf Turner
On 18/07/14 15:00, Dario Strbenac wrote:
> The example in the question was not inside a user function.

Don't be silly.

cheers,

Rolf Turner

--
Rolf Turner
Technical Editor ANZJS

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Dario Strbenac-2
It's a plausible use-case. For example, in the example section of a help file.

if(require(aPackage))
{
  # Do computations.
  # Show beginning of first result vector.
  # Show beginning of second result vector.
}
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Duncan Murdoch-2
In reply to this post by Dario Strbenac-2
On 17/07/2014, 10:00 PM, Dario Strbenac wrote:
> The example in the question was not inside a user function.

The explanations you were given were slightly inaccurate.  The usual
rule is that results returned at the top level are printed unless they
are marked as invisible.  (There are a few cases where "top level" is
faked, e.g. in example code, and in Sweave.)

When you do something like

if(test) { a; b; c }

you have an expression that returns NULL invisibly if the test is FALSE,
and returns the value of the block (i.e. c) visibly if it is TRUE.  It
is the value of the if that is printed.

There is no difference in the handling of a, b and c:  each is an
expression that returns the value of the corresponding variable without
marking it as invisible.  But none of them are top-level expressions, so
none of them print.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

David Winsemius

On Jul 20, 2014, at 6:30 AM, Duncan Murdoch wrote:

> On 17/07/2014, 10:00 PM, Dario Strbenac wrote:
>> The example in the question was not inside a user function.
>
> The explanations you were given were slightly inaccurate.  The usual
> rule is that results returned at the top level are printed unless they
> are marked as invisible.  (There are a few cases where "top level" is
> faked, e.g. in example code, and in Sweave.)
>
> When you do something like
>
> if(test) { a; b; c }
>
> you have an expression that returns NULL invisibly if the test is FALSE,
> and returns the value of the block (i.e. c) visibly if it is TRUE.  It
> is the value of the if that is printed.
>
> There is no difference in the handling of a, b and c:  each is an
> expression that returns the value of the corresponding variable without
> marking it as invisible.  But none of them are top-level expressions, so
> none of them print.

I'm not sure what that last one was intended to mean but it seemed to imply that nothing would be printed if those expressions had values (even if the interpreter were able to find values in one of hte enclosing environments). That would not be what I expected. I think of curved-braces as a function and the results of the last evaluation would be expected to be returned:

> test <- TRUE
> a=2;b=3;c=4
> if(test){a;b;c}
[1] 4

If one of them had no value, an error would be thrown.

> rm(b)
> if(test){a;b;c}
Error: object 'b' not found

--

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vector of Numbers Not Output to Screen

Duncan Murdoch-2
On 20/07/2014, 5:46 PM, David Winsemius wrote:

>
> On Jul 20, 2014, at 6:30 AM, Duncan Murdoch wrote:
>
>> On 17/07/2014, 10:00 PM, Dario Strbenac wrote:
>>> The example in the question was not inside a user function.
>>
>> The explanations you were given were slightly inaccurate.  The usual
>> rule is that results returned at the top level are printed unless they
>> are marked as invisible.  (There are a few cases where "top level" is
>> faked, e.g. in example code, and in Sweave.)
>>
>> When you do something like
>>
>> if(test) { a; b; c }
>>
>> you have an expression that returns NULL invisibly if the test is FALSE,
>> and returns the value of the block (i.e. c) visibly if it is TRUE.  It
>> is the value of the if that is printed.
>>
>> There is no difference in the handling of a, b and c:  each is an
>> expression that returns the value of the corresponding variable without
>> marking it as invisible.  But none of them are top-level expressions, so
>> none of them print.
>
> I'm not sure what that last one was intended to mean but it seemed to imply that nothing would be printed if those expressions had values (even if the interpreter were able to find values in one of hte enclosing environments). That would not be what I expected. I think of curved-braces as a function and the results of the last evaluation would be expected to be returned:

I don't understand your misunderstanding.  The expression "{ a; b; c }"
(ignore the quotes here and later) is found by evaluating a, then
evaluating b, then evaluating c, and the value of c is returned as the
value of the whole expression.  Braces don't affect visibility, so if c
is 4 and no error occurs, the value of "{ a; b; c }" is a visible 4.

>> test <- TRUE
>> a=2;b=3;c=4
>> if(test){a;b;c}
> [1] 4

In this case, the expression isn't "{ a; b; c }", it's "if (test) { a;
b; c }".  Since test is TRUE, that returns the value of "{ a; b; c }"
visibly, i.e. the value is 4.

If test had been FALSE, the value would be NULL, marked as invisible.
Braces don't affect visibility, but parens do, so

if (FALSE) 4

and

{if (FALSE) 4}

both print nothing, but

(if (FALSE) 4)

will print NULL.  All three versions have the value NULL, as you could
see if you assigned them to a variable, e.g.

x <- if (FALSE) 4
x
x <- {if (FALSE) 4}
x
x <- (if (FALSE) 4)
x

which will print NULL three times.

>
> If one of them had no value, an error would be thrown.
>
>> rm(b)
>> if(test){a;b;c}
> Error: object 'b' not found

I don't see what this has to do with the previous discussion.  If you
thought I was talking about errors in expressions, you misunderstood me.

Duncan Murdoch

______________________________________________
[hidden email] 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.