title: Dynamically creating functions and expressions
body: <p>I am currently dealing with a problem. I am working on a package for some specific distributions where among other things I would like to create a function that will fit an mixture to some data. For this I would like to use for example the <code>fitdistr</code> function. The problem is that I don't know from what distributions and weights and number of components the mixture will be composed of. Hence I need a function that will dynamically create an density function of some specified mixture so the <code>fitdistr</code> function can use it. For example if the user will call:</p>
  
  <pre><code>fitmix(data,dist=c(norm,chisq),params=list(c(mean=0,sd=3),df=2),wights=c(0.5,0.5))
  </code></pre>
  
  <p>to use ML method the code needs to create an density function</p>
  
  <pre><code>function(x,mean,sd,df) 0.5*dnorm(x,mean,sd)+0.5*dchisq(x,df)
  </code></pre>
  
  <p>so it can call <code>optim</code> or <code>fitdistr</code>.</p>
  
  <p>An obvious solution is to use a lot of <code>paste</code>+<code>eval</code>+<code>parse</code> but I don't think this is the most elegant solution. A nice solution is probably hiding somewhere in non-standard evaluation and expression manipulation, but  I have not enough skills in this problematic.</p>
  
  <p>P.S. the params can be used as starting values for the optimizer.</p>