4

I'm writing an R script whose contents can change from time to time. It would be really helpful if I could insert a command that would copy the current contents of the script to a file, so I can go back later and see exactly what commands I executed during that run of the code.

How can I do this?

Jessica
  • 2,335
  • 2
  • 23
  • 36
  • 2
    If you don't want to use a version control system like git, you could stick something like this somewhere in your script `system(sprintf("cp my_script.R my_script_%s.R", format(Sys.time(), '%Y_%m_%d_%H_%m')))` make sure you save the script and then `source` it. this line will make a copy of your script and rename it with the current date/time – rawr Jul 24 '15 at 18:08

1 Answers1

2

You can do this with the teaching demos package:

    install.packages("TeachingDemos")
    library(TeachingDemos)

    #Will write to a file in the working directory
    txtStart("captureCode.txt")

    #This comment will not appear in the file, all commands and output will
    Sys.Date() 

    #This command ends writing to the file
    txtStop()

Source

Community
  • 1
  • 1
user3055034
  • 593
  • 1
  • 5
  • 14