Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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
like image 911
AndriusZ Avatar asked Jul 29 '14 11:07

AndriusZ


People also ask

How do you assign a value to a variable in Python?

The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”. After executing this line, this number will be stored into this variable.

How do you print an assigned value in Python?

Now, you can assign a value to a variable and return that same value using "assignment expressions" (colloquially called "the Walrus operator"). Which will print "Hello, world" and assign the string "world" to the variable "w" all in one line. good job......!

How do you print the value of a variable?

As you might expect, printf can also print the values of variables. Here's an example: printf("The answer is %d\n", answer); The arguments to printf are a ``control'' string followed by the variables whose values you wish to print.

How do you get the value of a variable in Python?

Python variables store values in a program. You can refer to the name of a variable to access its value. The value of a variable can be changed throughout your program. Variables are declared using this syntax: name = value.


1 Answers

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.

like image 128
alko989 Avatar answered Oct 07 '22 02:10

alko989