title: How to create dynamic query in data table in R
body: <p><strong>updated with example:</strong>
  I have a function as follows:</p>
  
  <pre><code>myfun &lt;- function(DT, var){  
    for(i in 1:length(var)){
      s = substitute(!(is.na(x) | is.nan(x)), list(x=as.symbol(eval(var[i]))))
      DT = DT[eval(s)]
    }
    return(DT)
  }
  </code></pre>
  
  <p>input:</p>
  
  <pre><code>&gt; dt = data.table(id=c(1,2,3,4,5), x=c(1,2,NA,4,5), y=c(1,NA,3,4,NA))
  &gt; dt
     id  x  y
  1:  1  1  1
  2:  2  2 NA
  3:  3 NA  3
  4:  4  4  4
  5:  5  5 NA
  </code></pre>
  
  <p>runs:</p>
  
  <pre><code>&gt; myfun(dt, var=c("x", "y"))
     id x y
  1:  1 1 1
  2:  4 4 4
  &gt; myfun(dt, var=c("x"))
     id x  y
  1:  1 1  1
  2:  2 2 NA
  3:  4 4  4
  4:  5 5 NA
  </code></pre>
  
  <p><code>var</code> is an character array of some variables in DT. The goal is to only obtain rows in DT which do not have any <code>NA</code> or <code>NaN</code> wrt to any of variables in <code>var</code>.</p>
  
  <p><strong>I do not WANT the for loop.</strong> I want to construct a query <code>s</code> with all the conditions and then evaluate that query for <code>DT</code>. for the first case I want:</p>
  
  <pre><code>s = !(is.na(x) | is.nan(x) | is.na(y) | is.nan(y))
  </code></pre>
  
  <p>and for the second case I want:</p>
  
  <pre><code>s = !(is.na(x) | is.nan(x))
  </code></pre>
  
  <p>How can I construct a dynamic query <code>s</code> and just run it once as an <code>i/where</code> query in the data table.</p>
  
  <p>More generally how can I create dynamic <code>expression</code> based on input. Using <code>expression(paste())</code> did not help me. Then I can use <code>substitute</code>.</p>