Skip to contents

Fetches the code to generate results seen in the app

Usage

MB_fetch_code(state)

Arguments

state

MB state from MB_fetch_state()

Value

Character object vector with the lines of code

Examples

#library(ruminate)
# This will get the full session:
library(formods)
if( Sys.getenv("ruminate_rxfamily_found") == "TRUE"){
  sess_res = MB_test_mksession(session=list(), full_session=TRUE)
  # 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))
}
#> → 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: 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: model checksum updated: 98407146cbcfb0e5392b7bf47aeb1d09
#> → MB: module checksum updated:a317b4031c8ca91b0c52f2a06412fe69
#>  
#>  
#>  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: 99b8ba1b35abab95a3b996e9a746e957
#> → MB: module checksum updated:73f9beb51e56ccf966b8875d8b341812
#> ! 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: 7868cad93a54cc04bddb2f453d86688f
#> → MB: module checksum updated:400b526b0c5f4b68c51a507d4ebc95bd
#> 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: user====
#> 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))
#> )
#>