Skip to contents

Takes a MB state and returns the current active model

Usage

MB_set_current_element(state, element)

Arguments

state

MB state from MB_fetch_state()

element

Element list from MB_fetch_current_element()

Value

MB state object with the current model set using the supplied value.

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: setting word placeholders: 
#> → ASM:   -> setting docx ph: HEADERLEFT = left header
#> → ASM:   -> setting docx ph: HEADERRIGHT = right header
#> → ASM:   -> setting docx ph: FOOTERLEFT = left footer
#> → 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:f7de2055542d3ff1b085fafc2d30a1f8
#> → 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: a543101a70f40a8eff0529d82698c1bc
#> → MB: module checksum updated:63fff7897ef4a799a577bb192819b838
#>  
#>  
#>  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: bc6d6388344f93e2ef182158365cfa2c
#> → MB: module checksum updated:7a9f6d44cd01df537f00069843458a02
#> → 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: 5bff10708c5ea5af163998802097985f
#> → MB: module checksum updated:b663b8449f51cc38f968a47bcdbaedc8
#> → 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: f8056874dafab1591f98ac775c8085ac
#> → MB: module checksum updated:dd7239ae6fd7e5e0c9ff73aeb799f2f9
#> → 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: c6410535ff7e6fdfce110e4dbd5c602c
#> → MB: module checksum updated:10a54f7598b03eb185f1bd9443d64633
#> → MB: added element idx: 2
#> → MB: module isgood: TRUE
#> ! 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: 1216d9aaed18d15ed7a3bfed61385e8f
#> → MB: module checksum updated:cdfbde013117295a39227c42ea16d66e
#> 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))
#> )
#>