title: How to convert character string to executable code in R?
body: <p>I have a dataframe e.g.</p>
  <pre><code>df_reprex &lt;- data.frame(id = rep(paste0(&quot;S&quot;,round(runif(100, 1000000, 9999999),0)), each=10),
                          date = rep(seq.Date(today(), by=-7, length.out = 10), 100),
                          var1 = runif(1000, 10, 20),
                          var2 = runif(1000, 20, 50),
                          var3 = runif(1000, 2, 5),
                          var250 = runif(1000, 100, 200),
                          var1_baseline = rep(runif(100, 5, 10), each=10),
                          var2_baseline = rep(runif(100, 50, 80), each=10),
                          var3_baseline = rep(runif(100, 1, 3), each=10),
                          var250_baseline = rep(runif(100, 20, 70), each=10))
  </code></pre>
  <p>I want to write a function containing a for loop that for each row in the dataframe will subtract every &quot;_baseline&quot; column from the non-baseline column with the same name.</p>
  <p>I have created a script that automatically creates a character string containing the code I would like to run:</p>
  <pre><code>df &lt;- df_reprex
  
  # get only numeric columns
  df_num &lt;- df %&gt;% dplyr::select_if(., is.numeric)
  
  # create a version with no baselines
  df_nobaselines &lt;- df_num %&gt;% select(-contains(&quot;baseline&quot;))
  
  #extract names of non-baseline columns
  numeric_cols &lt;- names(df_nobaselines)
  
  #initialise empty string  
  mutatestring &lt;- &quot;&quot;
  
  #write loop to fill in string:
  for (colname in numeric_cols) {
    
   mutatestring &lt;- paste(mutatestring, &quot;,&quot;, paste0(colname, &quot;_change&quot;), &quot;=&quot;, colname, &quot;-&quot;, paste0(colname, &quot;_baseline&quot;)) 
   
   # df_num &lt;- df_num %&gt;%
   #   mutate(paste0(col, &quot;_change&quot;) = col - paste0(col, &quot;_baseline&quot;))
    
  }
  
  mutatestring &lt;- substr(mutatestring, 4, 9999999) # remove stuff at start (I know it's inefficient)
  mutatestring2 &lt;- paste(&quot;df %&gt;% mutate(&quot;, mutatestring, &quot;)&quot;) # add mutate call
  </code></pre>
  <p>but when I try to call &quot;mutatestring2&quot; it just prints the character string e.g.:</p>
  <pre><code>[1] &quot;df %&gt;% mutate( var1_change = var1 - var1_baseline , var2_change = var2 - var2_baseline , var3_change = var3 - var3_baseline , var250_change = var250 - var250_baseline )&quot;
  </code></pre>
  <p>I thought that this part would be relatively easy and I'm sure I've missed something obvious, but I just can't get the text inside that string to run!</p>
  <p>I've tried various slightly ridiculous methods but none of them return the desired output (i.e. the result returned by the character string if it was entered into the console as a command):</p>
  <pre><code>call(mutatestring2)
  eval(mutatestring2)
  parse(mutatestring2)
  str2lang(mutatestring2)
  mget(mutatestring2)
  
  diff_func &lt;- function() {mutatestring2}
  diff_func1 &lt;- function() {
    a &lt;-mutatestring2
    return(a)}
  diff_func2 &lt;- function() {str2lang(mutatestring2)}
  diff_func3 &lt;- function() {eval(mutatestring2)}
  diff_func4 &lt;- function() {parse(mutatestring2)}
  diff_func5 &lt;- function() {call(mutatestring2)}
  
  diff_func()
  diff_func1()
  diff_func2()
  diff_func3()
  diff_func4()
  diff_func5()
  </code></pre>
  <p>I'm sure there must be a very straightforward way of doing this, but I just can't work it out!</p>
  <p>How do I convert a character string to something that I can run or pass to a magrittr pipe?</p>