R script: inline.R
Instead of using an external text file for the model, it is possible to define the model inline, i.e. in the R
script with the command
myModel <- inlineModel("
[LONGITUDINAL]
...
")
This model can then be used as any model Mlxtran:
res <- simulx( model=myModel, ... )
The syntax for implementing the model is exactly the same: you can copy and paste any Mlxtran model in the R
script, in the comment area defined by ("
and ")
. The only difference is that all the information required for performing a given simulation is in one single R
file.
Function inlineModel creates a temporary model file filename. Default name is “tempModel.txt”. filename=“random” generates a random name.
myModel1 <- inlineModel("
[LONGITUDINAL]
input = {ka, V, k, a}
EQUATION:
D = 100
f = D*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))
DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
")
print(myModel1)
## $filename
## [1] "tempModel.txt"
##
## $str
## [1] "\n[LONGITUDINAL]\ninput = {ka, V, k, a}\n\nEQUATION:\nD = 100\nf = D*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))\n\nDEFINITION:\ny = {distribution=normal, prediction=f, sd=a}\n"
We can now use myModel1.txt with simulx:
f <- list(name='f', time=seq(0, 30, by=0.1))
y <- list(name='y', time=seq(0, 30, by=2))
res <- simulx(model = myModel1,
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output = list(f, y))
plot(ggplot(data=res$f, aes(x=time, y=f)) + geom_line(size=0.5) +
geom_point(data=res$y, aes(x=time, y=y), colour="red"))
filename=“random” creates a tempory model file with a random name. This may be useful when several people use siultaneously the same Shiny app for instance.
myModel2 <- inlineModel("
[LONGITUDINAL]
input = {ka, V, k, a}
EQUATION:
f = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))
DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
", filename="random")
print(myModel2)
## $filename
## [1] "tempModel_0be81a5d-1d0b-42c5-b89c-d9076b7f1f7f.txt"
##
## $str
## [1] "\n[LONGITUDINAL]\ninput = {ka, V, k, a}\n\nEQUATION:\nf = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))\n\nDEFINITION:\ny = {distribution=normal, prediction=f, sd=a}\n"
res <- simulx(model = myModel2,
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output = list(f, y))
Obviously, the model can be written directly in any text file using basic R command:
model.str <- "
[LONGITUDINAL]
input = {ka, V, k, a}
EQUATION:
f = 100*ka/(V*(ka-k))*(exp(-k*t) - exp(-ka*t))
DEFINITION:
y = {distribution=normal, prediction=f, sd=a}
"
write(model.str,"pk_model.txt")
res <- simulx(model = "pk_model.txt",
parameter = c(ka=0.5, V=10, k=0.2, a=0.3),
output = list(f, y))