Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locally defined functions not found #23

Open
joel-beck opened this issue Dec 25, 2022 · 2 comments
Open

Locally defined functions not found #23

joel-beck opened this issue Dec 25, 2022 · 2 comments

Comments

@joel-beck
Copy link

Many of my shortcuts have a nearly identical structure and share large parts of their code. In order to reduce duplication I would like to extract shared code into helper functions which are not shortcuts themselves, e.g.

# .shrtcts.R

# helper function reused across the file
print_message <- function(message) {
  cat(message)
}


#' Print message 'foo'
#'
#' @description
#'   Print 'foo'
#' @interactive
print_foo <- function() {
  print_message("foo")
}

#' Print message 'bar'
#'
#' @description
#'   Print 'bar'
#' @interactive
print_bar <- function() {
  print_message("bar")
}

Executing print_foo, this approach leads to the error message Error in print_message("foo") : could not find function "print_message".
Is there a way to modularize the code for shortcuts? I could not find any remarks about this in the documentation.

Thank you very much for this great package!

@gadenbuie
Copy link
Owner

This is an interesting idea, thanks for the issue. Right now, shrtcts doesn't source the file containing the shortcuts, instead it parses the file and then runs a labelled function. I've been thinking about ways to expose the function names (which are currently ignored) and in doing so I might need to source the entire script.

I'll have to think about it, but I currently think it's an advantage that the shortcuts R script isn't executed when running a single shortcut. This helps ensure that there aren't any accidental side-effects beyond what's required to run the shortcut function.

One workaround is to put your helper functions somewhere on your computer, e.g. next to the .shortcuts.R file, and then source it from within any shortcut functions that require the helpers.

@joel-beck
Copy link
Author

Thanks for considering this idea. Your concern about undesired side-effects through dependencies within the file is very valid. Thus, sourcing a second .shrtcts_helpers.R file wherever needed is a great workaround for now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants