title: Checking the validity of a function's arguments in R
body: <p>I'd like to create a function that first checks to make sure the arguments passed to that function are valid. My particular function takes two arguments and I've been successful in checking the validity of the first argument. However, checking the second argument is giving me trouble.</p>
  
  <p>I'd like to check that the input for <code>state</code> is a member of <code>state.abb</code>. This seems to work correctly.</p>
  
  <p>I'd also like to check that the input for <code>outcome</code> is a member of <code>"heart attack", "heart failure", "pneumonia"</code>. This doesn't work correctly.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>best &lt;- function(state, outcome){
  
    ## Read outcome data
  
    outcome &lt;- read.csv("outcome-of-care-measures.csv", colClasses = "character")
  
    ## Check that state and outcome are valid
  
    if (!any(state == state.abb)){
      stop("invalid state")
    }
    if (!any(outcome == c("heart attack", "heart failure", "pneumonia"))){
      stop("invalid outcome")
    }
  
    ## The function will do other things after this is sorted out.
  
  }
  </code></pre>
  
  <p>When I enter a <em>wrong</em> state abbreviation the function gives me the proper error:</p>
  
  <pre><code>&gt; best("FF", "heart failure")
  Error in best("FF", "heart failure") : invalid state
  </code></pre>
  
  <p>Now when I enter a <em>correct</em> state abbreviation and a <em>wrong</em> outcome the function appears to give me the correct error:</p>
  
  <pre><code>&gt; best("CA", "this is wrong")
  Error in best("CA", "this is wrong") : invalid outcome
  </code></pre>
  
  <p>Yet this same error persists even when I have a valid argument for outcome:</p>
  
  <pre><code>&gt; best("CA", "heart failure")
  Error in best("CA", "heart failure") : invalid outcome
  </code></pre>
  
  <p>Can anyone give me a bump in the right direction on sorting out the validity check of the second argument?</p>
  
  <p><strong>Edit</strong>: Per <a href="https://stackoverflow.com/users/1327739/lukea">lukeA</a>'s comment it appears the issue might stem from the <code>read.csv</code> command. I've now included that command in the above code.</p>