Peng Yu
2009-Sep-07 01:13 UTC
[R] Why this statement does not print anything in an if-statement that includes 'q()'?
Hi, I run the following script. I don't understand why the second 'length(args)' does not show anything but the first one shows '0'. Is it because the command 'q()' affects anything in the if-statement. However, if I change the second 'length(args)' to 'print(length(args))', the script will print the length of 'args'. Can somebody let me know what is the difference between the 'print(x)' statement and the 'x' statement where 'x' is a variable? Regards, Peng $ Rscript not_print_in_if_block.R> args=commandArgs(T) > length(args)[1] 0> > if(length(args)!=3){+ length(args) + q() + }
jim holtman
2009-Sep-07 01:21 UTC
[R] Why this statement does not print anything in an if-statement that includes 'q()'?
You need to explicitly 'print' the second 'length' function call. Only at the top level of the interactive session is the value of an object printed out if you just reference the object. On Sun, Sep 6, 2009 at 9:13 PM, Peng Yu<pengyu.ut at gmail.com> wrote:> Hi, > > I run the following script. I don't understand why the second > 'length(args)' does not show anything but the first one shows '0'. Is > it because the command 'q()' affects anything in the if-statement. > > However, if I change the second 'length(args)' to > 'print(length(args))', the script will print the length of 'args'. Can > somebody let me know what is the difference between the 'print(x)' > statement and the 'x' statement where 'x' is a variable? > > Regards, > Peng > > $ Rscript not_print_in_if_block.R >> args=commandArgs(T) >> length(args) > [1] 0 >> >> if(length(args)!=3){ > + ? length(args) > + ? q() > + } > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Seemingly Similar Threads
- How to convert a string passed as an argument to a vector?
- Command line option to an R script running through Rscript
- How to get the current script file name in the script?
- Is there an variant of apply() that does not return anything?
- How to pop up the graphics window from Rscript?