From b216b06d6c492567cbeaf9c2d4a8a3f9422e0475 Mon Sep 17 00:00:00 2001 From: TuomasBorman Date: Sun, 17 Mar 2024 18:06:49 +0200 Subject: [PATCH] up --- DESCRIPTION | 2 +- R/MgnifyClient.R | 41 +++++++++++++++--------------- R/getResult.R | 2 -- man/MgnifyClient.Rd | 12 ++++----- tests/testthat/test-MgnifyClient.R | 2 +- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c9b7c64..574c1de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: MGnifyR Type: Package -Version: 0.99.25 +Version: 0.99.26 Authors@R: c(person(given = "Tuomas", family = "Borman", role = c("aut", "cre"), email = "tuomas.v.borman@utu.fi", diff --git a/R/MgnifyClient.R b/R/MgnifyClient.R index 723a4d8..098249e 100644 --- a/R/MgnifyClient.R +++ b/R/MgnifyClient.R @@ -19,12 +19,10 @@ #' (By default: \code{useCache = FALSE}) #' #' @param cacheDir A single character value specifying a folder to contain the -#' local cache. If NULL, and useCache is TRUE, the new subdirectory -#' \code{.MGnifyR_cache} in the current working directory will be used. Note -#' that cached files are persistent, so the cache directory may be reused -#' between sessions, taking advantage of previously downloaded results. The -#' directory will be created if it doesn't exist already. -#' (By default: \code{cachedir = NULL}) +#' local cache. Note that cached files are persistent, so the cache directory +#' may be reused between sessions, taking advantage of previously downloaded +#' results. The directory will be created if it doesn't exist already. +#' (By default: \code{cacheDir = tempdir()}) #' #' @param showWarnings A single boolean value specifying whether to print #' warnings during invocation of some MGnifyR functions. @@ -66,8 +64,9 @@ NULL #' @importFrom methods new #' @export MgnifyClient <- function( - username = NULL, password = NULL, useCache = FALSE, cacheDir = NULL, - showWarnings = FALSE, verbose = TRUE, clearCache = FALSE, ...){ + username = NULL, password = NULL, useCache = FALSE, + cacheDir = tempdir(), showWarnings = FALSE, verbose = TRUE, + clearCache = FALSE, ...){ ############################### INPUT CHECK ################################ if( !(is.null(username) || .is_non_empty_string(username)) ){ stop( @@ -84,9 +83,9 @@ MgnifyClient <- function( "'useCache' must be a boolean value specifying whether to use ", "on-disk caching.", call. = FALSE) } - if( !(is.null(cacheDir) || .is_non_empty_string(cacheDir)) ){ + if( !.is_non_empty_string(cacheDir) ){ stop( - "'cacheDir' must be NULL or single character value specifying ", + "'cacheDir' must be single character value specifying ", "the the directory for cache.", call. = FALSE) } if( !.is_a_bool(showWarnings) ){ @@ -127,24 +126,26 @@ MgnifyClient <- function( stop("Failed to authenticate.", call. = FALSE) } } - # Assume we're not using it - cachepath <- NA_character_ - # Get the directory where cache will be stored, if cache is used - if (is.null(cacheDir) ){ - cachepath <- file.path(getwd(), ".MGnifyR_cache") - } else{ - cachepath <- cacheDir - } + # Get the directory where cache will be stored. + # If user has specified the subdirectory, ensure that it works in any + # system by adding correct "/". + cacheDir <- as.list(strsplit(cacheDir, "[/\\\\]")[[1]]) + cacheDir <- do.call(file.path, cacheDir) + # Add subdirectory. If user has specified for example working directory, + # the directory would be full of files. This is unintentional. + cacheDir <- file.path(cacheDir, ".MGnifyR_cache") # Make it if needed - assume the user is sensible and the path will # work... - dir.create(cachepath, showWarnings = FALSE) + if( useCache ){ + dir.create(cacheDir, showWarnings = FALSE) + } # Return the final object obj <- new( "MgnifyClient", databaseUrl = url, authTok = authtok, useCache = useCache, - cacheDir = cachepath, + cacheDir = cacheDir, showWarnings = showWarnings, clearCache = clearCache, verbose = verbose diff --git a/R/getResult.R b/R/getResult.R index 047754a..eb4e3e1 100644 --- a/R/getResult.R +++ b/R/getResult.R @@ -496,8 +496,6 @@ setMethod("getResult", signature = c(x = "MgnifyClient"), function( tse <- loadFromBiom( biom_path, removeTaxaPrefixes = TRUE, only.taxa.col = TRUE, rankFromPrefix = TRUE, remove.artifacts = TRUE) - # Remove unnecessary column from taxonomy data - rowData(tse)[["taxonomy_unparsed"]] <- NULL # If the file was not in store already but fetched from database, and cache # storing is disabled if( fetched_from_url && !use.cache ){ diff --git a/man/MgnifyClient.Rd b/man/MgnifyClient.Rd index b093369..0d29ab1 100644 --- a/man/MgnifyClient.Rd +++ b/man/MgnifyClient.Rd @@ -11,7 +11,7 @@ MgnifyClient( username = NULL, password = NULL, useCache = FALSE, - cacheDir = NULL, + cacheDir = tempdir(), showWarnings = FALSE, verbose = TRUE, clearCache = FALSE, @@ -30,12 +30,10 @@ caching of results during this session. In most use cases should be TRUE. (By default: \code{useCache = FALSE})} \item{cacheDir}{A single character value specifying a folder to contain the -local cache. If NULL, and useCache is TRUE, the new subdirectory -\code{.MGnifyR_cache} in the current working directory will be used. Note -that cached files are persistent, so the cache directory may be reused -between sessions, taking advantage of previously downloaded results. The -directory will be created if it doesn't exist already. -(By default: \code{cachedir = NULL})} +local cache. Note that cached files are persistent, so the cache directory +may be reused between sessions, taking advantage of previously downloaded +results. The directory will be created if it doesn't exist already. +(By default: \code{cacheDir = tempdir()})} \item{showWarnings}{A single boolean value specifying whether to print warnings during invocation of some MGnifyR functions. diff --git a/tests/testthat/test-MgnifyClient.R b/tests/testthat/test-MgnifyClient.R index 564e165..85f2671 100644 --- a/tests/testthat/test-MgnifyClient.R +++ b/tests/testthat/test-MgnifyClient.R @@ -44,7 +44,7 @@ test_that("MgnifyClient", { showWarnings = FALSE, url = "test" ) - expect_equal(cacheDir(mg), "test") + expect_equal(cacheDir(mg), "test/.MGnifyR_cache") expect_equal(showWarnings(mg), FALSE) expect_equal(databaseUrl(mg), "test") mg <- MgnifyClient(