Skip to content

Instantly share code, notes, and snippets.

@yjunechoe
Created September 4, 2024 14:09
Show Gist options
  • Save yjunechoe/021e164b15c7f1c50372985dcd09c90b to your computer and use it in GitHub Desktop.
Save yjunechoe/021e164b15c7f1c50372985dcd09c90b to your computer and use it in GitHub Desktop.
# Top-level
thefuck <- function(e = rlang::last_error()) {
stopifnot(rlang::is_call_simple(e$call))
if (grep("^unused argument", e$message)) {
fix_unused_arg(e)
}
# ... more cases
}
# Internal methods
fix_unused_arg <- function(e) {
unused <- gsub(".*\\((.*) =.*", "\\1", e$message)
fn <- get(e$call[[1]])
fn_args <- formalArgs(fn)
closest <- fn_args[which.min(adist(fn_args, unused))]
new <- e$call
names(new)[names(new) == unused] <- closest
if (rstudioapi::isAvailable()) {
rstudioapi::sendToConsole(deparse1(new))
} else {
message(new)
}
}
# Demo in RStudio:
rlang::global_entrace()
f <- tempfile()
write.table(data.frame(x=1), f, colnames = TRUE)
thefuck()
read.table(f)
@yjunechoe
Copy link
Author

yjunechoe commented Sep 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment