title: R add multiple different inputs interacted within an lapply function
body: <p>I am trying to run a complex repeated function in r using lapply to create multiple datasets and perform multiple similar analyses. My base function works fine, essentially being the following:</p>
  <p><code>models &lt;- paste0(&quot;outcome&quot;, 1:10, &quot; ~ explanatoryvariable | fe_variable |0|fe_variable&quot;) |&gt; lapply(\(x) felm(as.formula(x), data = df))</code></p>
  <p>I then plot models based on that formula:</p>
  <p><code>plot1 &lt;- plot_model(models[[1]],  show.values = TRUE, value.offset = .3, vline.color = &quot;Blue&quot;, vline.size = 0.1, p.threshold = 0.05, colors = &quot;Black&quot;, digits = 3)</code></p>
  <p>I do this for all plots 1-10. This allows me to perform the same regression analysis for multiple versions of <code>explanatoryvariable</code>, which are all included in my dataset with their corresponding number 1-10.</p>
  <p>This works perfectly, however I now need to repeat this with a more complex formula. I need to add an interaction term to the orginal function. However, the variable being interacted is not the same between each model. Essentially, I need the following:</p>
  <p><code>models &lt;- paste0(&quot;outcome&quot;, 1:10, &quot; ~ explanatoryvariable*interactionvariable(x) | fe_variable |0|fe_variable&quot;) |&gt; lapply(\(x) felm(as.formula(x), data = df))</code></p>
  <p>Where <code>interactionvariable</code> changes for each model.</p>
  <p>So, when I create the plots</p>
  <p><code>plot1</code> will include <code>explanatoryvariable</code> interacted with <code>interactionvariable1</code> but <code>plot2</code> will include <code>explanatoryvariable</code> interacted with <code>interactionvariable2</code>, and so on.</p>
  <p>Because of the way my function with <code>lapply</code> was written before, I cannot insert this changing interactive variable into the pasted text. Is there a way to design an <code>lapply</code> function with these two changing inputs? How can I change the code to preserve the overall function for multiple models/plots, but adjust it for this more complex interaction term? Is it possible to have to sets of changing variables within the <code>lapply</code> function, especially given that they must be part of an interaction term?</p>
  <p><em>Note: <code>plot_model</code> is from the <code>sjPlot</code> package and <code>felm</code> is from the <code>lfe</code> package</em></p>
  <p><strong>Update with attempts using <code>map</code> function</strong></p>
  <p>I tried using the <code>map</code> function and seem closer but am receiving errors:</p>
  <pre><code>input_fun&lt;-function(data, input1, input2){
    felm(as.formula(paste(&quot;outcome&quot;,input1,&quot; ~ explanatoryvariable*&quot;,input2, &quot;| fe_variable |0|fe_variable&quot;, collapse = &quot;&quot;, sep = &quot;&quot;, data = data)))}
  </code></pre>
  <pre><code>model &lt;-map(.x = 1:2, .f =  ~ input_fun(data= df, input1 = 1:10, input2 = c(interactionvariable1,interactionvariable2, ...)))
  </code></pre>
  <p>which returns the following error:</p>
  <p><code>Error in str2lang(x) : &lt;text&gt;:69:396: unexpected symbol</code></p>
  <p>I also tried:</p>
  <pre><code>input_fun&lt;-function(data, input1, input2){
    felm(as.formula(paste(&quot;outcome&quot;,input1,&quot; ~ explanatoryvariable*&quot;,input2, &quot;| fe_variable |0|fe_variable |data = df&quot;, collapse = &quot;&quot;, sep = &quot;&quot;)))}
  </code></pre>
  <pre><code>model &lt;-map_chr(.x = 1:2, .f =  ~ input_fun(data= df, input1 = 1:10, input2 = c(interactionvariable1,interactionvariable2, ...)))
  </code></pre>
  <p>and received:</p>
  <pre><code>Warning: invalid formula c(&quot;outcome1 ~ explanatoryvariable*interactionvariable1 | fe_variable |0|fe_variable | data = df&quot;, &quot;outcome2 ~ explanatoryvariable*interactionvariable2 | fe_variable |0|fe_variable | data = df&quot;)...: assignment is deprecatedError in class(ff) &lt;- &quot;formula&quot; : cannot set attribute on a symbol
  </code></pre>
  <p>As @allan suggested, <code>map</code> seems to be on the right track, but how can I fix these errors?</p>