-
Notifications
You must be signed in to change notification settings - Fork 301
document data #2049
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
base: main
Are you sure you want to change the base?
document data #2049
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#' Document Data | ||
#' | ||
#' @description | ||
#' `use_document_data()` creates an .R file containing detailed documentation | ||
#' of a provided data set. It automatically generates a roxygen template including | ||
#' variables' names, data types, row and column counts, and placeholders for further description. | ||
#' | ||
#' @param .data A data set loaded in the R environment. The function extracts its | ||
#' name, type, class, dimensions, and column names for documentation. | ||
#' @param path The directory where the documentation file will be saved. Defaults to the current working directory. | ||
#' @param overwrite Logical, whether to overwrite an existing file with the same name. Defaults to `FALSE`. | ||
#' @param description A character string for the data set description. Defaults to "Describe your data set here". | ||
#' @param source A character string indicating the source of the data set. Defaults to "Add your source here". | ||
#' | ||
#' @return Invisibly returns the file path where the documentation was saved. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' the_data <- as.data.frame(datasets::Titanic) | ||
#' use_document_data(the_data) | ||
#' } | ||
use_document_data <- function(.data, path = ".", overwrite = FALSE, | ||
description = "Describe your dataset here", | ||
source = "Add your source here") { | ||
dataset_name <- rlang::as_name(rlang::enquo(.data)) | ||
|
||
if (!inherits(.data, "data.frame")) { | ||
ui_abort("The provided object is not a data frame.") | ||
} | ||
|
||
file_path <- fs::path(path, paste0(dataset_name, ".R")) | ||
|
||
if (fs::file_exists(file_path) && !overwrite) { | ||
ui_abort(paste0("File '", file_path, "' already exists. Use `overwrite = TRUE` to overwrite it.")) | ||
} | ||
|
||
data_description <- create_data_description(.data, dataset_name, description, source) | ||
cat(data_description, file = file_path) | ||
|
||
ui_bullets(c( | ||
"*" = paste0("Documentation file created: ", pth(file_path), "."), | ||
"_" = "Finish writing the data documentation in the generated file." | ||
)) | ||
|
||
invisible(file_path) | ||
} | ||
|
||
#' Create Data Description | ||
#' | ||
#' @description | ||
#' Generates a description of a data set, including information about | ||
#' its type, class, dimensions (rows and columns), and a placeholder for each | ||
#' variable's description. This description is formatted as a string that could | ||
#' be used directly in R documentation files or other descriptive materials. | ||
#' | ||
#' @param dataset A data frame for which the description is to be generated. | ||
#' @param name The name of the data set, which will be used in the title and usage | ||
#' sections of the generated description. | ||
#' @param description A character string for the data set description. | ||
#' @param source A character string indicating the source of the data set. | ||
#' | ||
#' @return A character string containing the structured documentation template | ||
#' for the data set. This includes the data set's basic information and | ||
#' placeholders for detailed descriptions of each variable. | ||
#' | ||
#' @keywords internal | ||
#' | ||
create_data_description <- function(dataset, name, description, source) { | ||
data_info <- generate_data_info(dataset) | ||
variable_descriptions <- generate_variable_descriptions(dataset) | ||
|
||
description_template <- paste0( | ||
"#' @title ", name, "\n", | ||
"#' @description ", description, "\n", | ||
"#' @docType data\n", | ||
"#' @usage data(", name, ")\n", | ||
"#' @format ", data_info, | ||
"#' \\itemize{\n", | ||
"#' ", paste(variable_descriptions, collapse = "\n#' "), "\n#' }\n", | ||
"#' @source ", source, "\n", | ||
"'", name, "'" | ||
) | ||
|
||
return(description_template) | ||
} | ||
|
||
generate_data_info <- function(dataset) { | ||
paste0( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Within usethis, newer functions use |
||
"A ", typeof(dataset), " [", class(dataset), "] with ", | ||
nrow(dataset), " rows and ", length(names(dataset)), " variables:\n" | ||
) | ||
} | ||
|
||
generate_variable_descriptions <- function(dataset) { | ||
purrr::map_chr(names(dataset), function(var) { | ||
paste0("\\item{", var, "} {", class(dataset[[var]]), ": Type label here}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here for |
||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In time, this could be done using template file and the
use_template()
function. I would not worry about this now, but it would not surprise me if this were used, once things become more settled.