|
| 1 | +#' @rdname get_path |
| 2 | +#' @name get_ecotox_sqlite_file |
| 3 | +#' @export |
| 4 | +get_ecotox_sqlite_file <- function(path = get_ecotox_path(), version) { |
| 5 | + if (missing(version)) { |
| 6 | + version <- NULL |
| 7 | + } else { |
| 8 | + if (length(version) != 1) stop("Argument 'version' should hold a single element!") |
| 9 | + version <- as.Date(version, format = "%m_%d_%Y") |
| 10 | + } |
| 11 | + files <- attributes(.fail_on_missing(path))$files |
| 12 | + results <- nrow(files) |
| 13 | + files <- files[which(files$date == ifelse(is.null(version), max(files$date)[[1]], version)),] |
| 14 | + if (results > 1 && is.null(version)) { |
| 15 | + warning(sprintf("Multiple versions of the database found and not one specified. Using the most recent version (%s)", |
| 16 | + format(files$date, "%Y-%m-%d"))) |
| 17 | + } |
| 18 | + return(file.path(files$path, files$database)) |
| 19 | +} |
| 20 | + |
| 21 | +#' Open or close a connection to the local ECOTOX database |
| 22 | +#' |
| 23 | +#' Wrappers for \code{\link[RSQLite:SQLite]{dbConnect}} and \code{\link[RSQLite:SQLite]{dbDisconnect}} methods. |
| 24 | +#' |
| 25 | +#' Open or close a connection to the local ECOTOX database. These functions are only required when you want |
| 26 | +#' to send custom queries to the database. For most searches the \code{\link{search_ecotox}} function |
| 27 | +#' will be adequate. |
| 28 | +#' |
| 29 | +#' @param path A \code{character} string with the path to the location of the local database (default is |
| 30 | +#' \code{\link{get_ecotox_path}()}). |
| 31 | +#' @param version A \code{character} string referring to the release version of the database you wish to locate. |
| 32 | +#' It should have the same format as the date in the EPA download link, which is month, day, year, separated by |
| 33 | +#' underscores ("\%m_\%d_\%Y"). When missing, the most recent available copy is selected automatically. |
| 34 | +#' @param conn An open connection to the ECOTOX database that needs to be closed. |
| 35 | +#' @param ... Arguments that are passed to \code{\link[RSQLite:SQLite]{dbConnect}} method |
| 36 | +#' or \code{\link[RSQLite:SQLite]{dbDisconnect}} method. |
| 37 | +#' @return A database connection in the form of a \code{\link[DBI]{DBIConnection-class}} object. |
| 38 | +#' The object is tagged with: a time stamp; the package version used; and the |
| 39 | +#' file path of the SQLite database used in the connection. These tags are added as attributes |
| 40 | +#' to the object. |
| 41 | +#' @rdname dbConnectEcotox |
| 42 | +#' @name dbConnectEcotox |
| 43 | +#' @examples |
| 44 | +#' \dontrun{ |
| 45 | +#' ## This will only work when a copy of the database exists: |
| 46 | +#' con <- dbConnectEcotox() |
| 47 | +#' |
| 48 | +#' ## check if the connection works by listing the tables in the database: |
| 49 | +#' dbListTables(con) |
| 50 | +#' |
| 51 | +#' ## Let's be a good boy/girl and close the connection to the database when we're done: |
| 52 | +#' dbDisconnectEcotox(con) |
| 53 | +#' } |
| 54 | +#' @author Pepijn de Vries |
| 55 | +#' @export |
| 56 | +dbConnectEcotox <- function(path = get_ecotox_path(), version, ...) { |
| 57 | + f <- get_ecotox_sqlite_file(path, version) |
| 58 | + return(.add_tags(RSQLite::dbConnect(RSQLite::SQLite(), f, ...), f)) |
| 59 | +} |
| 60 | + |
| 61 | +#' @rdname dbConnectEcotox |
| 62 | +#' @name dbDisconnectEcotox |
| 63 | +#' @export |
| 64 | +dbDisconnectEcotox <- function(conn, ...) { |
| 65 | + RSQLite::dbDisconnect(conn, ...) |
| 66 | +} |
| 67 | + |
| 68 | +#' Cite the downloaded copy of the ECOTOX database |
| 69 | +#' |
| 70 | +#' Cite the downloaded copy of the ECOTOX database and this package for reproducible results. |
| 71 | +#' |
| 72 | +#' When you download a copy of the EPA ECOTOX database using \code{\link{download_ecotox_data}()}, a BibTex file |
| 73 | +#' is stored that registers the database release version and the access (= download) date. Use this function |
| 74 | +#' to obtain a citation to that specific download. |
| 75 | +#' |
| 76 | +#' In order for others to reproduce your results, it is key to cite the data source as accurately as possible. |
| 77 | +#' @param path A \code{character} string with the path to the location of the local database (default is |
| 78 | +#' \code{\link{get_ecotox_path}()}). |
| 79 | +#' @param version A \code{character} string referring to the release version of the database you wish to locate. |
| 80 | +#' It should have the same format as the date in the EPA download link, which is month, day, year, separated by |
| 81 | +#' underscores ("\%m_\%d_\%Y"). When missing, the most recent available copy is selected automatically. |
| 82 | +#' @return Returns a \code{vector} of \code{\link{bibentry}}'s, containing a reference to the downloaded database |
| 83 | +#' and this package. |
| 84 | +#' @rdname cite_ecotox |
| 85 | +#' @name cite_ecotox |
| 86 | +#' @examples |
| 87 | +#' \dontrun{ |
| 88 | +#' ## In order to cite downloaded database and this package: |
| 89 | +#' cite_ecotox() |
| 90 | +#' } |
| 91 | +#' @author Pepijn de Vries |
| 92 | +#' @export |
| 93 | +cite_ecotox <- function(path = get_ecotox_path(), version) { |
| 94 | + db <- get_ecotox_sqlite_file(path, version) |
| 95 | + bib <- gsub(".sqlite", "_cit.txt", db, fixed = T) |
| 96 | + if (!file.exists(bib)) stop("No bibentry reference to database download found!") |
| 97 | + result <- utils::readCitationFile(bib) |
| 98 | + return(c(result, utils::citation("ECOTOXr"))) |
| 99 | +} |
| 100 | + |
| 101 | +#' Get information on the local ECOTOX database when available |
| 102 | +#' |
| 103 | +#' Get information on how and when the local ECOTOX database was build. |
| 104 | +#' |
| 105 | +#' Get information on how and when the local ECOTOX database was build. This information is retrieved |
| 106 | +#' from the log-file that is (optionally) stored with the local database when calling \code{\link{download_ecotox_data}} |
| 107 | +#' or \code{\link{build_ecotox_sqlite}}. |
| 108 | +#' @param path A \code{character} string with the path to the location of the local database (default is |
| 109 | +#' \code{\link{get_ecotox_path}()}). |
| 110 | +#' @param version A \code{character} string referring to the release version of the database you wish to locate. |
| 111 | +#' It should have the same format as the date in the EPA download link, which is month, day, year, separated by |
| 112 | +#' underscores ("\%m_\%d_\%Y"). When missing, the most recent available copy is selected automatically. |
| 113 | +#' @return Returns a \code{vector} of \code{character}s, containing a information on the selected local ECOTOX database. |
| 114 | +#' @rdname get_ecotox_info |
| 115 | +#' @name get_ecotox_info |
| 116 | +#' @examples |
| 117 | +#' \dontrun{ |
| 118 | +#' ## Show info on the current database (only works when one is downloaded and build): |
| 119 | +#' get_ecotox_info() |
| 120 | +#' } |
| 121 | +#' @author Pepijn de Vries |
| 122 | +#' @export |
| 123 | +get_ecotox_info <- function(path = get_ecotox_path(), version) { |
| 124 | + default <- "No information available\n" |
| 125 | + inf <- tryCatch({ |
| 126 | + db <- get_ecotox_sqlite_file(path, version) |
| 127 | + gsub(".sqlite", ".log", db, fixed = T) |
| 128 | + }, error = function(e) return(default)) |
| 129 | + if (file.exists(inf)) { |
| 130 | + inf <- readLines(inf) |
| 131 | + } else { |
| 132 | + inf <- default |
| 133 | + } |
| 134 | + cat(paste(inf, collapse = "\n")) |
| 135 | + return(invisible(inf)) |
| 136 | +} |
| 137 | + |
| 138 | +#' List the field names that are available from the ECOTOX database |
| 139 | +#' |
| 140 | +#' List the field names (table headers) that are available from the ECOTOX database |
| 141 | +#' |
| 142 | +#' This can be useful when specifying a \code{\link{search_ecotox}}, to identify which fields |
| 143 | +#' are available from the database, for searching and output. |
| 144 | +#' @param which A \code{character} string that specifies which fields to return. Can be any of: |
| 145 | +#' '\code{default}': returns default output field names; '\code{all}': returns all fields; or |
| 146 | +#' '\code{full}': returns all except fields from table 'dose_response_details'. |
| 147 | +#' @param include_table A \code{logical} value indicating whether the table name should be included |
| 148 | +#' as prefix. Default is \code{TRUE}. |
| 149 | +#' @return Returns a \code{vector} of type \code{character} containing the field names from the ECOTOX database. |
| 150 | +#' @rdname list_ecotox_fields |
| 151 | +#' @name list_ecotox_fields |
| 152 | +#' @examples |
| 153 | +#' ## Fields that are included in search results by default: |
| 154 | +#' list_ecotox_fields("default") |
| 155 | +#' |
| 156 | +#' ## All fields that are available from the ECOTOX database: |
| 157 | +#' list_ecotox_fields("all") |
| 158 | +#' |
| 159 | +#' ## All except fields from the table 'dose_response_details' |
| 160 | +#' ## that are available from the ECOTOX database: |
| 161 | +#' list_ecotox_fields("all") |
| 162 | +#' @author Pepijn de Vries |
| 163 | +#' @export |
| 164 | +list_ecotox_fields <- function(which = c("default", "full", "all"), include_table = TRUE) { |
| 165 | + which <- match.arg(which) |
| 166 | + result <- .db_specs$field_name |
| 167 | + if (include_table) result <- paste(.db_specs$table, result, sep = ".") |
| 168 | + if (which == "default") result <- result[.db_specs$default_output] |
| 169 | + if (which == "full") result <- result[.db_specs$table != "dose_response_details"] |
| 170 | + return(result) |
| 171 | +} |
0 commit comments