0

In R Script, I need to

  1. read input from command line
  2. check if the input is numeric or character.
  3. Print Error, if the input is a Character

I'm converting the input into numeric using

  a = as.integer(a1)

If User has entered character, a becomes NA

When I try below

if(is.na(a) == FALSE){
  cat("<b>Please enter a numeral to continue the conversation<b><br>"
)

I get a warning

Warning message: NAs introduced by coercion

is.na returns logical value, I got this confirmed using

cat(mode(is.na(a))

How do I clear this warning message?

  • Use `is.numeric` to check if you have a number before doing `a = as.integer(a1)`. Something like `if(!is.numeric(a)) stop('Please enter a numeral to continue the conversation') else `a <- as.integer(a)` – Ronak Shah Oct 24 '20 at 02:12
  • No Sir, ...I tried that, ... Input from commandline is always character, If I pass aab or 4..... is.numeric returns FALSE so can't so ..... Thank you for this though – ManaSmitha MN Oct 24 '20 at 03:33
  • Yes, you are right. Try with `if(!grepl('^\\d+$', a))` – Ronak Shah Oct 24 '20 at 03:40
  • Respected Ronak Sir, Thank you very much.... Your input helped me to solve this. I need to accept both positive and negative numbers.... I used your input and modified it for negative integers. if(!grepl('^[-+]?\\d+*$', a)) I fear, I won't be able to buy you a Coffee, but heart-felt Thank you. – ManaSmitha MN Oct 28 '20 at 14:52

0 Answers0