Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Imports:
glue,
stringr,
zip,
here
here,
fs
Config/roxygen2/version: 8.0.0
Suggests:
testthat (>= 3.0.0)
Expand Down
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ export(arrangeCdmNames)
export(assertCdmNames)
export(createResultsDir)
export(getPkgZips)
export(setLoggers)
export(unZipStudyFiles)
export(zipStudyFiles)
importFrom(ParallelLogger,logInfo)
importFrom(ParallelLogger,createFileAppender)
importFrom(ParallelLogger,createLogger)
importFrom(ParallelLogger,layoutParallel)
importFrom(ParallelLogger,registerLogger)
importFrom(checkmate,assertCharacter)
importFrom(checkmate,assertDirectoryExists)
importFrom(checkmate,assertFileExists)
Expand All @@ -16,6 +20,7 @@ importFrom(cli,cli_alert_danger)
importFrom(cli,cli_alert_info)
importFrom(cli,cli_alert_success)
importFrom(cli,cli_alert_warning)
importFrom(fs,path_ext_set)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(here,here)
Expand Down
49 changes: 36 additions & 13 deletions R/createResultsDir.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,65 @@
#' \item \code{resultsDirName}: The name of the results subdirectory.
#' }
#'
#' @importFrom ParallelLogger logInfo
#' @importFrom cli cli_alert_info
#' @importFrom checkmate assertDirectoryExists
#' @importFrom glue glue
#'
#' @export
#'
#' @examples
#' outputDir <- file.path(
#' tempdir(),
#' "examples"
#' )
#' directories <- createResultsDir(
#' outputDir,
#' dbname = "OMOP"
#' )
#' unlink(outputDir, recursive = TRUE)
createResultsDir <- function(
outputDir = NULL,
dbname
) {
# Set folder location for results ----
ParallelLogger::logInfo("Setting location for results")
cli::cli_alert_info(
"Creating locations to save results"
)
if (is.null(outputDir)) {
outputDir <- getwd()
checkmate::assertDirectoryExists(outputDir)
checkmate::assertDirectoryExists(
outputDir
)
} else {
if (!dir.exists(outputDir)) {
dir.create(outputDir)
checkmate::assertDirectoryExists(outputDir)
dir.create(
outputDir
)
checkmate::assertDirectoryExists(
outputDir
)
} else {
outputDir <- normalizePath(outputDir)
checkmate::assertDirectoryExists(outputDir)
outputDir <- normalizePath(
outputDir
)
checkmate::assertDirectoryExists(
outputDir
)
}
}

resultsDirName <- glue::glue("results_{dbname}")

resultsDirName <- glue::glue(
"results_{dbname}"
)
resultsDir <- file.path(
outputDir,
resultsDirName
)

if (!dir.exists(resultsDir)) {
dir.create(resultsDir)
}

checkmate::assertDirectoryExists(resultsDir)
checkmate::assertDirectoryExists(
resultsDir
)
return(
list(
outputDir = outputDir,
Expand Down
5 changes: 2 additions & 3 deletions R/getPkgZips.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
#' @export
#'
#' @examples
#' \dontrun{
#' pkg_status_list <- getPkgZips()
#'
#' # Format supp in this way:
#' devtools <- list(Package = "devtools", Version = "2.5.1")
#' duckdb <- list(Package = "duckdb", Version = "1.5.2")
#' supp <- list(Packages = list(devtools = devtools, duckdb = duckdb))
#'
#' pkg_status_list <- getPkgZips(supplement = supp)
#'
#' }
#' @details
#' If the package version is not found under any of the R minor releases, the package will be downloaded from the specified `backupRrel` regardless of package version specified in renv.lock
#' @importFrom renv lockfile_read
Expand Down
115 changes: 115 additions & 0 deletions R/setLoggers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#' `setLoggers()` as text files in the results directory
#'
#' @description
#' Sets `ohdsi/ParallelLogger` log and error report for a study. Logger
#' registered as 'OMOP_STUDY_LOGGER' and error report as 'OMOP_STUDY_ERROR_REPORT'.
#' Please note: avoid using inside a tryCatch() or similar because events
#' will not be 'obsorbed' and not recorded by ParallelLogger.
#'
#' @param resultsDir A valid folder where to save the results of a study.
#' This function will work best with the folder structure formed by
#' `createsResultsDir()`
#' @param logFileName A file name in character. Default 'log'
#' @param errorFileName A file name in character. Default 'error'
#' @param eventLevel TRACE is the default, captures all the output from
#' the console
#' @param errorLevel ERROR is the default, captures errors and fatal events
#'
#' @returns Invisible
#'
#' @importFrom checkmate assertDirectoryExists
#' @importFrom checkmate assertCharacter
#' @importFrom fs path_ext_set
#' @importFrom ParallelLogger registerLogger createLogger createFileAppender layoutParallel
#' @importFrom cli cli_alert_info
#' @importFrom glue glue
#'
#' @export
#'
#' @examples
#' outputDir <- file.path(
#' tempdir(),
#' "examples"
#' )
#' directories <- createResultsDir(
#' outputDir,
#' dbname = "OMOP"
#' )
#' setLoggers(
#' resultsDir = directories$resultsDir
#' )
#' ParallelLogger::clearLoggers()
#' unlink(outputDir, recursive = TRUE)
setLoggers <- function(
resultsDir,
logFileName = "log",
errorFileName = "error",
eventLevel = "TRACE",
errorLevel = "ERROR"
) {

checkmate::assertDirectoryExists(resultsDir)
checkmate::assertCharacter(logFileName)
checkmate::assertCharacter(errorFileName)

stopifnot(
'eventLevel should be one of:
"TRACE", "DEBUG", "INFO", "WARN",
"ERROR", "FATAL"' = eventLevel %in% c(
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
)
)

# Logger ----
logFileLocation <- file.path(
resultsDir,
fs::path_ext_set(
logFileName,
".txt"
)
)
ParallelLogger::registerLogger(
logger <- ParallelLogger::createLogger(
name = "OMOP_STUDY_LOGGER",
threshold = eventLevel,
appenders = list(
ParallelLogger::createFileAppender(
layout = ParallelLogger::layoutParallel,
fileName = logFileLocation
)
)
)
)
ParallelLogger::logInfo(
glue::glue(
"Logger file will be created at: {resultsDir}"
)
)
checkmate::assertFileExists(logFileLocation)

# Error ---------------
errorFileLocation <- file.path(
resultsDir,
fs::path_ext_set(
errorFileName,
".txt"
)
)

ParallelLogger::registerLogger(
ParallelLogger::createLogger(
name = "OMOP_STUDY_ERROR_REPORT",
threshold = errorLevel,
appenders = list(
ParallelLogger::createFileAppender(
layout = ParallelLogger::layoutErrorReport,
fileName = errorFileLocation,
overwrite = TRUE,
expirationTime = 60
)
)
)
)

return(invisible())
}
17 changes: 17 additions & 0 deletions R/unzipStudyFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
#' @importFrom glue glue
#'
#' @export
#'
#' @examples
#' \dontrun{
#' path <- testthat::test_path(
#' "data",
#' "results_execution"
#' )
#' outputDir <- file.path(
#' tempdir(),
#' "examples"
#' )
#' unZipStudyFiles(
#' path = path,
#' outputDir = outputDir
#' )
#' unlink(outputDir, recursive = TRUE)
#' }
unZipStudyFiles <- function(
path,
pattern,
Expand Down
16 changes: 16 additions & 0 deletions R/zipStudyFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
#' @importFrom glue glue
#'
#' @export
#' @examples
#' outputDir <- file.path(
#' tempdir(),
#' "examples"
#' )
#' dbname <- "IPCI"
#' directories <- createResultsDir(
#' outputDir,
#' dbname = dbname
#' )
#' zipStudyFiles(
#' resultsDirName = directories$resultsDir,
#' outputDir = directories$outputDir,
#' dbname = dbname
#' )
#' unlink(outputDir, recursive = TRUE)
zipStudyFiles <- function(
resultsDirName,
outputDir,
Expand Down
11 changes: 11 additions & 0 deletions man/createResultsDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/getPkgZips.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions man/setLoggers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/unZipStudyFiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading