Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to not run simulations when converting snapshots #1488

Merged
merged 17 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
27 changes: 24 additions & 3 deletions R/snapshots.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ runSimulationsFromSnapshot <- function(..., output = ".", exportCSV = TRUE, expo
#' @param ... character strings, path to files or a directory containing files to convert
#' @param format, character string, target format either "snapshot" or "project".
#' @param output character string, path to the output directory where to write the converted files
#' @param runSimulations logical, whether to run simulations during conversion (default = FALSE).
#' Only when converting from snapshot to project.
#'
#' @return NULL
#' @export
Expand All @@ -104,7 +106,7 @@ runSimulationsFromSnapshot <- function(..., output = ".", exportCSV = TRUE, expo
#' convertSnapshot("path/to/snapshot.json", format = "project")
#' convertSnapshot("path/to/project.pksim5", format = "snapshot")
#' }
convertSnapshot <- function(..., format, output = ".") {
convertSnapshot <- function(..., format, output = ".", runSimulations = FALSE) {
rlang::arg_match(arg = format, values = c("snapshot", "project"))

initPKSim()
Expand All @@ -115,6 +117,12 @@ convertSnapshot <- function(..., format, output = ".") {
SnapshotRunOptions$set(name = "InputFolder", value = temp_dir)
SnapshotRunOptions$set(name = "OutputFolder", value = normalizePath(output))

if (isTRUE(runSimulations)) {
SnapshotRunOptions$set(name = "RunSimulations", value = TRUE)
} else {
SnapshotRunOptions$set(name = "RunSimulations", value = FALSE)
}

if (format == "project") {
SnapshotRunOptions$set("ExportMode", 0L)
nfiles <- length(list.files(temp_dir, pattern = ".json"))
Expand All @@ -124,12 +132,25 @@ convertSnapshot <- function(..., format, output = ".") {
}

cli::cli_process_start(
msg = "Converting {nfiles} files{?s} to {format} format",
msg = "Converting {nfiles} file{?s} to {format} format",
msg_done = "Conversion completed",
msg_failed = "An error occured while converting files"
)

rSharp::callStatic("PKSim.R.Api", "RunSnapshot", SnapshotRunOptions)
tryCatch(
{
invisible(rSharp::callStatic("PKSim.R.Api", "RunSnapshot", SnapshotRunOptions))
},
error = function(e) {
message <- stringr::str_extract(as.character(e), "(?<=Message: )[^\\n]*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some error messages won't have a message line of format "Message: *". In this case str_extract() returns in NA resulting in

Error in `convertSnapshot()`:
! NA
✖ An error occured while converting files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I added an if case to handle this.


if (is.na(message){
message <- e
})

cli::cli_abort(message = message, call = rlang::caller_env(n = 4))
}
)
}


Expand Down
Loading