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?

1
  • 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 Commented Jul 24, 2015 at 18:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

In addition, lots of cool options / related functions ?txtStart

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.