Takes an rxode2 object and updates the model components of the current element.
Arguments
- state
MB state from
MB_fetch_state()
- session
Shiny session variable
- current_ele
MB model element from
MB_fetch_current_element()
- rx_obj
rxode2 model from
rxode2::rxode2()
- note
text indicating what this update does (e.g. "added parameter")
- reset
boolean indicating that the element needs to be reset (i.e. if you change the base model) default:
FALSE
.
Examples
#library(ruminate)
# This will get the full session:
library(formods)
if( Sys.getenv("ruminate_rxfamily_found") == "TRUE"){
sess_res = MB_test_mksession()
# This is just for CRAN
#sess_res = MB_test_mksession(session=list())
session = sess_res$session
input = sess_res$input
# Configuration files
FM_yaml_file = system.file(package = "formods", "templates", "formods.yaml")
MOD_yaml_file = system.file(package = "ruminate", "templates", "MB.yaml")
# Creating an empty state object
state = MB_fetch_state(id = "MB",
input = input,
session = session,
FM_yaml_file = FM_yaml_file,
MOD_yaml_file = MOD_yaml_file,
react_state = NULL)
# This will provide a list of the available models
models = MB_fetch_catalog(state)
# This is a summary of the tables in the model:
models$summary
# This will test the models in the catalog, set as_cran
# to FALSE to test all the models.
mtres = MB_test_catalog(state, as_cran=TRUE)
mtres$isgood
# Creates a new empty element
state = MB_new_element(state)
# Delete the current element
state = MB_del_current_element(state)
# Fetch a list of the current element
element = MB_fetch_current_element(state)
# This will attach a model to it:
# Pulling the first model from the catalog
fcn_def = models[["summary"]][1, ][["Model"]]
fcn_obj = models[["summary"]][1, ][["Object"]]
mdl_type = models[["summary"]][1, ][["Type"]]
fcn_desc = models[["summary"]][1, ][["Description"]]
# This will build the rxode2 object from the model
mk_rx_res = mk_rx_obj(
type = mdl_type,
model = list(fcn_def = fcn_def,
fcn_obj = fcn_obj))
# This will attach the model to the current element
element = MB_update_model(
state = state,
session = session,
current_ele = element,
rx_obj = mk_rx_res[["capture"]][["rx_obj"]],
note = fcn_desc,
reset = TRUE)
# You can now place element back in the state
state = MB_set_current_element(state, element)
# This will fetch the current component
component = MB_fetch_component(state, element)
fares = MB_fetch_appends(state, element)
# You can use the component to build the code to generate the model:
gen_code =
MB_build_code(state = state, session = session,
fcn_def = component[["fcn_def"]],
time_scale = element[["ui"]][["time_scale"]],
fcn_obj_name = "my_fcn_obj",
rx_obj_name = "my_obj_name",
ts_obj_name = "my_ts_name")
# Model code to be included in a larger script
message(paste0(gen_code$model_code, collapse="\n"))
# Stand-alone model code
message(paste0(gen_code$model_code_sa, collapse="\n"))
# This will fetch the code to regenerate all of the components of this module
message(MB_fetch_code(state))
}
#> → ASM: including file
#> → ASM: source: file.path(system.file(package="onbrand"), "templates", "report.docx")
#> → ASM: dest: file.path("config","report.docx")
#> → ASM: including file
#> → ASM: source: file.path(system.file(package="onbrand"), "templates", "report.pptx")
#> → ASM: dest: file.path("config","report.pptx")
#> → ASM: including file
#> → ASM: source: file.path(system.file(package="onbrand"), "templates", "report.yaml")
#> → ASM: dest: file.path("config","report.yaml")
#> → ASM: State initialized
#> → ASM: module isgood: TRUE
#> → MB: including file
#> → MB: source: file.path(system.file(package="onbrand"), "templates", "report.docx")
#> → MB: dest: file.path("config","report.docx")
#> → MB: including file
#> → MB: source: file.path(system.file(package="onbrand"), "templates", "report.pptx")
#> → MB: dest: file.path("config","report.pptx")
#> → MB: including file
#> → MB: source: file.path(system.file(package="onbrand"), "templates", "report.yaml")
#> → MB: dest: file.path("config","report.yaml")
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.R not found (skipping)
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.ctl not found (skipping)
#> → MB: module checksum updated:24933f86b657b9503f22440e8c4d3cac
#> → MB: State initialized
#> → MB: loading model idx: 1
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> → MB: model checksum updated: 03794438dbfd3a3a27eebc39ad436203
#> → MB: module checksum updated:34a469deda5568fc0e260a6afd1891f3
#> → MB: setting name: One compartment model
#> → MB: setting time scale: hours
#> → MB: setting base from: user
#> → MB: setting catalog selection:
#> → MB: setting base model id: manual
#> → MB: setting base model name: manual
#> → MB: model checksum updated: cc835f1a733e62e5519b3a962ae0ae89
#> → MB: module checksum updated:927a0ae0a0cdc1138d0d7d9fa0c05836
#> → MB: added element idx: 1
#> → MB: loading model idx: 2
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> → MB: model checksum updated: fc406d7985a63a9d9180ee8cf5078467
#> → MB: module checksum updated:0022b3bca2d4246a8138498d608566b0
#> → MB: setting name: PK Biomarker
#> → MB: setting time scale: days
#> → MB: setting base from: user
#> → MB: setting catalog selection:
#> → MB: setting base model id: manual
#> → MB: setting base model name: manual
#> → MB: model checksum updated: 24cee75002f07a3b3385b9a9a478760c
#> → MB: module checksum updated:b0996670161add364a6078a8c7872849
#> → MB: added element idx: 2
#> Called from: MB_preload(session = session, src_list = src_list, yaml_res = yaml_res,
#> mod_ID = mod_ID, react_state = react_state, quickload = quickload)
#> debug: formods::FM_le(state, paste0("module isgood: ", isgood))
#> → MB: module isgood: TRUE
#> debug: if (("ShinySession" %in% class(session))) {
#> FM_set_mod_state(session, mod_ID, state)
#> } else {
#> session = FM_set_mod_state(session, mod_ID, state)
#> }
#> debug: session = FM_set_mod_state(session, mod_ID, state)
#> debug: res = list(isgood = isgood, msgs = msgs, session = session, input = input,
#> react_state = react_state, state = state)
#> debug: res
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.R not found (skipping)
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.ctl not found (skipping)
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.R not found (skipping)
#> ! MB: User-defined model: /Users/jmh/projects/ruminate/github/ruminate/docs/reference/user_model.ctl not found (skipping)
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> ✔ MB: PK_1cmt
#>
#>
#> ℹ parameter labels from comments are typically ignored in non-interactive mode
#> ℹ Need to run with the source intact to parse comments
#> → MB: model checksum updated: 5bd986a71bb83ce58d1918c6acbf4f1d
#> → MB: module checksum updated:1f8546bb45a252684ba8f3b50d35136f
#> my_fcn_obj = function ()
#> {
#> description <- "One compartment PK model with linear clearance"
#> ini({
#> lka <- 0.45
#> label("Absorption rate (Ka)")
#> lcl <- 1
#> label("Clearance (CL)")
#> lvc <- 3.45
#> label("Central volume of distribution (V)")
#> propSd <- c(0, 0.5)
#> label("Proportional residual error (fraction)")
#> })
#> model({
#> ka <- exp(lka)
#> cl <- exp(lcl)
#> vc <- exp(lvc)
#> Cc <- linCmt()
#> Cc ~ prop(propSd)
#> })
#> }
#>
#> my_obj_name = rxode2::rxode2(my_fcn_obj)
#>
#> my_ts_name = list(
#> system = "weeks",
#> details =
#> list(months = list(verb = "Months", conv = 4.13359788359788e-07),
#> weeks = list(verb = "Weeks", conv = 1.65343915343915e-06),
#> days = list(verb = "Days", conv = 1.15740740740741e-05),
#> hours = list(verb = "Hours", conv = 0.000277777777777778))
#> )
#> library("nlmixr2lib")
#> library("nonmem2rx")
#> library("rxode2")
#>
#> my_fcn_obj = function ()
#> {
#> description <- "One compartment PK model with linear clearance"
#> ini({
#> lka <- 0.45
#> label("Absorption rate (Ka)")
#> lcl <- 1
#> label("Clearance (CL)")
#> lvc <- 3.45
#> label("Central volume of distribution (V)")
#> propSd <- c(0, 0.5)
#> label("Proportional residual error (fraction)")
#> })
#> model({
#> ka <- exp(lka)
#> cl <- exp(lcl)
#> vc <- exp(lvc)
#> Cc <- linCmt()
#> Cc ~ prop(propSd)
#> })
#> }
#>
#> my_obj_name = rxode2::rxode2(my_fcn_obj)
#>
#> my_ts_name = list(
#> system = "weeks",
#> details =
#> list(months = list(verb = "Months", conv = 4.13359788359788e-07),
#> weeks = list(verb = "Weeks", conv = 1.65343915343915e-06),
#> days = list(verb = "Days", conv = 1.15740740740741e-05),
#> hours = list(verb = "Hours", conv = 0.000277777777777778))
#> )
#> # Model Building ----
#> # Base model: manual====
#> MB_obj_1_fcn = function ()
#> {
#> description <- "One compartment PK model with linear clearance"
#> ini({
#> lka <- 0.45
#> label("Absorption rate (Ka)")
#> lcl <- 1
#> label("Clearance (CL)")
#> lvc <- 3.45
#> label("Central volume of distribution (V)")
#> propSd <- c(0, 0.5)
#> label("Proportional residual error (fraction)")
#> })
#> model({
#> ka <- exp(lka)
#> cl <- exp(lcl)
#> vc <- exp(lvc)
#> Cc <- linCmt()
#> Cc ~ prop(propSd)
#> })
#> }
#>
#> MB_obj_1_rx = rxode2::rxode2(MB_obj_1_fcn)
#>
#> MB_obj_1_ts = list(
#> system = "weeks",
#> details =
#> list(months = list(verb = "Months", conv = 4.13359788359788e-07),
#> weeks = list(verb = "Weeks", conv = 1.65343915343915e-06),
#> days = list(verb = "Days", conv = 1.15740740740741e-05),
#> hours = list(verb = "Hours", conv = 0.000277777777777778))
#> )
#>
#>
#> # Base model: manual====
#> MB_obj_2_fcn = function ()
#> {
#> ini({
#> TV_F1 <- fix(2.22044604925031e-16, 0.744, Inf)
#> TV_ka <- c(2.22044604925031e-16, 0.282, Inf)
#> TV_CL <- c(-36.0436533891172, -1.6094379124341, 709.782712893384)
#> TV_Vc <- c(-36.0436533891172, 1.28370777234479, 709.782712893384)
#> TV_Vp <- c(-36.0436533891172, 1.01160091167848, 709.782712893384)
#> TV_Q <- c(-36.0436533891172, -0.29169009384932, 709.782712893384)
#> TV_MW <- fix(2.22044604925031e-16, 140, Inf)
#> TV_BM_IC <- c(2.22044604925031e-16, 1000, Inf)
#> TV_kdeg_BM <- fix(2.22044604925031e-16, 0.1, Inf)
#> TV_Emax <- c(2.22044604925031e-16, 100, Inf)
#> TV_EC50 <- fix(2.22044604925031e-16, 300, Inf)
#> prop_err_PK <- c(2.22044604925031e-16, 0.1, Inf)
#> add_err_PK <- c(2.22044604925031e-16, 0.1, Inf)
#> add_err_BM <- c(2.22044604925031e-16, 0.1, Inf)
#> ETAka ~ 0.416
#> ETACL ~ 0.09875
#> ETAVc ~ c(0.0786, 0.116)
#> ETAVp ~ c(0.0619, 0.0377, 0.0789)
#> ETAQ ~ 0.699
#> ETABM_IC ~ 0.05
#> ETAEmax ~ 0.1
#> })
#> model({
#> F1 = TV_F1
#> ka = TV_ka * exp(ETAka)
#> CL = exp(TV_CL) * exp(ETACL)
#> Vc = exp(TV_Vc) * exp(ETAVc)
#> Cc = Ac/(Vc)
#> Vp = exp(TV_Vp) * exp(ETAVp)
#> Q = exp(TV_Q) * exp(ETAQ)
#> MW = TV_MW
#> BM_IC = TV_BM_IC * exp(ETABM_IC)
#> kdeg_BM = TV_kdeg_BM
#> Emax = TV_Emax * exp(ETAEmax)
#> EC50 = TV_EC50
#> Dinf = 0
#> WTTV = 70
#> CL_IND = CL * (1 + SEX_ID * 0.1) * (1 + SUBTYPE_ID *
#> 0.1)
#> kel = CL_IND/Vc * ((WT/WTTV))^(-0.35)
#> kcp = Q/Vc * ((WT/WTTV))^(-0.35)
#> kpc = Q/Vp * ((WT/WTTV))^(-0.35)
#> ksyn_BM = kdeg_BM * BM_IC
#> STIM = 1 + Emax * Cc/(EC50 + Cc)
#> BM(0) = BM_IC
#> d/dt(At) = (-ka * At)
#> d/dt(Ac) = ((ka * At * F1/Vc - kel * Cc - kcp * Cc +
#> kpc * Cp * Vp/Vc + Dinf/Vc)) * Vc
#> d/dt(Cp) = (kcp * Cc * Vc/Vp - kpc * Cp)
#> d/dt(BM) = ksyn_BM * STIM - (kdeg_BM * BM)
#> C_ng_ml = Cc * MW
#> C_ng_ml ~ add(add_err_PK) + prop(prop_err_PK)
#> BM_obs = BM
#> BM_obs ~ add(add_err_BM)
#> })
#> }
#>
#> MB_obj_2_rx = rxode2::rxode2(MB_obj_2_fcn)
#>
#> MB_obj_2_ts = list(
#> system = "weeks",
#> details =
#> list(months = list(verb = "Months", conv = 4.13359788359788e-07),
#> weeks = list(verb = "Weeks", conv = 1.65343915343915e-06),
#> days = list(verb = "Days", conv = 1.15740740740741e-05),
#> hours = list(verb = "Hours", conv = 0.000277777777777778))
#> )
#>