title: Creating an R function with a list of equations
body: <p>I <a href="https://stackoverflow.com/questions/62748018/is-there-a-way-to-create-an-r-function-using-a-complicated-string-formula-using">asked a previous question</a> and got help with creating a function with equations of the same type:</p>
  <p>Converted this:</p>
  <pre><code>HS.model &lt;- ' visual  =~ x1 + x2 + x3 
                textual =~ x4 + x5 + x6
                speed   =~ x7 + x8 + x9 '
  </code></pre>
  <p>to this:</p>
  <pre><code>myFunction &lt;- function(...) {
    params &lt;- list(...)
    lefts = params[seq(1,length(params), by=2)]
    rights = params[seq(2,length(params), by=2)]
    rights &lt;- Map(paste, rights, collapse=&quot;+&quot;)
    paste(paste0(lefts, &quot; =~&quot;, rights), collapse=&quot;\n&quot;)
  </code></pre>
  <p>which uses this input:</p>
  <pre><code>myFunction(&quot;visual&quot;, c(&quot;x1&quot;, &quot;x2&quot;, &quot;x3&quot;), 
            &quot;textual&quot;, c(&quot;x4&quot;, &quot;x5&quot;, &quot;x6&quot;), 
            &quot;speed&quot;  , c(&quot;x7&quot;, &quot;x8&quot;, &quot;x9&quot;))
  </code></pre>
  <p>This above function uses only &quot;=~&quot;
  Now I would like to combine multiple types of equations such as &quot;~~&quot; and &quot;~&quot;. For example, the list of equations would look like this:</p>
  <pre><code>model &lt;- '
    # latent variable definitions (latvars)
      ind60 =~ x1 + x2 + x3
      dem60 =~ y1 + y2 + y3 + y4
      dem65 =~ y5 + y6 + y7 + y8
    # regressions                 (regress)
      dem60 ~ ind60
      dem65 ~ ind60 + dem60
    # residual (co)variances      (covars)
      y1 ~~ y5
      y2 ~~ y4 + y6
      y3 ~~ y7
      y4 ~~ y8
      y6 ~~ y8
  '
  </code></pre>
  <p>I imagine the new function input would look like:</p>
  <pre><code>myFunction(latvars = (&quot;ind60&quot;, c(&quot;x1&quot;, &quot;x2&quot;, &quot;x3&quot;), 
                        &quot;dem60&quot;, c(&quot;y1&quot;, &quot;y2&quot;, &quot;y3&quot;, &quot;y4&quot;), 
                        &quot;dem65&quot;, c(&quot;y5&quot;, &quot;y6&quot;, &quot;y7&quot;, &quot;y8&quot;)),
             regress = (&quot;dem60&quot;, c(&quot;ind60&quot;),
                        &quot;dem65&quot;, c(&quot;ind60&quot;, &quot;dem60&quot;)),
             covars  = (&quot;y1&quot;, c(&quot;y5&quot;),
                        &quot;y2&quot;, c(&quot;y4&quot;, &quot;y6&quot;),
                        &quot;y3&quot;, c(&quot;y7&quot;),
                        &quot;y4&quot;, c(&quot;y8&quot;),
                        &quot;y6&quot;, c(&quot;y8&quot;))
            )
  </code></pre>
  <p>I tried to do this</p>
  <pre><code>myFunction &lt;- function(...) {
    params &lt;- list(...)
    lefts = params[seq(1,length(params), by=2)]
    rights = params[seq(2,length(params), by=2)]
    rights &lt;- Map(paste, rights, collapse=&quot;+&quot;)
    
    lefts2 = params[seq(3,length(params), by=2)]
    rights2 = params[seq(4,length(params), by=2)]
    rights2 &lt;- Map(paste, rights2, collapse=&quot;+&quot;)
  
    paste(paste0(lefts, &quot; =~&quot;, rights), paste0(lefts2, &quot; ~&quot;, rights2), collapse=&quot;\n&quot;)
  
  }
   
  </code></pre>
  <p>I'm obviously getting errors, and I'm unable to find a way to make the various arguments. Would like some help please. Thank you for your time.</p>