Skip to content

Commit

Permalink
Merge pull request #120 from ropensci/verbose
Browse files Browse the repository at this point in the history
Deal with excessive verbosity
  • Loading branch information
slager committed Apr 16, 2024
2 parents 3852150 + ffa9282 commit 3173a54
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 85 deletions.
34 changes: 34 additions & 0 deletions R/DataPackageR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,37 @@
## usethis namespace: start
## usethis namespace: end
NULL

#' Options consulted by DataPackageR
#'
#' @description User-configurable options consulted by DataPackageR, which
#' provide a mechanism for setting default behaviors for various functions.
#'
#' If the built-in defaults don't suit you, set one or more of these options.
#' Typically, this is done in the \code{.Rprofile} startup file, which you can open
#' for editing with \code{usethis::edit_r_profile()} - this will set the specified
#' options for all future R sessions. The following setting is recommended to
#' not be prompted upon each package build for a NEWS update:
#'
#' \code{options(DataPackageR_interact = FALSE)}
#'
#' @section Options for the DataPackageR package:
#'
#' - \code{DataPackageR_interact}: Upon package load, this defaults to the value of
#' \code{interactive()}, unless the option has been previously set (e.g., in
#' \code{.Rprofile}). TRUE prompts user interactively for a NEWS update on
#' \code{package_build()}. See the example above and the [rOpenSci blog
#' post](https://ropensci.org/blog/2018/09/18/datapackager/) for more details
#' on how to set this to FALSE, which will never prompt user for a NEWS
#' update. FALSE is also the setting used for DataPackageR's internal tests.
#'
#' - \code{DataPackageR_verbose}: Default upon package load is TRUE. FALSE suppresses
#' all console output and is currently only used for DataPackageR's automated
#' unit tests.
#'
#' - \code{DataPackageR_packagebuilding}: Default upon package load is FALSE. This
#' option is used internally for package operations and changing it is not
#' recommended.
#'
#' @name DataPackageR_options
NULL
25 changes: 13 additions & 12 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ package_build <- function(packageName = NULL,
}
# This should always be a proper name of a directory, either current or a
# subdirectory
if (inherits(
try(is_r_package$find_file(path = package_path))
, "try-error"
)) {
flog.fatal(paste0(
package_path,
" is not a valid R package directory beneath ",
getwd()
), name = "console")
stop("exiting", call. = FALSE)
}
tryCatch({is_r_package$find_file(path = package_path)},
error = function(cond){
flog.fatal(paste0(
package_path,
" is not a valid R package directory beneath ",
getwd()
), name = "console")
stop("exiting", call. = FALSE)
}
)

# Return success if we've processed everything
success <-
Expand All @@ -105,7 +104,8 @@ package_build <- function(packageName = NULL,
.multilog_trace("Building package")
location <- build(package_path,
path = dirname(package_path),
vignettes = vignettes
vignettes = vignettes,
quiet = ! getOption('DataPackageR_verbose', TRUE)
)
# try to install and then reload the package in the current session
if (install) {
Expand All @@ -116,6 +116,7 @@ package_build <- function(packageName = NULL,
}

.next_steps <- function() {
if (! getOption('DataPackageR_verbose', TRUE)) return(invisible(NULL))
cat(crayon::green(crayon::bold("Next Steps")), "\n") # nolint
cat(crayon::white(crayon::yellow(crayon::bold("1. Update your package documentation.")), "\n")) # nolint
cat(crayon::white(" - Edit the documentation.R file in the package source", crayon::green("data-raw"), "subdirectory and update the roxygen markup."), "\n") # nolint
Expand Down
10 changes: 7 additions & 3 deletions R/environments.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ datapackager_object_read <- function(name) {
temp_folder_path<-file.path(tempdir(),yml_find(usethis::proj_get())[["configuration"]][["render_root"]]$tmp)

if(file.exists(objectPath<-file.path(temp_folder_path,paste0(name,".rds")))){
message('loading ',name,' from temporary folder from previous build attempt.')
if (getOption('DataPackageR_verbose', TRUE)){
message('loading ',name,' from temporary folder from previous build attempt.')
}
object<-readRDS(objectPath)

}else if(file.exists(objectPath<-file.path(project_data_path(),paste0(name,".rda")))){
message('loading ',name,' from data directory.')
print(objectPath)
if (getOption('DataPackageR_verbose', TRUE)){
message('loading ',name,' from data directory.')
print(objectPath)
}
load_env<-new.env()
load(objectPath,envir = load_env)
object<-load_env[[ls(load_env)[1]]]
Expand Down
12 changes: 11 additions & 1 deletion R/logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
flog.threshold(console, name = "console")
flog.threshold(logfile, name = "logfile")
}

select_console_appender <- function(){
if (getOption('DataPackageR_verbose', TRUE)){
appender.console()
} else {
# quiet console appender
function(line) { }
}
}

.multilog_setup <- function(LOGFILE = NULL) {
if (!is.null(LOGFILE)) {
flog.logger(
Expand All @@ -37,7 +47,7 @@
}
flog.logger(
name = "console",
appender = appender.console(),
appender = select_console_appender(),
threshold = INFO
)
}
8 changes: 5 additions & 3 deletions R/processData.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
#' @importFrom utils getSrcref modifyList
#' @importFrom usethis proj_set proj_get
DataPackageR <- function(arg = NULL, deps = TRUE) {
if (! getOption('DataPackageR_verbose', TRUE)){
withr::local_options(list(usethis.quiet = TRUE))
}
pkg_dir <- arg
pkg_dir <- normalizePath(pkg_dir, winslash = "/")
cat("\n")
if (getOption('DataPackageR_verbose', TRUE)) cat("\n")
usethis::proj_set(path = pkg_dir)
raw_data_dir <- "data-raw"
target <- normalizePath(file.path(pkg_dir, raw_data_dir), winslash = "/")
Expand Down Expand Up @@ -504,7 +507,6 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {


.ppfiles_mkvignettes <- function(dir = NULL) {
cat("\n")
if (proj_get() != dir) {
usethis::proj_set(dir) #nocov
}
Expand Down Expand Up @@ -748,7 +750,7 @@ project_data_path <- function(file = NULL) {
#' }
#' }
document <- function(path = ".", install = TRUE, ...) {
cat("\n")
if (getOption('DataPackageR_verbose', TRUE)) cat("\n")
usethis::proj_set(path = path)
path <- usethis::proj_get()
assert_that(file.exists(file.path(path, "data-raw", "documentation.R")))
Expand Down
14 changes: 9 additions & 5 deletions R/prompt.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
if (interactive()&interact) {
cat(crayon::cyan("Enter a text description of the changes for the NEWS.md file.\n")) #nocov
}else{
cat(crayon::cyan("Non-interactive NEWS.md file update.\n"))
if (getOption('DataPackageR_verbose', TRUE)){
cat(crayon::cyan("Non-interactive NEWS.md file update.\n"))
}
}
change_description <-
ifelse(
Expand Down Expand Up @@ -83,10 +85,12 @@
added <- objectlist[["added"]]
deleted <- objectlist[["deleted"]]
changed <- objectlist[["changed"]]

.write_changes <- function(string, news_con, what = NULL) {
if (length(string) != 0) {
cat(crayon::cyan(paste0("* ",what,": ",string,"\n")))
if (getOption('DataPackageR_verbose', TRUE)){
cat(crayon::cyan(paste0("* ",what,": ",string,"\n")))
}
writeLines(text = paste0("* ",what,": ", string),
con = news_con,
sep = "\n")
Expand All @@ -95,12 +99,12 @@
.write_changes(added, news_con, "Added")
.write_changes(deleted, news_con, "Deleted")
.write_changes(changed, news_con, "Changed")

#write the rest of the data
writeLines(news_file_data,
con = news_con,
sep = "\n"
)
flush(news_con)
close(news_con)
}
}
5 changes: 4 additions & 1 deletion R/skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ datapackage_skeleton <-
r_object_names = character(),
raw_data_dir = character(),
dependencies = character()) {
if (! getOption('DataPackageR_verbose', TRUE)){
withr::local_options(list(usethis.quiet = TRUE))
}
if (is.null(name)) {
stop("Must supply a package name", call. = FALSE)
}
Expand Down Expand Up @@ -217,5 +220,5 @@ datapackage.skeleton <- function(name = NULL,
}

.cat_line <- function(...) {
cat(..., "\n", sep = "")
if (getOption('DataPackageR_verbose', TRUE)) cat(..., "\n", sep = "")
}
14 changes: 8 additions & 6 deletions R/use.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#' the inst/extdata directory.
#'
#' @param path \code{character} path to file or directory.
#' @param ignore \code{logical} whether to ignore the path or file in git and R build.
#'
#' @param ignore \code{logical} whether to ignore the path or file in git and R build.
#'
#' @return invisibly returns TRUE for success. Stops on failure.
#' @importFrom usethis proj_get proj_set create_package use_data_raw
#' @importFrom utils file_test
Expand Down Expand Up @@ -42,7 +42,7 @@ use_raw_dataset <- function(path = NULL, ignore = FALSE) {
overwrite = TRUE
)
if (ignore) {
# inst/extdata is a path relative to the project root
# inst/extdata is a path relative to the project root
# as needed by git_ignore
use_ignore(basename(raw_file), path = file.path("inst", "extdata"))
}
Expand All @@ -68,7 +68,7 @@ use_raw_dataset <- function(path = NULL, ignore = FALSE) {
#'
#' The Rmd or R file or directory specified by \code{file} will be moved into
#' the data-raw directory. It will also be added to the yml configuration file.
#' Any existing file by that name will be overwritten when overwrite is set to TRUE
#' Any existing file by that name will be overwritten when overwrite is set to TRUE
#'
#' @param file \code{character} path to an existing file or name of a new R or Rmd file to create.
#' @param title \code{character} title of the processing script for the yaml header. Used only if file is being created.
Expand Down Expand Up @@ -112,7 +112,7 @@ use_processing_script <- function(file = NULL, title = NULL, author = NULL, over
}
}
raw_file <- suppressWarnings(normalizePath(file))

if (utils::file_test("-f", raw_file)) {
# test if it's an R or Rmd file.
if (!(grepl("\\.rmd$", tolower(raw_file)) |
Expand Down Expand Up @@ -141,7 +141,9 @@ use_processing_script <- function(file = NULL, title = NULL, author = NULL, over
!overwrite) {
.bullet(paste0("Skipping file creation: pass overwrite = TRUE to use_processing_script()"), bullet = crayon::red("\u2622")) #nolint
} else {
cat("Attempting to create ", raw_file)
if (getOption('DataPackageR_verbose', TRUE)){
cat("Attempting to create ", raw_file)
}
file.create(file.path(proj_path, "data-raw", basename(raw_file)))
.update_header(file.path(proj_path,
"data-raw",
Expand Down
24 changes: 14 additions & 10 deletions R/yamlR.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @details Add, remove files and objects, enable or disable parsing of specific files, list objects or files in a yaml config, or write a config back to a package.
#' @importFrom yaml yaml.load_file as.yaml write_yaml
#' @importFrom stats runif
#' @importFrom withr with_options
#' @importFrom withr with_options
#' @export
#'
#' @examples
Expand Down Expand Up @@ -62,7 +62,7 @@ yml_add_files <- function(config, filenames) {
config[["configuration"]][["files"]][[i]]$enabled <- TRUE
}
}
cat(yaml::as.yaml(config))
if (getOption('DataPackageR_verbose', TRUE)) cat(yaml::as.yaml(config))
return(config)
}

Expand Down Expand Up @@ -111,7 +111,7 @@ yml_add_objects <- function(config, objects) {
config[["configuration"]][["objects"]],
objects
))
cat(yaml::as.yaml(config))
if (getOption('DataPackageR_verbose', TRUE)) cat(yaml::as.yaml(config))
return(config)
}

Expand All @@ -123,8 +123,10 @@ yml_list_objects <- function(config) {
# assume config is a package root path
config <- yml_find(config)
}
cat("\n")
cat(config[["configuration"]][["objects"]])
if (getOption('DataPackageR_verbose', TRUE)){
cat("\n")
cat(config[["configuration"]][["objects"]])
}
invisible(config[["configuration"]][["objects"]])
}

Expand All @@ -135,8 +137,10 @@ yml_list_files <- function(config) {
# assume config is a package root path
config <- yml_find(config)
}
cat("\n")
cat(names(config[["configuration"]][["files"]]))
if (getOption('DataPackageR_verbose', TRUE)){
cat("\n")
cat(names(config[["configuration"]][["files"]]))
}
invisible(names(config[["configuration"]][["files"]]))
}

Expand All @@ -152,7 +156,7 @@ yml_remove_objects <- function(config, objects) {
config[["configuration"]][["objects"]],
objects
)
cat(yaml::as.yaml(config))
if (getOption('DataPackageR_verbose', TRUE)) cat(yaml::as.yaml(config))
return(config)
}

Expand All @@ -168,7 +172,7 @@ yml_remove_files <- function(config, filenames) {
config[["configuration"]][["files"]][[i]] <- NULL
}
}
cat(yaml::as.yaml(config))
if (getOption('DataPackageR_verbose', TRUE)) cat(yaml::as.yaml(config))
return(config)
}

Expand Down Expand Up @@ -237,7 +241,7 @@ construct_yml_config <- function(code = NULL, data = NULL, render_root = NULL) {
for (i in code) {
files[[i]]$enabled <- TRUE
}

# create render root at a temporary directory.
# this will be stored in the yaml. What if we restart?
# see processData - it gets validated and created if not existing.
Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.onLoad <- function(libname, pkgname) {
# keeping this first option hardcoded on load for now
options("DataPackageR_packagebuilding" = FALSE)
options("DataPackageR_verbose" = TRUE)
# respect previous user setting for 'DataPackageR_interact' if set
op <- options()
op.DataPackageR <- list(
Expand Down
37 changes: 37 additions & 0 deletions man/DataPackageR_options.Rd

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

0 comments on commit 3173a54

Please sign in to comment.