Skip to content

Commit

Permalink
implement pkAnalysisTODataFrame() without saving to csv
Browse files Browse the repository at this point in the history
  • Loading branch information
rengelke committed Nov 22, 2024
1 parent af8ae73 commit 74173fc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export(ospUnits)
export(ospsuiteSettingNames)
export(pkAnalysesAsDataFrame)
export(pkAnalysesToDataFrame)
export(pkAnalysesToDataFrame_)
export(pkAnalysesToTibble)
export(pkParameterByName)
export(plotGrid)
Expand Down
52 changes: 52 additions & 0 deletions R/utilities-pk-analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,58 @@ pkAnalysesToDataFrame <- function(pkAnalyses) {
return(pkAnalysesData)
}

#' @title Convert the pk-Analysis to data frame
#'
#' @param pkAnalyses pK-Analyses to convert to data frame (typically calculated
#' using `calculatePKAnalyses` or imported from file).
#'
#' @export
pkAnalysesToDataFrame_ <- function(pkAnalyses) {
validateIsOfType(pkAnalyses, "SimulationPKAnalyses")
pkAnalysisTask <- .getNetTask("PKAnalysisTask")
pkDataTable <- pkAnalysisTask$call(
"ConvertToDataTable",
pkAnalyses, pkAnalyses$simulation
)

rows <- pkDataTable$call("get_Rows")
rowCount <- rows$call("get_Count")

columns <- pkDataTable$call("get_Columns")
columnCount <- columns$call("get_Count")

columnNames <- sapply(0:(columnCount - 1), function(i) {
columns$call("get_Item", as.integer(i))$call("get_ColumnName")
})

data <- lapply(0:(rowCount - 1), function(j) {
row <- rows$call("get_Item", as.integer(j))
sapply(columnNames, function(colName) {
row$call("get_Item", colName)
})
})

pkAnalysesDf <- dplyr::bind_rows(data) |>
dplyr::mutate(
dplyr::across(dplyr::everything(), ~ gsub('^"|"$', "", .)),
dplyr::across(
dplyr::everything(),
~ dplyr::case_when(
. %in% c("NaN", "", "NA", "Infinity", "-Infinity") ~ NA_character_,
TRUE ~ .
)
),
dplyr::across(dplyr::where(is.character), enc2utf8),
IndividualId = as.integer(IndividualId),
QuantityPath = as.character(QuantityPath),
Parameter = as.character(Parameter),
Value = as.numeric(Value),
Unit = as.character(Unit)
)

return(pkAnalysesDf)
}

#' @rdname pkAnalysesToDataFrame
#'
#' @export
Expand Down

0 comments on commit 74173fc

Please sign in to comment.