AndriusZ
AndriusZ

Reputation: 822

How to automatically print the value of a variable after an assignment?

I read somewhere that there is a way (with some char at the beginning or at the end of the line), which forces an automatic print result of the expression.

I want to automatically print n, i.e. without having to type n after n <- 3

n <- 3
n

Upvotes: 13

Views: 3821

Answers (1)

alko989
alko989

Reputation: 7908

If you put the expression in parentheses the result will be printed:

(n <- 3)
##[1] 3 

This works because the assignment operator <- returns the value (invisibly, which strangely is not in the documentation). Putting it in parentheses (or in print or c, or show, or cat (without newline)) makes it visible.

This "printing behaviour" of ( is documented in ?"(":

For (, the result of evaluating the argument. This has visibility set, so will auto-print if used at top-level.

Upvotes: 24

Related Questions