Search code examples
rcoercion

How to coerce input from readline( ) from character to numeric


I'm a new R user and I'm working on writing a function that will prompt the user for data using readline(). I'd like for the answer to be used in a formula later on in the function.

I'm getting the error, "Error in w/m : non-numeric argument to binary operator"

I found the input from the user was character class and I'd like to convert it to numeric, so that I can run it through the (wh) formula.

My code looks like this...

lands<-function(w, g, r, b, u, l, au, mc, uw, gw, wr, bw, gr, gb, ug, rb, ur, ub){

l<-readline("How many lands are you playing?")

au<-readline("How many of these lands can be tapped for more than or less than 2 colors      of mana? note: omit fetch lands that get two different types of land, but include fetch lands that can get any basic.")

mc<-readline("Count the number of mana symbols on your cards an input the number. Be sure to include flash back costs that differ from regular mana costs, hybrid mana should be split (add 1 for odd numbers).")

w<-readline("How many mana symbols are white?")
      wh<-(((w/mc)*(l-au)))    ##-((uw+gw+wr+bw)/2))
}

Solution

  • You need to convert via as.numeric

    foo <- as.numeric(readline("Question text? "))