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

fetch all namespaced calls from project #15

Open
moodymudskipper opened this issue Mar 11, 2022 · 2 comments
Open

fetch all namespaced calls from project #15

moodymudskipper opened this issue Mar 11, 2022 · 2 comments

Comments

@moodymudskipper
Copy link
Owner

This is useful for project diagnostics, esp if we're trying to build a package from a messy project

Something like this :

get_project_namespaces <- function() {
  all_scripts <- list.files(
    here::here(), # start at the project root
    pattern = "\\.[rR]$", # all R files
    full.names = TRUE, # return absolute paths
    recursive = TRUE # check in subfolder
  )
  code <- sapply(all_scripts, parse)
  namespaces <- list()
  collect_namespaced_calls <- function(call) {
    if(!is.call(call) && !is.expression(call)) {
      return()
    }
    if(rlang::is_call(call, "::")) {
      namespaces <<- c(namespaces, call[[2]])
      return()
    }
    lapply(as.list(call), collect_namespaced_calls)
    invisible()
  }
  lapply(code, collect_namespaced_calls)
  sort(as.character(unique(namespaces)))
}
get_project_namespaces()

should be generalized to ::: and have a flexible dir to start on

@moodymudskipper
Copy link
Owner Author

Should fetch calls to library, require, requireNamespace etc too, we might return a list with :

  • all package names (the most important one)
  • all pkg::fun calls
  • all library calls
  • etc

We might return this list silently and propose a usethis::use_package call if we're in a package, or otherwise a call like the following which the user might paste at the top of their main script:

required_pkgs <- c("assertthat", "bag.epi.dboard.etl", "bag.epi.io", "bag.epi.shapes", 
"data.table", "dtplyr", "httr", "openxlsx", "padr", "plyr")
for (pkg in required_pkgs) {
  requireNamespace(pkg)
}

@moodymudskipper
Copy link
Owner Author

We could use the "Find in Files" pane here too

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

1 participant