title: Is there a way to save R source code to a file programmatically?
body: <p>I'm sure this question has been answered but I'm googling the wrong terms. I have many R scripts that allow me to manually input various parameters, run the code, and save the results to a unique directory. Every time I run the code I input different parameters. I want to record the parameters along with the results, but I don't want to have to run <code>saveRDS</code> or <code>writeLines</code> for every single parameter. Since the parameters are usually text values that I manually enter at the top of the script, e.g. <code>search_term = "transcription"</code> just saving the text of the source code into the unique directory would do the job (this would also allow me to go back and see any little tweaks in the source code that broke/fixed the code). </p>
  
  <p>A little more searching revealed <code>rstudioapi::getSourceEditorContext()$path</code>, which gives me the path to the file for the source code. I can then use <code>file.copy</code> to copy the source to the unique directory. But I will have to remember to save the source before running it, or it will be the old version that is saved. So I guess the second part of the question is, is there a way to save a script programmatically from within the same script?</p>
  
  <p>Thanks.</p>