R/read_model_json.R

Defines functions read_model_json

Documented in read_model_json

#' Read model definition from JSON
#'
#' Does some substitution of escaped characters in strings in the JSON file,
#' then converts to a list with [jsonlite::fromJSON()]
#'
#' @param path Path to JSON file
#' @md
#' @export
#' @return List containing contents of original JSON file
read_model_json <- function(
  path
) {
  if(!file.exists(path)) {
    warning(paste0("Model information not found (", path, ")."))
    return()
  }
  ## Convert the multi-line objects in JSON5 to conventional JSON
  lines <- paste(readLines(path), collapse = "\n") %>%
    stringr::str_replace_all("'", "\"") %>%
    stringr::str_replace_all("\\\\n", "\n") %>%
    stringr::str_replace_all("\n", "") %>%
    stringr::str_replace_all("\\\\", "\\\\n")
  jsonlite::fromJSON(lines)
}

Try the PKPDsim package in your browser

Any scripts or data that you put into this service are public.

PKPDsim documentation built on March 7, 2023, 5:40 p.m.