Skip to content

Commit 219dc5e

Browse files
Merge pull request #1 from pepijn-devries/work-in-progress
Updates to comply with CRAN policy
2 parents f96b0ba + 2b40953 commit 219dc5e

File tree

7 files changed

+54
-35
lines changed

7 files changed

+54
-35
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: ECOTOXr
22
Type: Package
33
Title: Download and Extract Data from US EPA's ECOTOX Database
4-
Version: 0.1.0
5-
Date: 2021-10-03
4+
Version: 0.1.1
5+
Date: 2021-10-04
66
Authors@R: c(person("Pepijn", "de Vries", role = c("aut", "cre", "dtc"),
77
email = "[email protected]"))
88
Author:
@@ -28,7 +28,7 @@ Imports:
2828
Suggests:
2929
testthat (>= 3.0.0),
3030
webchem
31-
URL: https://github.com/pepijn-devries/ECOTOXr
31+
URL: <https://github.com/pepijn-devries/ECOTOXr>
3232
BugReports: https://github.com/pepijn-devries/ECOTOXr/issues
3333
License: GPL (>= 3)
3434
Encoding: UTF-8

NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
ECOTOXr v0.1.1 (Release date: 2021-10-04)
2+
=============
3+
4+
* Updates to DESCRIPTION file and manual in order to
5+
comply with CRAN policies.
6+
7+
* Function progress reports are now shown with 'message'
8+
instead of 'cat', such that they can be suppressed.
9+
110
ECOTOXr v0.1.0 (Release date: 2021-10-03)
211
=============
312

R/database_access.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ get_ecotox_info <- function(path = get_ecotox_path(), version) {
131131
} else {
132132
inf <- default
133133
}
134-
cat(paste(inf, collapse = "\n"))
134+
message(crayon::white(paste(inf, collapse = "\n")))
135135
return(invisible(inf))
136136
}
137137

@@ -158,7 +158,7 @@ get_ecotox_info <- function(path = get_ecotox_path(), version) {
158158
#'
159159
#' ## All except fields from the table 'dose_response_details'
160160
#' ## that are available from the ECOTOX database:
161-
#' list_ecotox_fields("all")
161+
#' list_ecotox_fields("full")
162162
#' @author Pepijn de Vries
163163
#' @export
164164
list_ecotox_fields <- function(which = c("default", "full", "all"), include_table = TRUE) {

R/helpers.r

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
.add_tags <- function(x, sqlite) {
22
if (missing(sqlite)) sqlite <- attributes(x)$database_file
3-
attributes(x)$date_created <- Sys.Date()
4-
attributes(x)$created_with <- sprintf("Package ECOTOXr v%s", utils::packageVersion("ECOTOXr"))
3+
attributes(x)$date_created <- Sys.Date()
4+
attributes(x)$created_with <- sprintf("Package ECOTOXr v%s", utils::packageVersion("ECOTOXr"))
55
attributes(x)$database_file <- sqlite
66
return(x)
77
}
8+
9+
.fail_on_missing <- function(path = get_ecotox_path()) {
10+
test <- check_ecotox_availability(path)
11+
if (!test) {
12+
stop("No local database located. Download data first by calling 'download_ecotox_data()'")
13+
} else return(test)
14+
}

R/imports.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
if (check_ecotox_availability()) {
44
crayon::green("ECOTOX database file located, you are ready to go!\n")
55
} else {
6-
crayon::red("ECOTOX database file not present! Invoke download and database build using 'download_ecotox_data()'\n")
6+
crayon::red("ECOTOX database file not present! Invoke download and\ndatabase build using 'download_ecotox_data()'\n")
77
}
88
})
99
}

R/init.r

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ check_ecotox_availability <- function(target = get_ecotox_path()) {
3434
return(result)
3535
}
3636

