untangle: Split an R script by functions

View source: R/untangle.R

untangleR Documentation

Split an R script by functions

Description

Split a R script with multiple functions into multiple single function R files.

Usage

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)

Details

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

Value

list of seperate functions

Author(s)

Jonathan Sidi

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

cat( readLines(file.path(test_dir,'yy.R')), sep = '\n')

cat( readLines(file.path(test_dir,'zz.R')), sep = '\n')

cat( readLines(file.path(tempdir(),'body.R')), sep = '\n')

unlink(test_dir, force = TRUE, recursive = TRUE)



sinew documentation built on March 31, 2022, 5:06 p.m.