Saving an open script with code when run

Hi all, I am looking for a way i can effectively auto save code each time I run it. I have been experimenting with:

documentSave(getActiveDocumentContext())

However, as of yet it does not seem to work. I would find it odd given the range of functionality in the rstudioapi package, you can not write a line of code to save the script. Any help here would be appreciated, thank you.

Hi @Zac.Muscat

Assuming you use RStudio, I would suggest you look at the documentSave() function, which you can find in the rstudioapi package.

I have been trying to use that, however, it seems to not be working.

Could we get specifics? The function works for me. You just need to make sure to put the command at the end of your code so that the script is saved when your code is done running.

Thank you for your help. I tried it as:

documentSave(getActiveDocumentContext()) and

documentSave()

I simply wrote it as that (after of course installing the rstudioapi package etc), and those lines of code were written at the base (end) of my code. I saved my script manually, and then I wrote some random words, ran the model (to see if it would save it), closed Rstudio, went to the file re-opened it and the random words were gone (implying it had not saved the file when it was run).

You are very welcome. Would it be okay to post your code here? It would help me troubleshoot it easier.

I hope that comes through ok.

# Saving a R Script 
#     Model Owner: Zac Muscat                

rm(list=ls(all=TRUE))
gc(verbose = FALSE, full = TRUE)

if(!require(rstudioapi)){#__________
  install.packages("rstudioapi")
}#_________________________________
library(rstudioapi)

My_Directory <- dirname(rstudioapi::getActiveDocumentContext()$path) # This element of the package works fine           
setwd(My_Directory)

# I want this script to auto save every time it is run

# TEST TEST TEST

#............
documentSave(getActiveDocumentContext())
#documentSave()

So i have tried it both away as you can see.

Kinda unrelated but I would advise against using setwd() in your code in favor of using RStudio projects. You can get more information about it here: RStudio projects.

Now, if you want to post code here, you could copy and paste it in code blocks. That would help readers understand it more.

Lastly (back to the main topic now), the document will not be saved if you do not run the code that saves it. If your model takes some time to run and you would like to save your document at the end of the code execution, you could put everything in a function. Here is an example:

library(rstudioapi)

# Create a function

run_my_model <- function(model_param = 5){
  # My model takes 5 seconds to run
  Sys.sleep((model_param))
  
  # Then it saves my script when it's done
  rstudioapi::documentSave()
}

# Run the model

run_my_model()

Thanks for that, the code block thing makes it look so much cleaner.

Right, okay thank you very much for your help. So your code worked fine, as expected. So I know there was something wrong in what I was doing. For a bit of context, I work for the Government and this is a large Monte Carlo Simulation which generates lots of tables and pretty pictures (graphics). The problem here is when the code runs and those tables are generated View() is executed and rstudioapi::documentSave() was not working because it was no longer the tab displayed. At lest that is my theory because when I use

rstudioapi::documentSaveAll() 

It works fine. Of course it will save all open scripts but, that is fine with me.

So this comes comes down to my programming ignorance as an economist. So I really do appreciate your time and effort today. Thank you.

I wouldn't say you suffer from ignorance in programming; however, the workflow you describe has a tiny bit of a flaw. If you are running a MC simulation that generates several datasets (in a loop or using using functional programming), then you do not want the View() function to be a part of that loop. It's better to let the loop do what it does (i.e. create the datasets and the graphics - and save them on disk maybe?) and then View() the final results on your own (interactively, in the console for example).

Also, you are correct. The rstudioapi::documentSave() function will save the document, which is currently open. So View() will definitely be an issue here.

You might also consider turning on RStudio's auto-save: go to Options -> Code -> Saving -> and check "Automatically save when editor loses focus". Then when you run code and the R console gets focus, the code will be automatically saved.

That is pretty much all I really wanted on reflection - thank you for that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.