37-
.fail_on_missing <- function(path = get_ecotox_path()) {
38-
test <- check_ecotox_availability(path)
39-
if (!test) {
40-
stop("No local database located. Download data first by calling 'download_ecotox_data()'")
41-
} else return(test)
42-
}
43-
4437
#' The local path to the ECOTOX database (directory or sqlite file)
4538
#'
4639
#' Obtain the local path to where the ECOTOX database is (or will be) placed.
@@ -80,6 +73,7 @@ get_ecotox_path <- function() {
8073
#' When found it will attempt to download the zipped archive containing all required data. This data is than
8174
#' extracted and a local copy of the database is build.
8275
#'
76+
#' Use '\code{\link{suppressMessages}}' to suppress the progress report.
8377
#' @section Known issues:
8478
#' On some machines this function fails to connect to the database download URL from the EPA website due to missing
8579
#' SSL certificates. Unfortunately, there is no easy fix for this in this package. A work around is to download and
@@ -100,7 +94,8 @@ get_ecotox_path <- function() {
10094
#' @name download_ecotox_data
10195
#' @examples
10296
#' \dontrun{
103-
#' download_ecotox_data()
97+
#' ## This will download and build the database in your temp dir:
98+
#' download_ecotox_data(tempdir())
10499
#' }
105100
#' @author Pepijn de Vries
106101
#' @export
@@ -110,13 +105,13 @@ download_ecotox_data <- function(target = get_ecotox_path(), write_log = TRUE, a
110105
cat(sprintf("A local database already exists (%s).", paste(attributes(avail)$file$database, collapse = ", ")))
111106
prompt <- readline(prompt = "Do you wish to continue and potentially overwrite the existing database? (y/n) ")
112107
if (!startsWith("Y", toupper(prompt))) {
113-
cat("Download aborted...\n")
108+
message("Download aborted...\n")
114109
return(invisible(NULL))
115110
}
116111
}
117112
if (!dir.exists(target)) dir.create(target, recursive = T)
118113
## Obtain download link from EPA website:
119-
cat("Obtaining download link from EPA website... ")
114+
message(crayon::white("Obtaining download link from EPA website... "))
120115
con <- url("https://cfpub.epa.gov/ecotox/index.cfm")
121116
link <- rvest::read_html(con)
122117
link <- rvest::html_nodes(link, "a.ascii-link")
@@ -125,26 +120,26 @@ download_ecotox_data <- function(target = get_ecotox_path(), write_log = TRUE, a
125120
dest_path <- file.path(target, utils::tail(unlist(strsplit(link, "/")), 1))
126121
closeAllConnections()
127122
if (length(link) == 0) stop("Could not find ASCII download link...")
128-
cat(crayon::green("Done\n"))
123+
message(crayon::green("Done\n"))
129124
proceed.download <- T
130125
if (file.exists(dest_path) && ask) {
131126
prompt <- readline(prompt = sprintf("ECOTOX data is already present (%s), overwrite (y/n)? ", dest_path))
132127
proceed.download <- startsWith("Y", toupper(prompt))
133128
}
134129
if (proceed.download) {
135-
cat(sprintf("Start downloading ECOTOX data from %s...\n", link))
130+
message(crayon::white(sprintf("Start downloading ECOTOX data from %s...\n", link)))
136131
con <- url(link, "rb")
137132
dest <- file(gsub(".zip", ".incomplete.download", dest_path, fixed = T), "wb")
138133
mb <- 0
139134
repeat {
140135
read <- readBin(con, "raw", 1024*1024) ## download in 1MB chunks.
141136
writeBin(read, dest)
142137
mb <- mb + 1
143-
cat(sprintf("\r%i MB downloaded...", mb))
138+
message(crayon::white(sprintf("\r%i MB downloaded...", mb)), appendLF = F)
144139
if (length(read) == 0) break
145140
}
146141
closeAllConnections()
147-
cat(crayon::green(" Done\n"))
142+
message(crayon::green(" Done\n"))
148143
}
149144
file.rename(gsub(".zip", ".incomplete.download", dest_path, fixed = T), dest_path)
150145

@@ -171,22 +166,22 @@ download_ecotox_data <- function(target = get_ecotox_path(), write_log = TRUE, a
171166
}
172167
}
173168
if (proceed.unzip) {
174-
cat("Extracting downloaded zip file... ")
169+
message(crayon::white("Extracting downloaded zip file... "))
175170
utils::unzip(file.path(target, utils::tail(unlist(strsplit(link, "/")), 1)), exdir = target)
176-
cat(crayon::green("Done\n"))
171+
message(crayon::green("Done\n"))
177172
if (ask &&
178173
startsWith("Y", toupper(readline(prompt = "Done extracting zip file, remove it to save disk space (y/n)? ")))) {
179-
cat("Trying to delete zip file... ")
174+
message(crayon::white("Trying to delete zip file... "))
180175
tryCatch({
181176
file.remove(file.path(target, utils::tail(unlist(strsplit(link, "/")), 1)))
182-
cat(crayon::green("Done\n"))
177+
message(crayon::green("Done\n"))
183178
}, error = function(e) {
184-
cat(crayon::red("Failed to delete the file, continuing with next step"))
179+
message(crayon::red("Failed to delete the file, continuing with next step"))
185180
})
186181
}
187182
}
188-
cat("Start constructing SQLite database from downloaded tables...\n")
189-
cat("Note that this may take some time...\n")
183+
message(crayon::white("Start constructing SQLite database from downloaded tables...\n"))
184+
message(crayon::white("Note that this may take some time...\n"))
190185
build_ecotox_sqlite(extr.path, target, write_log)
191186
return(invisible(NULL))
192187
}
@@ -213,6 +208,8 @@ download_ecotox_data <- function(target = get_ecotox_path(), write_log = TRUE, a
213208
#' consequences for reproducibility, but only if you build search queries that look for such special characters. It is
214209
#' therefore advised to stick to common (non-accented) alpha-numerical characters in your searches, for the sake of
215210
#' reproducibility.
211+
#'
212+
#' Use '\code{\link{suppressMessages}}' to suppress the progress report.
216213
#'
217214
#' @param source A \code{character} string pointing to the directory path where the text files with the raw
218215
#' tables are located. These can be obtained by extracting the zip archive from \url{https://cfpub.epa.gov/ecotox/}
@@ -237,7 +234,8 @@ download_ecotox_data <- function(target = get_ecotox_path(), write_log = TRUE, a
237234
#' dir <- gsub(".sqlite", "", files$database, fixed = T)
238235
#' path <- files$path
239236
#' if (dir.exists(file.path(path, dir))) {
240-
#' build_ecotox_sqlite(source = file.path(path, dir), destination = get_ecotox_path())
237+
#' ## This will build the database in your temp directory:
238+
#' build_ecotox_sqlite(source = file.path(path, dir), destination = tempdir())
241239
#' }
242240
#' }
243241
#' }
@@ -248,8 +246,11 @@ build_ecotox_sqlite <- function(source, destination = get_ecotox_path(), write_l
248246
dbcon <- RSQLite::dbConnect(RSQLite::SQLite(), file.path(destination, dbname))
249247

250248
## Loop the text file tables and add them to the sqlite database 1 by 1
249+
i <- 0
251250
by(.db_specs, .db_specs$table, function(tab) {
252-
cat(sprintf("Adding '%s' table to database:\n", tab$table[[1]]))
251+
i <<- i + 1
252+
message(crayon::white(sprintf("Adding '%s' table (%i/%i) to database:\n",
253+
tab$table[[1]], i, length(unique(.db_specs$table)))))
253254
filename <- file.path(source, paste0(tab$table[[1]], ".txt"))
254255
if (!file.exists(filename)) filename <- file.path(source, "validation", paste0(tab$table[[1]], ".txt"))
255256

@@ -276,7 +277,8 @@ build_ecotox_sqlite <- function(source, destination = get_ecotox_path(), write_l
276277
lines.read <- 1
277278
## Copy tables in 50000 line fragments to database, to avoid memory issues
278279
frag.size <- 50000
279-
cat(sprintf("\r 0 lines (incl. header) added of '%s' added to database", tab$table[[1]]))
280+
message(crayon::white(sprintf("\r 0 lines (incl. header) added of '%s' added to database", tab$table[[1]])),
281+
appendLF = F)
280282
repeat {
281283
if (is.null(head)) {
282284
head <- iconv(readr::read_lines(filename, skip = 0, n_max = 1, progress = F), to = "UTF8", sub = "*")
@@ -303,11 +305,12 @@ build_ecotox_sqlite <- function(source, destination = get_ecotox_path(), write_l
303305
stringsAsFactors = F, strip.white = F)
304306

305307
RSQLite::dbWriteTable(dbcon, tab$table[[1]], table.frag, append = T)
306-
cat(sprintf("\r %i lines (incl. header) added of '%s' added to database", lines.read, tab$table[[1]]))
308+
message(crayon::white(sprintf("\r %i lines (incl. header) added of '%s' added to database", lines.read, tab$table[[1]])),
309+
appendLF = F)
307310
if (length(body) < testsize) break
308311
}
309312
}
310-
cat(crayon::green(" Done\n"))
313+
message(crayon::green(" Done\n"))
311314
})
312315
RSQLite::dbDisconnect(dbcon)
313316
if (write_log) {

R/wrappers.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
#' @examples
7777
#' \dontrun{
7878
#' ## let's find the ids of all ecotox tests on species
79-
#' ## where latin names contain either of 2 specific genus names and
79+
#' ## where Latin names contain either of 2 specific genus names and
8080
#' ## where they were exposed to the chemical benzene
8181
#' if (check_ecotox_availability()) {
8282
#' search <-

0 commit comments

Comments
 (0)