mirror of
https://github.com/msberends/AMR.git
synced 2025-07-08 13:21:50 +02:00
(v1.1.0.9019) mo_source fix
This commit is contained in:
@ -22,7 +22,7 @@
|
||||
#' Property of an antibiotic
|
||||
#'
|
||||
#' Use these functions to return a specific property of an antibiotic from the [antibiotics] data set. All input values will be evaluated internally with [as.ab()].
|
||||
#' @inheritSection lifecycle Maturing lifecycle
|
||||
#' @inheritSection lifecycle Stable lifecycle
|
||||
#' @param x any (vector of) text that can be coerced to a valid microorganism code with [as.ab()]
|
||||
#' @param tolower logical to indicate whether the first character of every output should be transformed to a lower case character. This will lead to e.g. "polymyxin B" and not "polymyxin b".
|
||||
#' @param property one of the column names of one of the [antibiotics] data set
|
||||
@ -33,7 +33,7 @@
|
||||
#' @param ... other parameters passed on to [as.ab()]
|
||||
#' @details All output will be [translate]d where possible.
|
||||
#'
|
||||
#' The function [ab_url()] will return the direct URL to the official WHO website. A warning will be returned if the reauired ATC code is not available.
|
||||
#' The function [ab_url()] will return the direct URL to the official WHO website. A warning will be returned if the required ATC code is not available.
|
||||
#' @inheritSection as.ab Source
|
||||
#' @rdname ab_property
|
||||
#' @name ab_property
|
||||
|
@ -22,6 +22,7 @@
|
||||
#' Property of a microorganism
|
||||
#'
|
||||
#' Use these functions to return a specific property of a microorganism. All input values will be evaluated internally with [as.mo()], which makes it possible to use microbial abbreviations, codes and names as input. Please see *Examples*.
|
||||
#' @inheritSection lifecycle Stable lifecycle
|
||||
#' @param x any (vector of) text that can be coerced to a valid microorganism code with [as.mo()]
|
||||
#' @param property one of the column names of the [microorganisms] data set or `"shortname"`
|
||||
#' @param language language of the returned text, defaults to system language (see [get_locale()]) and can also be set with `getOption("AMR_locale")`. Use `language = NULL` or `language = ""` to prevent translation.
|
||||
|
@ -19,13 +19,13 @@
|
||||
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
|
||||
# ==================================================================== #
|
||||
|
||||
#' Use predefined reference data set
|
||||
#' User-defined reference data set for microorganisms
|
||||
#'
|
||||
#' @description These functions can be used to predefine your own reference to be used in [as.mo()] and consequently all `mo_*` functions like [mo_genus()] and [mo_gramstain()].
|
||||
#'
|
||||
#' This is **the fastest way** to have your organisation (or analysis) specific codes picked up and translated by this package.
|
||||
#' @inheritSection lifecycle Stable lifecycle
|
||||
#' @param path location of your reference file, see Details
|
||||
#' @param path location of your reference file, see Details. Can be `""`, `NULL` or `FALSE` to delete the reference file.
|
||||
#' @rdname mo_source
|
||||
#' @name mo_source
|
||||
#' @aliases set_mo_source get_mo_source
|
||||
@ -35,11 +35,12 @@
|
||||
#'
|
||||
#' [get_mo_source()] will return the data set by reading `"~/.mo_source.rds"` with [readRDS()]. If the original file has changed (the file defined with `path`), it will call [set_mo_source()] to update the data file automatically.
|
||||
#'
|
||||
#' Reading an Excel file (`.xlsx`) with only one row has a size of 8-9 kB. The compressed file used by this package will have a size of 0.1 kB and can be read by [get_mo_source()] in only a couple of microseconds (a millionth of a second).
|
||||
#' Reading an Excel file (`.xlsx`) with only one row has a size of 8-9 kB. The compressed file created with [set_mo_source()] will then have a size of 0.1 kB and can be read by [get_mo_source()] in only a couple of microseconds (millionths of a second).
|
||||
#'
|
||||
#' ## How it works
|
||||
#' @section How to setup:
|
||||
#'
|
||||
#' Imagine this data on a sheet of an Excel file (mo codes were looked up in the [microorganisms] data set). The first column contains the organisation specific codes, the second column contains an MO code from this package:
|
||||
#'
|
||||
#' Imagine this data on a sheet of an Excel file (mo codes were looked up in the `microorganisms` data set). The first column contains the organisation specific codes, the second column contains an MO code from this package:
|
||||
#' ```
|
||||
#' | A | B |
|
||||
#' --|--------------------|--------------|
|
||||
@ -50,27 +51,31 @@
|
||||
#' ```
|
||||
#'
|
||||
#' We save it as `"home/me/ourcodes.xlsx"`. Now we have to set it as a source:
|
||||
#'
|
||||
#' ```
|
||||
#' set_mo_source("home/me/ourcodes.xlsx")
|
||||
#' # Created mo_source file '~/.mo_source.rds' from 'home/me/ourcodes.xlsx'.
|
||||
#' #> NOTE: Created mo_source file '~/.mo_source.rds' from 'home/me/ourcodes.xlsx'
|
||||
#' #> (columns "Organisation XYZ" and "mo")
|
||||
#' ```
|
||||
#'
|
||||
#' It has now created a file `"~/.mo_source.rds"` with the contents of our Excel file, but only the first column with foreign values and the 'mo' column will be kept.
|
||||
#' It has now created a file `"~/.mo_source.rds"` with the contents of our Excel file. Only the first column with foreign values and the 'mo' column will be kept when creating the RDS file.
|
||||
#'
|
||||
#' And now we can use it in our functions:
|
||||
#'
|
||||
#' ```
|
||||
#' as.mo("lab_mo_ecoli")
|
||||
#' [1] B_ESCHR_COLI
|
||||
#' #> [1] B_ESCHR_COLI
|
||||
#'
|
||||
#' mo_genus("lab_mo_kpneumoniae")
|
||||
#' [1] "Klebsiella"
|
||||
#' #> [1] "Klebsiella"
|
||||
#'
|
||||
#' # other input values still work too
|
||||
#' as.mo(c("Escherichia coli", "E. coli", "lab_mo_ecoli"))
|
||||
#' [1] B_ESCHR_COLI B_ESCHR_COLI B_ESCHR_COLI
|
||||
#' #> [1] B_ESCHR_COLI B_ESCHR_COLI B_ESCHR_COLI
|
||||
#' ```
|
||||
#'
|
||||
#' If we edit the Excel file to, let's say, by adding row 4 like this:
|
||||
#' If we edit the Excel file by, let's say, adding row 4 like this:
|
||||
#'
|
||||
#' ```
|
||||
#' | A | B |
|
||||
#' --|--------------------|--------------|
|
||||
@ -82,36 +87,41 @@
|
||||
#' ```
|
||||
#'
|
||||
#' ...any new usage of an MO function in this package will update your data file:
|
||||
#'
|
||||
#' ```
|
||||
#' as.mo("lab_mo_ecoli")
|
||||
#' # Updated mo_source file '~/.mo_source.rds' from 'home/me/ourcodes.xlsx'.
|
||||
#' [1] B_ESCHR_COLI
|
||||
#' #> NOTE: Updated mo_source file '~/.mo_source.rds' from 'home/me/ourcodes.xlsx'
|
||||
#' #> (columns "Organisation XYZ" and "mo")
|
||||
#' #> [1] B_ESCHR_COLI
|
||||
#'
|
||||
#' mo_genus("lab_Staph_aureus")
|
||||
#' [1] "Staphylococcus"
|
||||
#' #> [1] "Staphylococcus"
|
||||
#' ```
|
||||
#'
|
||||
#' To remove the reference data file completely, just use `""` or `NULL` as input for `[set_mo_source()]`:
|
||||
#' To delete the reference data file, just use `""`, `NULL` or `FALSE` as input for [set_mo_source()]:
|
||||
#'
|
||||
#' ```
|
||||
#' set_mo_source(NULL)
|
||||
#' # Removed mo_source file '~/.mo_source.rds'.
|
||||
#' ```
|
||||
#'
|
||||
#' If the original Excel file is moved or deleted, the mo_source file will be removed upon the next use of [as.mo()]. If the mo_source file is manually deleted (i.e. without using [set_mo_source()]), the references to the mo_source file will be removed upon the next use of [as.mo()].
|
||||
#' @export
|
||||
#' @inheritSection AMR Read more on our website!
|
||||
set_mo_source <- function(path) {
|
||||
|
||||
file_location <- path.expand("~/mo_source.rds")
|
||||
|
||||
if (!is.character(path) | length(path) > 1) {
|
||||
stop("`path` must be a character of length 1.")
|
||||
if (length(path) > 1) {
|
||||
stop("`path` must be of length 1.")
|
||||
}
|
||||
|
||||
if (path %in% c(NULL, "")) {
|
||||
if (is.null(path) || path %in% c(FALSE, "")) {
|
||||
options(mo_source = NULL)
|
||||
options(mo_source_timestamp = NULL)
|
||||
if (file.exists(file_location)) {
|
||||
unlink(file_location)
|
||||
message("Removed mo_source file '", file_location, "'.")
|
||||
message(font_red(paste0("Removed mo_source file '", font_bold(file_location), "'")))
|
||||
}
|
||||
return(invisible())
|
||||
}
|
||||
@ -154,13 +164,13 @@ set_mo_source <- function(path) {
|
||||
# check integrity
|
||||
mo_source_isvalid(df)
|
||||
|
||||
df <- df %>% filter(!is.na(mo))
|
||||
df <- subset(df, !is.na(mo))
|
||||
|
||||
# keep only first two columns, second must be mo
|
||||
if (colnames(df)[1] == "mo") {
|
||||
df <- df[, c(2, 1)]
|
||||
df <- df[, c(colnames(df)[2], "mo")]
|
||||
} else {
|
||||
df <- df[, c(1, 2)]
|
||||
df <- df[, c(colnames(df)[1], "mo")]
|
||||
}
|
||||
|
||||
df <- as.data.frame(df, stringAsFactors = FALSE)
|
||||
@ -174,30 +184,40 @@ set_mo_source <- function(path) {
|
||||
saveRDS(df, file_location)
|
||||
options(mo_source = path)
|
||||
options(mo_source_timestamp = as.character(file.info(path)$mtime))
|
||||
message(action, " mo_source file '", file_location, "' from '", path, "'.")
|
||||
message(font_blue(paste0("NOTE: ",
|
||||
action, " mo_source file '", font_bold(file_location), "'",
|
||||
" from '", font_bold(path), "'",
|
||||
'\n (columns "', colnames(df)[1], '" and "', colnames(df)[2], '")')))
|
||||
}
|
||||
|
||||
#' @rdname mo_source
|
||||
#' @export
|
||||
get_mo_source <- function() {
|
||||
if (is.null(getOption("mo_source", NULL))) {
|
||||
NULL
|
||||
} else {
|
||||
old_time <- as.POSIXct(getOption("mo_source_timestamp"))
|
||||
new_time <- as.POSIXct(as.character(file.info(getOption("mo_source", ""))$mtime))
|
||||
|
||||
if (is.na(new_time)) {
|
||||
# source file was deleted, remove reference too
|
||||
set_mo_source("")
|
||||
return(NULL)
|
||||
}
|
||||
if (new_time != old_time) {
|
||||
# set updated source
|
||||
set_mo_source(getOption("mo_source"))
|
||||
}
|
||||
file_location <- path.expand("~/mo_source.rds")
|
||||
readRDS(file_location)
|
||||
return(NULL)
|
||||
}
|
||||
|
||||
if (!file.exists(path.expand("~/mo_source.rds"))) {
|
||||
options(mo_source = NULL)
|
||||
options(mo_source_timestamp = NULL)
|
||||
message(font_blue("NOTE: Removed references to deleted mo_source file (see ?mo_source)"))
|
||||
return(NULL)
|
||||
}
|
||||
|
||||
old_time <- as.POSIXct(getOption("mo_source_timestamp"))
|
||||
new_time <- as.POSIXct(as.character(file.info(getOption("mo_source", ""))$mtime))
|
||||
|
||||
if (is.na(new_time)) {
|
||||
# source file was deleted, remove reference too
|
||||
set_mo_source("")
|
||||
return(NULL)
|
||||
}
|
||||
if (new_time != old_time) {
|
||||
# set updated source
|
||||
set_mo_source(getOption("mo_source"))
|
||||
}
|
||||
file_location <- path.expand("~/mo_source.rds")
|
||||
readRDS(file_location)
|
||||
}
|
||||
|
||||
mo_source_isvalid <- function(x, refer_to_name = "`reference_df`", stop_on_error = TRUE) {
|
||||
|
Reference in New Issue
Block a user