r - Copy script after executing -
is there possibility copy executed lines or script working directory?
so normal scenario is, have stand alone script need sourced within working directory , need. after few month, made update these scripts , love have snapshot script when executed source...
so file.copy(itself, '.')
or this.
i think you're looking for:
file.copy(sys.frame(1)$ofile, = file.path(dirname(sys.frame(1)$ofile), paste0(sys.date(), ".r")))
this take current file , copy new file, in same directory , name of currentdate.r, example 2015-07-14.r
if want copy working directory instead of original script directory, use
file.copy(sys.frame(1)$ofile, = file.path(getwd(), paste0(sys.date(), ".r")))
just note sys.frame(1)$ofile
works if saved script sourced, trying run in terminal fail. worth mentioning though might not best practice. perhaps looking version control system better.
explanation:
tbh might not best person explain (i copied idea somewhere , use sometimes), i'll try. in order have information script file r needs running file inside environment information, , when environment source
call contains ofile
data. use (1)
select next (source()
's) environment following global environment (which 0
). when you're running terminal, there's no frame/environment other global (that's error message), since no file being ran - commands sent straight terminal.
to illustrate that, can simple test:
> sys.frame(1) error in sys.frame(1) : not many frames on stack
but if call function:
> myf <- function() sys.frame(1) > myf() <environment: 0x0000000013ad7638>
our function's environment doesn't have in it, exists but, in case, not have oflile
:
> myf <- function() names(sys.frame(1)) > myf() character(0)
Comments
Post a Comment