---
title: "Get started with CAMPSIS model"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Get started with CAMPSIS model}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

### Load example from model library

First import the `campsismod` package. This step is not required if you have already loaded the `campsis` package.

```{r, message=FALSE}
library(campsismod)
```

Load 2-compartment PK model from built-in model library and show content.

```{r}
model <- model_suite$pk$`2cpt_fo`
show(model)
```

### Write CAMPSIS model

A CAMPSIS model can be persisted on your local drive as follows:

```{r}
model %>% write(file="path_to_model_folder")
list.files("path_to_model_folder")
```

As shown, the output directory will contain the model (all code records) and 1 `csv` file per type of parameter (THETA, OMEGA and SIGMA). 

### Read CAMPSIS model

To read a CAMPSIS model from your local drive, use the `read.campsis` function.
The exact same model should be retrieved.

```{r}
model <- read.campsis(file="path_to_model_folder")
show(model)
```

The `MAIN` record is the part of the model where your model parameters are defined. The `ODE` record is where your ordinary differential equations (ODE) go, as well as any equation depending on the simulation time. The `ERROR` record is the place where the error model is defined. The model parameters are then shown, followed by the all the compartments.
 
### Export CAMPSIS model to RxODE

`campsismod` has powerful export capabilities to `RxODE` and `mrgsolve`, the 2 simulation engines supported by `campsis`.
The following code exports the model to `RxODE`. Please note that this step is implicit in CAMPSIS when you call the `simulate` method with your preferred simulation engine.

```{r}
rxmod <- model %>% export(dest="RxODE")
rxmod
```

### Export CAMPSIS model to mrgsolve

The following code exports the model to `mrgsolve` (text form).

```{r}
mrgmod <- model %>% export(dest="mrgsolve")
mrgmod
```
