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

document data #2049

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export(use_dev_package)
export(use_dev_version)
export(use_devtools)
export(use_directory)
export(use_document_data)
export(use_git)
export(use_git_config)
export(use_git_hook)
Expand Down
99 changes: 99 additions & 0 deletions R/use_document_data.R
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))

Check warning on line 26 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L26

Added line #L26 was not covered by tests

if (!inherits(.data, "data.frame")) {
ui_abort("The provided object is not a data frame.")

Check warning on line 29 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L28-L29

Added lines #L28 - L29 were not covered by tests
}

file_path <- fs::path(path, paste0(dataset_name, ".R"))

Check warning on line 32 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L32

Added line #L32 was not covered by tests

if (fs::file_exists(file_path) && !overwrite) {
ui_abort(paste0("File '", file_path, "' already exists. Use `overwrite = TRUE` to overwrite it."))

Check warning on line 35 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L34-L35

Added lines #L34 - L35 were not covered by tests
}

data_description <- create_data_description(.data, dataset_name, description, source)
cat(data_description, file = file_path)

Check warning on line 39 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L38-L39

Added lines #L38 - L39 were not covered by tests

ui_bullets(c(
"*" = paste0("Documentation file created: ", pth(file_path), "."),
"_" = "Finish writing the data documentation in the generated file."
))

Check warning on line 44 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L41-L44

Added lines #L41 - L44 were not covered by tests

invisible(file_path)

Check warning on line 46 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L46

Added line #L46 was not covered by tests
}

#' 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)

Check warning on line 71 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L70-L71

Added lines #L70 - L71 were not covered by tests

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, "'"
)

Check warning on line 83 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L73-L83

Added lines #L73 - L83 were not covered by tests

return(description_template)

Check warning on line 85 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L85

Added line #L85 was not covered by tests
}

generate_data_info <- function(dataset) {
paste0(
"A ", typeof(dataset), " [", class(dataset), "] with ",
nrow(dataset), " rows and ", length(names(dataset)), " variables:\n"
)

Check warning on line 92 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L89-L92

Added lines #L89 - L92 were not covered by tests
}

generate_variable_descriptions <- function(dataset) {
purrr::map_chr(names(dataset), function(var) {
paste0("\\item{", var, "} {", class(dataset[[var]]), ": Type label here}")
})

Check warning on line 98 in R/use_document_data.R

View check run for this annotation

Codecov / codecov/patch

R/use_document_data.R#L96-L98

Added lines #L96 - L98 were not covered by tests
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ reference:
- use_citation
- use_tutorial
- use_author
- use_document_data
- title: Package setup
desc: >
Package setup tasks, typically performed once.
Expand Down
30 changes: 30 additions & 0 deletions man/create_data_description.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions man/use_document_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading