Skip to content

Commit

Permalink
Improve logging.
Browse files Browse the repository at this point in the history
Improve console messages.
#50 and #51
  • Loading branch information
gfinak committed Sep 7, 2018
1 parent 136d87a commit a0f74d0
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 66 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Expand Up @@ -2,7 +2,8 @@
* Reduce the console output from logging. (ropensci/DataPackageR/issues/50)
* Create a new logger that logs at different thresholds to console and to file (ropensci/DataPackageR/issues/50)
* Default on build is not to install.
* Nicer message of what data sets are created (ropensci/DataPackageR/issues/51)
* Hide console output from Rmd render.
* Nicer messages describing data sets that are created (ropensci/DataPackageR/issues/51)

# DataPackageR 0.15.3
* conditional tests when pandoc is missing (ropensci/DataPackager/issues/46)
Expand Down
4 changes: 3 additions & 1 deletion R/build.R
Expand Up @@ -50,7 +50,9 @@ package_build <- function(packageName = NULL,
packageName <- basename(package_path)
# Is this a package root?
if (!is_r_package$find_file() == package_path) {
flog.fatal(paste0(package_path, " is not an R package root directory"), name = "console")
flog.fatal(paste0(package_path,
" is not an R package root directory"),
name = "console")
stop("exiting", call. = FALSE)
}
} else {
Expand Down
6 changes: 0 additions & 6 deletions R/digests.R
Expand Up @@ -16,9 +16,6 @@
old_data_digest[["DataVersion"]],
" and ", new_data_digest[["DataVersion"]]
))
{
stop("exiting", call. = FALSE)
}
}
greater <- apply(t(cbind(oldv, newv)), 2, function(x) x[2] > x[1])
equal <- apply(t(cbind(oldv, newv)), 2, function(x) x[2] == x[1])
Expand Down Expand Up @@ -94,9 +91,6 @@
"DESCRIPTION file must have a DataVersion",
" line. i.e. DataVersion: 0.2.0"
))
{
stop("exiting", call. = FALSE)
}
}
new_data_digest <- list()
new_data_digest[["DataVersion"]] <- pkg_description[["DataVersion"]]
Expand Down
18 changes: 11 additions & 7 deletions R/logger.R
Expand Up @@ -29,11 +29,15 @@
}
.multilog_setup <- function(LOGFILE = NULL) {
if (!is.null(LOGFILE)) {
flog.logger(name = "logfile",
appender = appender.file(LOGFILE),
threshold = TRACE)
flog.logger(
name = "logfile",
appender = appender.file(LOGFILE),
threshold = TRACE
)
}
flog.logger(name = "console",
appender = appender.console(),
threshold = INFO)
}
flog.logger(
name = "console",
appender = appender.console(),
threshold = INFO
)
}
52 changes: 42 additions & 10 deletions R/processData.R
Expand Up @@ -253,6 +253,9 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {
can_write <- FALSE
# environment for the data
ENVS <- new.env(hash = TRUE, parent = .GlobalEnv)
object_tally <- 0
already_built <- NULL
building <- NULL
for (i in seq_along(r_files)) {
dataenv <- new.env(hash = TRUE, parent = .GlobalEnv)
# assign ENVS into dataenv.
Expand Down Expand Up @@ -303,21 +306,49 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {
quiet = TRUE
)
# The created objects
object_names <- ls(dataenv)
object_names <- setdiff(ls(dataenv),
c("ENVS", already_built)) # ENVS is removed
object_tally <- object_tally | objects_to_keep %in% object_names
already_built <- unique(c(already_built,
objects_to_keep[objects_to_keep %in% object_names]))
.multilog_trace(paste0(
sum(objects_to_keep %in% object_names),
" required data objects created by ",
" data set(s) created by ",
basename(r_files[i])
))
.done(paste0(
sum(objects_to_keep %in% object_names),
" data set(s) created by ",
basename(r_files[i])
))
.done(paste0(sum(objects_to_keep %in% object_names),
" data objects created by ",
basename(r_files[i])))
if (sum(objects_to_keep %in% object_names) > 0) {
.bullet(
paste0(objects_to_keep[which(objects_to_keep %in% object_names)],"\n"),
crayon::red("\u2022"))
.add_newlines_to_vector <- function(x) {
x <- paste0(x, sep = "\n")
x[length(x)] <- gsub("\n", "", x[length(x)])
x
}
.bullet(
.add_newlines_to_vector(
objects_to_keep[which(objects_to_keep %in% object_names)]),
crayon::red("\u2022")
)
}

.bullet(
paste0(
"Built ",
ifelse(
sum(object_tally) == length(object_tally),
" all datasets!",
paste0(sum(object_tally), " of ",
length(object_tally), " data sets.")
)
),
ifelse(
sum(object_tally) == length(object_tally),
crayon::green("\u2618"),
crayon::green("\u2605")
)
)
if (sum(objects_to_keep %in% object_names) > 0) {
for (o in objects_to_keep[objects_to_keep %in% object_names]) {
assign(o, get(o, dataenv), ENVS)
Expand Down Expand Up @@ -527,8 +558,9 @@ DataPackageR <- function(arg = NULL, deps = TRUE) {

.ppfiles_mkvignettes <- function(dir = NULL) {
cat("\n")
if (proj_get() != dir)
if (proj_get() != dir) {
usethis::proj_set(dir)
}
pkg <- desc::desc(dir)
pkg$set_dep("knitr", "Suggests")
pkg$set_dep("rmarkdown", "Suggests")
Expand Down
19 changes: 10 additions & 9 deletions R/skeleton.R
Expand Up @@ -42,12 +42,12 @@ datapackage_skeleton <-
if (is.null(name)) {
stop("Must supply a package name", call. = FALSE)
}
#if (length(r_object_names) == 0) {
# if (length(r_object_names) == 0) {
# stop("You must specify r_object_names", call. = FALSE)
#}
#if (length(code_files) == 0) {
# }
# if (length(code_files) == 0) {
# stop("You must specify code_files", call. = FALSE)
#}
# }
if (force) {
unlink(file.path(path, name), recursive = TRUE, force = TRUE)
}
Expand All @@ -65,12 +65,12 @@ datapackage_skeleton <-
description$set("Package" = name)
description$set("Roxygen" = "list(markdown = TRUE)")
description$write()
.done("Added DataVersion string to DESCRIPTION")
.done(paste0("Added DataVersion string to ", crayon::blue("'DESCRIPTION'")))

usethis::use_directory("data-raw")
usethis::use_directory("data")
usethis::use_directory("inst/extdata")
.done("Created data and data-raw directories")
# .done("Created data and data-raw directories")

con <-
file(file.path(package_path, "Read-and-delete-me"), open = "w")
Expand Down Expand Up @@ -111,7 +111,8 @@ datapackage_skeleton <-
obj <- match.arg(obj, c("code", "dependencies"))
for (y in x) {
file.copy(y, file.path(package_path, "data-raw"), overwrite = TRUE)
.done("Copied ", obj, " into data-raw")
.done(paste0("Copied ", basename(y),
" into ", crayon::blue("'data-raw'")))
}
}
}
Expand All @@ -122,7 +123,7 @@ datapackage_skeleton <-
file.copy(x, file.path(package_path, "inst/extdata"),
recursive = TRUE, overwrite = TRUE
)
.done("Moved data into inst/extdata")
.done(paste0("Moved data into ", crayon::blue("'inst/extdata'")))
}
}
.copy_files_to_data_raw(code_files, obj = "code")
Expand All @@ -131,7 +132,7 @@ datapackage_skeleton <-

yml <- construct_yml_config(code = code_files, data = r_object_names)
yaml::write_yaml(yml, file = file.path(package_path, "datapackager.yml"))
.done("configured yaml file")
.done(paste0("configured ", crayon::blue("'datapackager.yml'"), " file"))


oldrdfiles <-
Expand Down
14 changes: 10 additions & 4 deletions R/use.R
Expand Up @@ -117,7 +117,10 @@ use_processing_script <- function(file = NULL, title = NULL, author = NULL) {
grepl("\\.rmd$", tolower(raw_file)))) {
# we have a valid file name and should create it.
file.create(file.path(proj_path, "data-raw", basename(raw_file)))
.update_header(file.path(proj_path, "data-raw", basename(raw_file)), title = title, author = author)
.update_header(file.path(proj_path,
"data-raw",
basename(raw_file)),
title = title, author = author)
# add it to the yaml.
yml <- yml_find(path = proj_path)
yml <- yml_add_files(yml, basename(raw_file))
Expand Down Expand Up @@ -177,7 +180,8 @@ use_data_object <- function(object_name = NULL) {
partitioned_file <- .partition_rmd_front_matter(file_contents)
}
if (!is.null(partitioned_file$front_matter)) {
front_matter <- .parse_yaml_front_matter(gsub("#'\\s*", "", partitioned_file$front_matter))
front_matter <- .parse_yaml_front_matter(
gsub("#'\\s*", "", partitioned_file$front_matter))
} else {
front_matter <- list()
}
Expand Down Expand Up @@ -206,9 +210,11 @@ use_data_object <- function(object_name = NULL) {
# open the file for writing.
connection <- file(file, open = "w+")
# write the header
writeLines(ifelse(grepl("\\.r$", tolower(file)), "#' ---", "---"), con = connection)
writeLines(ifelse(grepl("\\.r$", tolower(file)),
"#' ---", "---"), con = connection)
writeLines(front_matter, con = connection, sep = "")
writeLines(ifelse(grepl("\\.r$", tolower(file)), "#' ---", "---"), con = connection)
writeLines(ifelse(grepl("\\.r$", tolower(file)),
"#' ---", "---"), con = connection)
# write the body
if (!is.null(partitioned_file$body)) {
writeLines(partitioned_file$body, con = connection)
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Expand Up @@ -284,7 +284,7 @@
],
"releaseNotes": "https://github.com/ropensci/DataPackageR/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/DataPackageR/blob/master/README.md",
"fileSize": "305.803KB",
"fileSize": "306.25KB",
"contIntegration": [
"https://travis-ci.org/ropensci/DataPackageR",
"https://codecov.io/github/ropensci/DataPackageR?branch=master",
Expand Down
3 changes: 2 additions & 1 deletion inst/doc/YAML_CONFIG.R
@@ -1,7 +1,8 @@
## ---- echo = FALSE, results = 'hide'-------------------------------------
library(DataPackageR)
library(yaml)
yml <- DataPackageR::construct_yml_config(code = "subsetCars.Rmd", data = "cars_over_20")
yml <- DataPackageR::construct_yml_config(code = "subsetCars.Rmd",
data = "cars_over_20")

## ---- echo = FALSE, comment=""-------------------------------------------
cat(yaml::as.yaml(yml))
Expand Down
7 changes: 5 additions & 2 deletions inst/doc/usingDataPackageR.R
Expand Up @@ -44,7 +44,9 @@ df <- data.frame(pathString = file.path(
as.Node(df)

## ---- echo=FALSE, eval = rmarkdown::pandoc_available()-------------------
cat(yaml::as.yaml(yaml::yaml.load_file(file.path(tempdir(),"mtcars20","datapackager.yml"))))
cat(yaml::as.yaml(yaml::yaml.load_file(
file.path(tempdir(),
"mtcars20","datapackager.yml"))))

## ---- eval = rmarkdown::pandoc_available()-------------------------------
# Run the preprocessing code to build cars_over_20
Expand All @@ -67,7 +69,8 @@ document(file.path(tempdir(),"mtcars20"))

## ---- eval = rmarkdown::pandoc_available()-------------------------------
# Let's use the package we just created.
install.packages(file.path(tempdir(),"mtcars20_1.0.tar.gz"), type = "source", repos = NULL)
install.packages(file.path(tempdir(),"mtcars20_1.0.tar.gz"),
type = "source", repos = NULL)
if(!"package:mtcars20"%in%search())
attachNamespace('mtcars20') #use library() in your code
data("cars_over_20") # load the data
Expand Down
3 changes: 2 additions & 1 deletion tests/spelling.R
@@ -1,3 +1,4 @@
if (requireNamespace("spelling", quietly = TRUE)) {
spelling::spell_check_test(vignettes = TRUE, error = FALSE, skip_on_cran = TRUE)
spelling::spell_check_test(vignettes = TRUE,
error = FALSE, skip_on_cran = TRUE)
}
4 changes: 2 additions & 2 deletions tests/testthat/test-edge-cases.R
Expand Up @@ -166,8 +166,8 @@ test_that("package built in different edge cases", {

library(futile.logger)
DataPackageR:::.multilog_setup("/tmp/test.log")
DataPackageR:::.multilog_thresold(INFO,TRACE)
DataPackageR:::.multilog_thresold(INFO, TRACE)

suppressWarnings(expect_false({
DataPackageR:::.compare_digests(
list(
Expand Down
32 changes: 18 additions & 14 deletions tests/testthat/test-logger.R
@@ -1,25 +1,29 @@
context("logger")
test_that(".multilog_setup",{
expect_null(DataPackageR:::.multilog_setup(file.path(tempdir(),"test.log")))
test_that(".multilog_setup", {
expect_null(DataPackageR:::.multilog_setup(file.path(tempdir(), "test.log")))
})
test_that(".multilog_threshold",{
test_that(".multilog_threshold", {
expect_null(DataPackageR:::.multilog_thresold(INFO, TRACE))
})
test_that(".multilog_info",{
expect_output(DataPackageR:::.multilog_info("message"),"INFO .* message")
expect_true(file_test("-f",file.path(tempdir(),"test.log")))
test_that(".multilog_info", {
expect_output(DataPackageR:::.multilog_info("message"), "INFO .* message")
expect_true(file_test("-f", file.path(tempdir(), "test.log")))
})
test_that(".multilog_error",{
expect_output(DataPackageR:::.multilog_error("message"),"ERROR .* message")
test_that(".multilog_error", {
expect_output(DataPackageR:::.multilog_error("message"), "ERROR .* message")
})
test_that(".multilog_trace",{
test_that(".multilog_trace", {
expect_silent(DataPackageR:::.multilog_trace("message"))
expect_true(length(grep(pattern = "TRACE",readLines(file.path(tempdir(),"test.log"))))>0)
expect_true(length(grep(pattern = "TRACE",
readLines(file.path(tempdir(),
"test.log")))) > 0)
})
test_that(".multilog_warn",{
expect_output(DataPackageR:::.multilog_warn("message"),"WARN")
test_that(".multilog_warn", {
expect_output(DataPackageR:::.multilog_warn("message"), "WARN")
})
test_that(".multilog_debug",{
test_that(".multilog_debug", {
expect_silent(DataPackageR:::.multilog_debug("message"))
expect_true(length(grep(pattern = "DEBUG",readLines(file.path(tempdir(),"test.log"))))>0)
expect_true(length(grep(pattern = "DEBUG",
readLines(file.path(tempdir(),
"test.log")))) > 0)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-r-processing.R
Expand Up @@ -13,15 +13,15 @@ test_that("R file processing works and creates vignettes", {
expect_equal(
basename(
package_build(
file.path(tempdir(), "rfiletest"),
file.path(tempdir(), "rfiletest"),
install = TRUE
)
),
"rfiletest_1.0.tar.gz"
)
v <- vignette(package = "rfiletest")
expect_equal(v$results[, "Item"], "rfileTest")

unlink(file.path(tempdir(), "rfiletest"),
recursive = TRUE,
force = TRUE
Expand Down

0 comments on commit a0f74d0

Please sign in to comment.