Split a R script with multiple functions into multiple single function R files.
untangle( file = "", text = NULL, dir.out = "", keep.body = TRUE, dir.body = dirname(dir.out) )
file | character, path to R file, Default: '' |
---|---|
text | character, vector of R commands, Default: NULL |
dir.out | character, path to save new R files, Default: NULL |
keep.body | boolean, if TRUE all non-functions will be saved to body.R in the parent directory of dir.out, Default: TRUE |
dir.body | character, path to save body.R files, Default: dirname(dir.out) |
list of seperate functions
Functions that are objects in lists are treated as objects and will stay in body.R .
test_dir <- file.path(tempdir(),'sinew_test') dir.create(test_dir) txt <- "#some comment yy <- function(a=4){ head(runif(10),a) # a comment } v <- 20 #another comment zz <- function(v=10,a=3){ head(runif(v),pmin(a,v)) } zz(v) " untangle(text = txt,dir.out = test_dir) list.files(tempdir(), recursive = TRUE, pattern = '.R$')#> [1] "body.R" "sinew_test/yy.R" "sinew_test/zz.R"#> #some comment #> yy <- function(a=4){ #> head(runif(10),a) #> # a comment #> }#> #another comment #> zz <- function(v=10,a=3){ #> head(runif(v),pmin(a,v)) #> }#> #> v <- 20 #> #> zz(v) #>