Reputation: 1313
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 saveRDS
or writeLines
for every single parameter. Since the parameters are usually text values that I manually enter at the top of the script, e.g. search_term = "transcription"
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).
A little more searching revealed rstudioapi::getSourceEditorContext()$path
, which gives me the path to the file for the source code. I can then use file.copy
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?
Thanks.
Upvotes: 1
Views: 712
Reputation: 31452
Provided you are working in RStudio, you can use rstudioapi::documentSave
. Here's an example
library(rstudioapi)
version = 1
documentSave(getActiveDocumentContext())
Upvotes: 2