Skip to content

Commit

Permalink
add R/typetrace-package.R for #76
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Aug 26, 2022
1 parent bfea746 commit f783c8d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: autotest
Title: Automatic Package Testing
Version: 0.0.2.195
Version: 0.0.2.196
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265")),
Expand All @@ -24,6 +24,7 @@ Imports:
rlang,
testthat,
tibble,
typetracer,
withr,
yaml
Suggests:
Expand All @@ -38,6 +39,8 @@ Suggests:
usethis
VignetteBuilder:
knitr
Remotes:
mpadge/typetracer
Encoding: UTF-8
Language: en-GB
LazyData: true
Expand Down
68 changes: 68 additions & 0 deletions R/typetrace-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Trace a package with 'typetracer'

get_typetrace_dir <- function () {

td <- getOption ("typetracedir")
if (is.null (td)) {
td <- tempdir ()
}
return (td)
}

autotest_trace_package <- function (package) {

Sys.setenv ("TYPETRACER_LEAVE_TRACES" = "true")

package <- dot_to_package (package)
pkg_name <- preload_package (package)
if (pkg_name != package) {
if (!dir.exists (package)) {
stop ("'package' should be a local directory.")
}
traces <- typetracer::trace_package (pkg_dir = package)
} else {
traces <- typetracer::trace_package (package = package)
}

trace_files <- list.files (
get_typetrace_dir (),
pattern = "^typetrace\\_.*\\.Rds$",
full.names = TRUE
)

Sys.unsetenv ("TYPETRACER_LEAVE_TRACES")

return (trace_files)
}

#' Get all (unique) parameter names from all traced functions.
#'
#' @param traces Result of 'typetracer::trace_package()' call.
#' @return Reduced version of 'traces' with only unique parameter names.
#' @noRd
get_unique_fn_pars <- function (traces) {

fn_pars <- unique (traces [, c ("fn_name", "par_name")])

par_types <- lapply (seq (nrow (fn_pars)), function (i) {
index <- which (traces$fn_name == fn_pars$fn_name [i] &
traces$par_name == fn_pars$par_name [i])
onecol <- function (traces, index, what = "classes") {
res <- traces [[what]] [index]
if (is.list (res)) {
res <- do.call (c, res)
}
res <- unique (res)
paste0 (res [which (!res == "NULL")], collapse = ", ")
}
data.frame (
class = onecol (traces, index, "class"),
typeof = onecol (traces, index, "typeof"),
mode = onecol (traces, index, "mode"),
storage_mode = onecol (traces, index, "storage_mode"),
length = onecol (traces, index, "length")
)
})

return (cbind (fn_pars, do.call (rbind, par_types)))
}
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/autotest",
"issueTracker": "https://github.com/ropensci-review-tools/autotest/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.0.2.195",
"version": "0.0.2.196",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down

0 comments on commit f783c8d

Please sign in to comment.