I want to read a numeric expression (ex:- 9/3) from the user during runtime and print the answer(3). I used readline() but since it reads the line as a string, when as.numeric() function is applied the output is NaN. How do I convert the expression to get a numeric value?
This is the code I used.
expression = readline(prompt = "Enter the expression : ");
exp = as.numeric(expression)
expression = readline(prompt = "Enter the expression : ") exp = eval(parse(text=expression))
- this is potentially dangerous (the user could enter, e.g.,
expressionalso refer to built-in functions in R; this code will work, but it’s best practice to avoid names of existing functions