Created
June 28, 2022 11:48
-
-
Save harveyl888/1f043414a4102ed5f04e8ed22b73c939 to your computer and use it in GitHub Desktop.
extract parameters from roxygen metadata tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' extract parameters from roxygen tags | |
#' | |
#' extract parameters from roxygen tags | |
#' | |
#' @param n Namespace | |
#' @param f Function | |
#' | |
#' @importFrom tools Rd_db | |
#' @importFrom jsonlite fromJSON | |
#' | |
#' @export | |
#' | |
get_params <- function(n, f) { | |
db <- tools::Rd_db(n) | |
fn_rd <- db[[paste0(f, ".Rd")]] | |
## get list of attributes | |
fn_attributes <- lapply(fn_rd, attributes) | |
## get sections | |
fn_sections <- which( | |
sapply(fn_attributes, function(x) { | |
x$Rd_tag == "\\section" | |
}) | |
) | |
## get param section | |
fn_params <- which( | |
sapply(fn_rd[fn_sections], function(x) { | |
x[[1]] == "Metadata" | |
}) | |
) | |
if (length(fn_params) > 0) { | |
input_tags <- fn_rd[[fn_sections[fn_params]]] | |
param_tag <- input_tags[[2]][[2]] | |
return(jsonlite::fromJSON(as.character(param_tag), simplifyVector = FALSE)) | |
} else { | |
return(NULL) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment