Skip to content

Commit 639fc68

Browse files
committed
cleaned up shim system
1 parent b8b0d75 commit 639fc68

11 files changed

Lines changed: 120 additions & 113 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: fertile
22
Title: Creating Optimal Conditions for Reproducibility
3-
Version: 1.1.9000
3+
Version: 1.1.9001
44
Authors@R: c(
55
person("Benjamin S.", "Baumer", email = "ben.baumer@gmail.com", role = c("aut"),
66
comment = c(ORCID = "0000-0002-3279-0516")),

NAMESPACE

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method(print,fertile)
44
S3method(print,fertile_check)
5+
export(active_shims)
56
export(add_all_possible_shims)
67
export(add_shim)
78
export(char_or_sym)
@@ -16,9 +17,7 @@ export(check_path_is_portable_shim)
1617
export(check_path_safe)
1718
export(check_path_shim)
1819
export(danger)
19-
export(disable_added_shims)
20-
export(edit_added_shims)
21-
export(enable_added_shims)
20+
export(edit_shims)
2221
export(find_all_shimmable_functions)
2322
export(find_pkg_shimmable_functions)
2423
export(find_pkgs_rec)
@@ -55,12 +54,14 @@ export(is_text_file)
5554
export(library)
5655
export(list_checks)
5756
export(load)
57+
export(load_shims)
5858
export(log_clear)
5959
export(log_push)
6060
export(log_report)
6161
export(log_touch)
6262
export(make_check)
6363
export(path_log)
64+
export(path_shims)
6465
export(print_one_check)
6566
export(proj_analyze)
6667
export(proj_analyze_files)
@@ -122,6 +123,7 @@ export(source)
122123
export(takes_path_arg)
123124
export(tbl)
124125
export(to_execute)
126+
export(unload_shims)
125127
export(write.csv)
126128
export(write_csv)
127129
import(fs)

R/fertile.R

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -955,61 +955,53 @@ add_shim <- function(func, package = "", path_arg = "") {
955955
func_lines <- get_shim_code(func, package, path_arg)
956956

957957
# Write code to .Rprofile
958-
path_shims <- file.path(Sys.getenv("HOME"), "fertile_shims.R")
958+
shims <- path_shims()
959959

960-
cat("", file = path_shims, sep = "\n", append = TRUE)
960+
cat("", file = shims, sep = "\n", append = TRUE)
961961

962962
for (line in func_lines) {
963-
cat(line, file = path_shims, sep = "\n", append = TRUE)
963+
cat(line, file = shims, sep = "\n", append = TRUE)
964964
}
965965

966966
# Execute file to make sure new shim is in environment
967-
base::source(path_shims)
967+
base::source(shims)
968968

969969
msg("Shim created")
970970
}
971971

972972
#' View/edit list of user created shims.
973+
#' @rdname add_shim
973974
#' @export
974975

975-
edit_added_shims <- function() {
976-
path_shims <- read_shims()
976+
edit_shims <- function() {
977+
shims <- path_shims()
977978

978979
msg("Viewing list of user-added shims")
979980

980981
# Open Rprofile in editing window
981-
utils::file.edit(path_shims)
982+
utils::file.edit(shims)
982983
}
983984

984985
#' Remove all user-added shims from the global environment
986+
#' @rdname add_shim
985987
#' @export
986988

987-
disable_added_shims <- function() {
988-
path_shims <- read_shims()
989-
990-
# Get names of functions from inside the shims file
991-
file_parsed <- parse(path_shims)
992-
functions <- Filter(is_function, file_parsed)
993-
function_names <- unlist(Map(function_name, functions))
994-
989+
unload_shims <- function() {
995990
# Remove them from the global environment
996-
rm(list = function_names, envir = .GlobalEnv)
991+
rm(list = active_shims(), envir = .GlobalEnv)
997992
}
998993

999-
1000-
1001-
1002-
#' Remove all user-added shims from the global environment
994+
#' @rdname add_shim
1003995
#' @export
1004996

1005-
enable_added_shims <- function() {
1006-
path_shims <- read_shims()
1007-
1008-
base::source(path_shims)
997+
load_shims <- function() {
998+
shims <- path_shims()
999+
base::source(shims)
10091000
}
10101001

10111002

10121003
#' Write shims for all possible shimmable functions
1004+
#' @rdname add_shim
10131005
#' @export
10141006

10151007
add_all_possible_shims <- function() {
@@ -1075,25 +1067,9 @@ add_all_possible_shims <- function() {
10751067
)
10761068

10771069
# List of functions already shimmed by the user
1078-
1079-
path_shims <- read_shims()
1080-
1081-
# Get names of functions from inside the shims file
1082-
file_code <- readLines(path_shims)
1083-
indices <- grep("fertile::log_push", file_code)
1084-
1085-
lines <- file_code[indices]
1086-
1087-
1088-
user_shims <- c()
1089-
for (line in lines) {
1090-
indices_apostrophe <- gregexpr("'", line)[[1]][1:2]
1091-
user_shim <- substr(line, indices_apostrophe[1] + 1, indices_apostrophe[2] - 1)
1092-
user_shims <- user_shims %>% append(user_shim)
1093-
}
1070+
user_shims <- read_shims()
10941071

10951072
# Only take ones that aren't shimmed by fertile or the user
1096-
10971073
combined_fertile_shims <- unique(c(fertile_shims, user_shims))
10981074

10991075
unwritten_shims <- shims_with_pkgs[!(shims_with_pkgs %in% combined_fertile_shims)]

R/utils.R

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#' @param pkgname a character string giving the name of the package
44
.onAttach <- function(libname, pkgname) {
55
if (Sys.getenv("IN_TESTTHAT") != TRUE & fs::dir_exists(Sys.getenv("HOME"))) {
6-
enable_added_shims()
6+
load_shims()
77
}
88
}
99

1010
#' Remove shims from environment when fertile is detached
1111
#' @param libpath a character string giving the complete path to the package
1212
.onDetach <- function(libpath) {
1313
if (Sys.getenv("IN_TESTTHAT") != TRUE) {
14-
disable_added_shims()
14+
unload_shims()
1515
}
1616
}
1717

@@ -692,19 +692,51 @@ is_assign <- function(expr) {
692692

693693
#' Check that shims file exists and return path
694694
#' @export
695+
#' @rdname add_shim
695696
#' @keywords internal
696697

697-
read_shims <- function() {
698+
path_shims <- function() {
698699

699700
# Get path to shim file
700-
path_shims <- fs::path(Sys.getenv("HOME"), "fertile_shims.R")
701+
x <- fs::path(Sys.getenv("HOME"), ".fertile_shims.R")
701702

702703
# If file exists, return the path
703704
# Otherwise create the file then return the path
704-
if (fs::file_exists(path_shims)) {
705-
return(path_shims)
705+
if (fs::file_exists(x)) {
706+
return(x)
706707
} else {
707-
fs:file_create(path_shims)
708-
return(path_shims)
708+
fs::file_create(x)
709+
return(x)
709710
}
710711
}
712+
713+
#' @rdname add_shim
714+
#' @export
715+
716+
read_shims <- function() {
717+
x <- path_shims()
718+
719+
# Get names of functions from inside the shims file
720+
file_code <- readLines(x)
721+
722+
file_code %>%
723+
stringr::str_subset("fertile::log_push") %>%
724+
stringr::str_extract("'.+::.+'") %>%
725+
stringr::str_remove_all("'")
726+
}
727+
728+
#' @rdname add_shim
729+
#' @export
730+
731+
active_shims <- function() {
732+
shims <- read_shims() %>%
733+
stringr::str_extract("::.+$") %>%
734+
stringr::str_remove_all("::")
735+
736+
# doesn't work -- need to fix
737+
# objects <- ls()
738+
# funs <- objects[purrr::map_lgl(objects, is.function)]
739+
740+
# intersect(funs, shims)
741+
shims
742+
}

man/add_all_possible_shims.Rd

Lines changed: 0 additions & 11 deletions
This file was deleted.

man/add_shim.Rd

Lines changed: 35 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/disable_added_shims.Rd

Lines changed: 0 additions & 11 deletions
This file was deleted.

man/edit_added_shims.Rd

Lines changed: 0 additions & 11 deletions
This file was deleted.

man/enable_added_shims.Rd

Lines changed: 0 additions & 11 deletions
This file was deleted.

man/read_shims.Rd

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)