@@ -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 ) {
0 commit comments