Skip to content

Commit

Permalink
Move functions from main.R to scripts per function
Browse files Browse the repository at this point in the history
  • Loading branch information
hsonne committed Aug 11, 2023
1 parent ae555bf commit 42c1ea6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 75 deletions.
10 changes: 10 additions & 0 deletions R/filter_scripts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# filter_scripts ---------------------------------------------------------------
#' @importFrom kwb.utils matchesCriteria removeEmptyColumns
filter_scripts <- function(scriptInfo, fun.min = 5, epf.min = 10)
{
criteria <- c(paste("fun >=", fun.min), paste("epf >=", epf.min))

scriptInfo <- scriptInfo[kwb.utils::matchesCriteria(scriptInfo, criteria), ]

kwb.utils::removeEmptyColumns(scriptInfo)
}
39 changes: 39 additions & 0 deletions R/get_full_function_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# get_full_function_info -------------------------------------------------------

#' Get information on function definitions in parsed R scripts
#'
#' @param trees list of R script parse trees as provided by
#' \code{\link{parse_scripts}}
#' @importFrom kwb.utils moveColumnsToFront rbindAll
#' @export
#' @seealso \code{\link{parse_scripts}}
get_full_function_info <- function(trees)
{
function_info <- trees %>%
lapply(function(tree) {
tree %>%
get_functions() %>%
lapply(FUN = get_function_info) %>%
kwb.utils::rbindAll()
}) %>%
kwb.utils::rbindAll(nameColumn = "script")

merge(
x = function_info,
y = multi_defined_functions(function_info),
by = "functionName"
) %>%
kwb.utils::moveColumnsToFront(c("script", "functionName", "n.def"))
}

# multi_defined_functions ------------------------------------------------------
multi_defined_functions <- function(functionInfo)
{
count <- stats::aggregate(
n.def ~ functionName,
cbind(n.def = seq_len(nrow(functionInfo)), functionInfo),
length
)

count[order(count$n, decreasing = TRUE), ]
}
12 changes: 12 additions & 0 deletions R/merge_function_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# merge_function_info ----------------------------------------------------------
merge_function_info <- function(scriptInfo, functionInfo)
{
funExpressions <- expressions_per_function(functionInfo)

merge(
scriptInfo,
funExpressions[, c("script", "epf")],
by = "script",
all.x = TRUE
)
}
70 changes: 3 additions & 67 deletions R/main.R → R/parse_scripts.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# parse_scripts ----------------------------------------------------------------

#' Parse all given R scripts into a tree structure
#'
#' @param root root directory to which the relative paths given in
Expand Down Expand Up @@ -56,76 +57,11 @@ parse_scripts <- function
content <- catAndRun(
paste("Reading", file), dbg = dbg, readLines(file, warn = FALSE)
)

expressions <- try(parse(text = content))

structure(expressions, n.lines = length(content))
})

stats::setNames(trees, scripts)
}

# get_full_function_info -------------------------------------------------------
#'
#' Get information on function definitions in parsed R scripts
#'
#' @param trees list of R script parse trees as provided by
#' \code{\link{parse_scripts}}
#'
#' @importFrom kwb.utils rbindAll
#' @importFrom kwb.utils moveColumnsToFront
#'
#' @export
#'
#' @seealso \code{\link{parse_scripts}}
get_full_function_info <- function(trees)
{
infos <- lapply(trees, function(tree) {
rbindAll(lapply(get_functions(tree), get_function_info))
})

functionInfo <- rbindAll(infos, nameColumn = "script")

count <- multi_defined_functions(functionInfo)

functionInfo <- merge(functionInfo, count, by = "functionName")

moveColumnsToFront(functionInfo, c("script", "functionName", "n.def"))
}

# multi_defined_functions ------------------------------------------------------
multi_defined_functions <- function(functionInfo)
{
count <- aggregate(
n.def ~ functionName,
cbind(n.def = seq_len(nrow(functionInfo)), functionInfo),
length
)

count[order(count$n, decreasing = TRUE), ]
}

# merge_function_info ----------------------------------------------------------
merge_function_info <- function(scriptInfo, functionInfo)
{
funExpressions <- expressions_per_function(functionInfo)

merge(
scriptInfo,
funExpressions[, c("script", "epf")],
by = "script",
all.x = TRUE
)
}

# filter_scripts ---------------------------------------------------------------
#' @importFrom kwb.utils matchesCriteria
#' @importFrom kwb.utils removeEmptyColumns
filter_scripts <- function(scriptInfo, fun.min = 5, epf.min = 10)
{
criteria <- c(paste("fun >=", fun.min), paste("epf >=", epf.min))

scriptInfo <- scriptInfo[matchesCriteria(scriptInfo, criteria), ]

removeEmptyColumns(scriptInfo)
}
10 changes: 4 additions & 6 deletions inst/extdata/cleanCodeBasics.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
config <- list(checks = list(
seq_1_n = list(
check = function(x) {
if (!is.call(x)) return(FALSE)
#str(as.list(x))
#substr(deparse(x)[1L], 1L, 2L) == "1:"
identical(x[[1]], as.name(":")) &&
identical(x[[2]], 1) &&
kwb.code:::is_colon_seq_1_to_any(x) &&
(is.name(x[[3]]) || is.call(x[[3]]))
},
report = function(x) {
Expand Down Expand Up @@ -66,11 +62,13 @@ config <- list(checks = list(
if (FALSE)
{
#files <- dir_r_files("R")
files <- dir_r_files("~/github-repos/K/kwb.misa")
files <- dir_r_files("C:/development/github-repos/K/kwb.misa")
files <- dir_r_files("~/R-Development/RScripts")

cat("\n ")

files <- files[1]

# Apply the configuration for all files
for (i in seq_along(files)) {
file <- files[i]
Expand Down
2 changes: 1 addition & 1 deletion man/get_full_function_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/parse_scripts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42c1ea6

Please sign in to comment.