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)
)

Arguments

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)

Value

list of seperate functions

Details

Functions that are objects in lists are treated as objects and will stay in body.R .

Examples

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"
cat( readLines(file.path(test_dir,'yy.R')), sep = '\n')
#> #some comment #> yy <- function(a=4){ #> head(runif(10),a) #> # a comment #> }
cat( readLines(file.path(test_dir,'zz.R')), sep = '\n')
#> #another comment #> zz <- function(v=10,a=3){ #> head(runif(v),pmin(a,v)) #> }
cat( readLines(file.path(tempdir(),'body.R')), sep = '\n')
#> #> v <- 20 #> #> zz(v) #>
unlink(test_dir, force = TRUE, recursive = TRUE)