Skip to content

Commit dd1b2a2

Browse files
Merge pull request #46 from mattfidler/nlmixr2-verse
Change for nlmixr2-verse style package
2 parents 279c0ca + 3bb24e4 commit dd1b2a2

13 files changed

Lines changed: 91 additions & 85 deletions

DESCRIPTION

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Authors@R: c(person("Richard", "Hooijmaijers", email = "richardhooijmaijers@gmai
66
person("LAPP Consultants",email = "info@lapp.nl", role=c("fnd","cph")),
77
person("Matthew Fidler",role=c("ctb")),
88
person("Veerle van Leemput",role=c("ctb")))
9-
Description: An R shiny user interface for the 'nlmixr2' (Fidler et al (2019) <doi:10.1002/psp4.12445>) package,
10-
designed to simplify the modeling process for users. Additionally, this package includes supplementary functions
9+
Description: An R shiny user interface for the 'nlmixr2' (Fidler et al (2019) <doi:10.1002/psp4.12445>) package,
10+
designed to simplify the modeling process for users. Additionally, this package includes supplementary functions
1111
to further enhances the usage of 'nlmixr2'.
1212
Depends:
1313
R (>= 3.5.0),
@@ -29,12 +29,14 @@ Imports:
2929
ps,
3030
xfun,
3131
fresh,
32-
nlmixr2,
3332
nlmixr2est,
3433
magrittr,
34+
rxode2,
3535
cli
3636
Suggests:
3737
xpose,
38+
nlmixr2,
39+
nlmixr2plot,
3840
xpose.nlmixr2,
3941
nlme,
4042
testthat,

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export(update_inits)
4444
import(bs4Dash)
4545
import(ggplot2)
4646
import(gridExtra)
47-
import(nlmixr2)
47+
import(nlmixr2est)
4848
importFrom(cli,cli_alert_info)
4949
importFrom(cli,cli_alert_success)
5050
importFrom(magrittr,"%>%")

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# shinyMixR (development version)
2+
3+
- This version implements fixes for the new `nlmixr2` 4.0 package
4+
15
# shinyMixR 0.5
26

37
This version is a preview version of the package. The main changes are:
@@ -14,7 +18,7 @@ Within this version some updates have been implemented, see below the most impor
1418
- Implemented shiny modules for the back-bone structure of the app
1519
- Used the bs4Dash package to create the dashboard instead of shinydashboard
1620
- Added more functionality to the existing widgets and included new widgets:
17-
- in parameter table provide options to include BSV, shrinkage and back-transformation
21+
- in parameter table provide options to include BSV, shrinkage and back-transformation
1822
- in gof plots added the option to subset and perform pre-coding, adjust plot size and option to color by a variable
1923
- in fit plots added the option to subset and perform pre-coding, adjust plot size and more options to select variables and adjust scaling
2024
- Added a data exploration widget to create exploratory (interactive) plots and show underlying data

R/fit_plot.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ fit_plot <- function(dfrm,type="xpose",by="ID",idv="TIME",obs="DV",pred="PRED",i
3939
if(!all(c(idv,obs,pred)%in%names(dfrm))) stop("Please provide correct 'idv', 'obs', 'pred'")
4040
if(!is.null(by) && !all(by%in%names(dfrm))) stop("Please provide correct 'by'")
4141
if(!is.null(ipred) && !ipred%in%names(dfrm)) stop("Please provide correct 'ipred'")
42-
42+
4343
if(type=="xpose"){
4444
if((length(by)==1 && by!="ID") || length(by)>1 || idv!="TIME" || obs!="DV" || pred!="PRED" || ipred!="IPRED" || grp!="ID") stop("Changing variables does not work with xpose type of plots")
45-
if(length(find.package("nlmixr2", quiet = TRUE))>0){
45+
if(length(find.package("nlmixr2est", quiet = TRUE))>0){
4646
xpdb <- xpose.nlmixr2::xpose_data_nlmixr2(dfrm)
4747
}else{
4848
stop("nlmixr2 package is not installed")
@@ -58,7 +58,7 @@ fit_plot <- function(dfrm,type="xpose",by="ID",idv="TIME",obs="DV",pred="PRED",i
5858
dfrm$byvar <- factor(dfrm$byvar,levels=unique(dfrm$byvar))
5959
}
6060
pl <- ggplot(dfrm,aes_string(x=idv,group=grp)) + geom_point(aes_string(y=obs)) +
61-
geom_line(aes_string(y=pred))
61+
geom_line(aes_string(y=pred))
6262
if(!is.null(ipred)) pl <- pl + geom_line(aes_string(y=ipred),linetype=2)
6363
if(!is.null(by)) pl <- pl + facet_wrap("byvar",scales=scales)
6464
if(logy) pl <- pl + scale_y_log10()

R/gof_plot.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
#' }
3535
gof_plot <- function(dfrm,type="xpose",mdlnm=NULL,colby=NULL,ptype="all",outnm=NULL,projloc=".",title=NULL,linscale=FALSE,...){
3636
if(type=="xpose" & !is.null(colby)) stop("Color by does not work with xpose type of plots")
37-
if(type=="xpose" && length(find.package("nlmixr2", quiet = TRUE))>0){
37+
if(type=="xpose" && length(find.package("nlmixr2est", quiet = TRUE))>0){
3838
dat <- xpose.nlmixr2::xpose_data_nlmixr2(dfrm)
3939
}else{
4040
dat <- as.data.frame(dfrm)
4141
}
4242
plfun <- function(data,plottype){
43-
if(type=="xpose"){
43+
if(type=="xpose"){
4444
# types are "all", "ipred.dv", "pred.dv", "idv.res", "pred.res" .
4545
if(plottype=="pred.dv") pl <- xpose::dv_vs_pred(data,title=NULL,subtitle=NULL,caption=NULL)
4646
if(plottype=="ipred.dv") pl <- xpose::dv_vs_ipred(data,title=NULL,subtitle=NULL,caption=NULL)
@@ -65,7 +65,7 @@ gof_plot <- function(dfrm,type="xpose",mdlnm=NULL,colby=NULL,ptype="all",outnm=N
6565
lims <- c(as.data.frame(dfrm)[,"DV"],as.data.frame(dfrm)[,limt])
6666
if(!linscale) lims <- range(lims[lims>0]) else lims <- range(lims)
6767
pl <- pl + coord_cartesian(xlim=lims,ylim=lims)
68-
if(!linscale) pl <- pl + scale_y_log10() + scale_x_log10() + annotation_logticks(sides = "bl")
68+
if(!linscale) pl <- pl + scale_y_log10() + scale_x_log10() + annotation_logticks(sides = "bl")
6969
}
7070
pl <- pl + theme_shinyMixR()
7171
return(pl)
@@ -79,7 +79,7 @@ gof_plot <- function(dfrm,type="xpose",mdlnm=NULL,colby=NULL,ptype="all",outnm=N
7979
pl <- plfun(dat,ptype)
8080
if(!is.null(title)) pl <- pl + labs(title=title)
8181
}
82-
82+
8383
if(is.null(outnm)){
8484
return(pl)
8585
}else{

R/module_run.R

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#'
66
#' @param id Module id
77
#' @param proj_obj Project object
8-
#'
8+
#'
99
#' @export
1010
#' @return A list of html tags used for th UI of the app
1111
module_run_ui <- function(id, proj_obj) {
@@ -16,22 +16,22 @@ module_run_ui <- function(id, proj_obj) {
1616
actionButton(ns("monMdl"), "Monitor Model(s)",icon=icon("play")),br(),br(),
1717
checkboxGroupInput(ns("addExtra"),label=NULL,choices=c("Add CWRES to output","Add NPDE to output"),selected=c("Add CWRES to output","Add NPDE to output"),inline=TRUE),
1818
div(verbatimTextOutput(ns("progrTxt")),class="card card-body bg-light p-0",id="progph"),
19-
19+
2020
)
2121
}
2222
#------------------------------------------ module_run_server ------------------------------------------
2323
#' Run model module for server
24-
#'
24+
#'
2525
#' @param id Module id
2626
#' @param r reactive values object that is defined top-level
27-
#'
27+
#'
2828
#' @export
2929
#' @return No return value, called for side effects
3030
module_run_server <- function(id, r) {
3131
moduleServer(id, function(input, output, session) {
32-
32+
3333
#print("Im here")
34-
# Adapt/update model list
34+
# Adapt/update model list
3535
observeEvent(r$active_tab,{
3636
if(r$active_tab=="run"){
3737
updateSelectInput(session, "runLst", choices = sort(names(r$proj_obj)[names(r$proj_obj)!="meta"]),selected=input$runmod_runLst)
@@ -46,7 +46,7 @@ module_run_server <- function(id, r) {
4646
proj <- r$proj_obj
4747
checkall <- unlist(sapply(input$runLst,function(x){
4848
chk <- proj[[x]]$model
49-
chkdat <- proj[[x]]$modeleval$meta$data
49+
chkdat <- proj[[x]]$modeleval$meta$data
5050
chksrc <- try(source(chk,local=TRUE),silent=TRUE)
5151
if("try-error"%in%class(chksrc)){
5252
return("syntax error within model file")
@@ -77,54 +77,54 @@ module_run_server <- function(id, r) {
7777
myalert("Please select models to run",type = "error")
7878
}
7979
})
80-
80+
8181
runmodmonit <- reactive({
82-
82+
8383
progFn <- list.files(paste0(r$this_wd,"/shinyMixR/temp"),pattern="prog\\.txt$",full.names = TRUE)
8484
txt <- lapply(progFn,function(x) c(paste0("\n ***************",x,"***************"),readLines(x, warn = FALSE)))
85-
85+
8686
if (r$models_running > 0) {
87-
87+
8888
invalidateLater(1000, session)
89-
89+
9090
if (any(grepl("run finished", txt))) {
9191

9292
finished_models <- progFn[which(grepl("run finished", txt))]
93-
93+
9494
if (!all(finished_models %in% r$finished_models)) {
95-
95+
9696
print(sprintf("%s model(s) finished running", length(finished_models)))
97-
97+
9898
r$finished_models <- c(isolate(r$finished_models), finished_models)
9999
r$models_running <- length(setdiff(progFn, finished_models))
100100
r$model_updated <- isolate(r$model_updated) + 1
101-
101+
102102
exportTestValues(
103103
model_updated = r$model_updated
104104
)
105105
}
106106
}
107-
107+
108108
return(paste(unlist(txt), collapse = "\n"))
109-
109+
110110
} else {
111111
return(paste(unlist(txt), collapse = "\n"))
112112
}
113113
})
114-
114+
115115
output$progrTxt <- renderText(runmodmonit())
116-
116+
117117
model_updated_d <- debounce(reactive(r$model_updated), millis = 1000)
118-
118+
119119
observe({
120120
req(model_updated_d() > 0)
121121
r$proj_obj <- get_proj(r$this_wd)
122122
})
123-
123+
124124
# Disable suspend for output$myplot, otherwise necessary reactives
125125
# don't trigger when user is not on this tab anymore
126126
outputOptions(output, "progrTxt", suspendWhenHidden = FALSE)
127-
127+
128128
# Monitor all external runs
129129
rv <- reactiveValues(montbl=NULL)
130130
monmodal <- function(){
@@ -141,11 +141,11 @@ module_run_server <- function(id, r) {
141141
if(any(grepl("shinyMixR/temp/script",cmd)) & !any(grepl("prog\\.txt",cmd))){
142142
scrf <- sub("--file=","",cmd[grepl("shinyMixR/temp/script",cmd)])
143143
scrf <- try(readLines(scrf),silent=TRUE)
144-
#runin <- grepl("modres <- try\\(nlmixr\\(",scrf)
145-
runin <- grepl("modres <- try\\(nlmixr2::nlmixr\\(",scrf)
144+
#runin <- grepl("modres <- try\\(nlmixr\\(",scrf)
145+
runin <- grepl("modres <- try\\(nlmixr2est::nlmixr\\(",scrf)
146146
if(any(runin)){
147147
#modf <- gsub("modres <- try\\(nlmixr\\(|,.*","",scrf[runin])
148-
modf <- gsub("modres <- try\\(nlmixr2::nlmixr\\(|,.*","",scrf[runin])
148+
modf <- gsub("modres <- try\\(nlmixr2est::nlmixr\\(|,.*","",scrf[runin])
149149
tim <- try(ps::ps_cpu_times(x)["user"],silent=TRUE)
150150
pid <- try(ps::ps_pid(x), silent=TRUE)
151151
if(!inherits(tim,"try-error") && !inherits(pid,"try-error")){
@@ -166,7 +166,7 @@ module_run_server <- function(id, r) {
166166
output$mon_out <- DT::renderDT(hr_out())
167167
observeEvent(input$monMdl,{showModal(monmodal())},ignoreInit = TRUE)
168168
observeEvent(input$killMdl,{
169-
pid <- rv$montbl[input$mon_out_rows_selected,]
169+
pid <- rv$montbl[input$mon_out_rows_selected,]
170170
try(tools::pskill(pid))
171171
removeModal()
172172
},ignoreInit = TRUE)

R/run_nmx.r

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @param projloc character with the base location of the shinyMixR project
1313
#' @param addcwres logical indicating if CWRES should be added to the output
1414
#' @param addnpde logical indicating if NPDE should be added to the output
15-
#'
15+
#'
1616
#' @details the meta data is obtained by compiling the model. The dataset, estimation method and control list are
1717
#' then included in the nlmixr call. Meta data is included in the model function which is comparable with NONMEM.
1818
#' This method was chosen so that all information to run a model is kept together in one function
@@ -32,7 +32,7 @@ run_nmx <- function(mod,proj=proj,ext=TRUE,saverds=TRUE,autoupdate=TRUE,projloc=
3232
# if(autoupdate) assign(dnm,get_proj(projloc=projloc))
3333
# Source model to obtain meta data (places meta object in env)
3434
sret <- try(source(proj[[mod]]$model,local=TRUE))
35-
meta <- suppressMessages(try(eval(parse(text=c("nlmixr2::nlmixr(",readLines(proj[[mod]]$model),")$meta"))),silent=TRUE))
35+
meta <- suppressMessages(try(eval(parse(text=c("nlmixr2est::nlmixr(",readLines(proj[[mod]]$model),")$meta"))),silent=TRUE))
3636
if(inherits(meta, "try-error") || inherits(sret, "try-error")){
3737
warning("Error in model syntax please check before running\n")
3838
if(ext) writeLines(meta, paste0(projloc,"/shinyMixR/temp/",mod,".prog.txt"))
@@ -51,21 +51,21 @@ run_nmx <- function(mod,proj=proj,ext=TRUE,saverds=TRUE,autoupdate=TRUE,projloc=
5151

5252
tmpl <- readLines(paste0(system.file(package = "shinyMixR"),"/other/run_nmx.tmp"))
5353
if(is.null(meta$subs)) subs <- "" else subs <- meta$subs
54-
55-
rlst <- list(modelloc=normalizePath(proj[[mod]]$model,winslash = "/",mustWork = FALSE),
56-
data=meta$data,
54+
55+
rlst <- list(modelloc=normalizePath(proj[[mod]]$model,winslash = "/",mustWork = FALSE),
56+
data=meta$data,
5757
subs=subs,
58-
est=meta$est,
59-
control=cntrll,
60-
saveres=saverds,
58+
est=meta$est,
59+
control=cntrll,
60+
saveres=saverds,
6161
modelname=mod,
6262
locproj=normalizePath(projloc,winslash = "/"),
6363
addcwres=addcwres,
6464
addnpde=addnpde)
6565

6666
tscr <- paste0(projloc,"/shinyMixR/temp/script.",stringi::stri_rand_strings(1,6),".r")
6767
writeLines(whisker::whisker.render(tmpl,rlst),tscr)
68-
68+
6969
if(Sys.info()['sysname']=="Windows"){
7070
shell(paste0(R.home("bin"), "/Rscript.exe \"", tscr, "\" > \"",projloc,"/shinyMixR/temp/",mod,".prog.txt\" 2>&1"),wait=FALSE)
7171
}else{
@@ -74,8 +74,8 @@ run_nmx <- function(mod,proj=proj,ext=TRUE,saverds=TRUE,autoupdate=TRUE,projloc=
7474
}else{
7575
data <- readRDS(paste0(projloc,"/data/", meta$data, ".rds"))
7676
if(!is.null(meta$subs) && meta$subs!="") data_nlm <- subset(data,eval(parse(text=(meta$subs)))) else data_nlm <- data
77-
modres <- nlmixr2::nlmixr(eval(parse(text=readLines(proj[[mod]]$model))), data_nlm, est=meta$est,control=meta$control,nlmixr2::tableControl(cwres=addcwres, npde=addnpde))
78-
if(length(find.package("nlmixr2", quiet = TRUE))>0){
77+
modres <- nlmixr2est::nlmixr(eval(parse(text=readLines(proj[[mod]]$model))), data_nlm, est=meta$est,control=meta$control,nlmixr2est::tableControl(cwres=addcwres, npde=addnpde))
78+
if(length(find.package("nlmixr2est", quiet = TRUE))>0){
7979
ressum <- list(OBJF=modres$objective,CONDNR=modres$conditionNumberCor,partbl=modres$parFixedDf,partblf=modres$parFixed,omega=modres$omega,tottime=rowSums(modres$time))
8080
}else{
8181
ressum <- list(OBJF=modres$objective,partbl=modres$popDf,partblf=modres$par.fixed,omega=modres$omega,tottime=rowSums(modres$time))

R/shinyMixR-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"_PACKAGE"
33

44
## usethis namespace: start
5-
#' @import nlmixr2
5+
#' @import nlmixr2est
66
#' @importFrom stats na.omit setNames
77
#' @importFrom utils getParseData installed.packages read.csv tail
88
#' @importFrom magrittr "%>%"
99
## usethis namespace: end
10-
NULL
10+
NULL

R/update_inits.r

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @param out character with the path for the updated model to save
1111
#'
1212
#' @export
13-
#' @return nothing will be returned the function saves the updated model to disk
13+
#' @return nothing will be returned the function saves the updated model to disk
1414
#' @author Richard Hooijmaijers
1515
#' @examples
1616
#'
@@ -22,9 +22,9 @@ update_inits <- function(mod,res,out){
2222
modnm <- ls()[!ls()%in%c("mod","res","out")]
2323
ores <- readRDS(res)
2424

25-
# Get parameters from original model (adapted to use direct interface; easier to obtain IIV as well)
25+
# Get parameters from original model (adapted to use direct interface; easier to obtain IIV as well)
2626
eomod <- get(modnm)
27-
eomod2 <- nlmixr2::nlmixr2(eomod)
27+
eomod2 <- nlmixr2est::nlmixr2(eomod)
2828
opar <- eomod2$iniDf[eomod2$iniDf$fix==FALSE,c("name","est")]
2929
opar <- setNames(signif(opar$est,4),opar$name)
3030

@@ -35,7 +35,7 @@ update_inits <- function(mod,res,out){
3535

3636
# Update the model with the inits (perform intersect to only update parameters that allign)
3737
apar <- rpar[intersect(names(rpar),names(opar))]
38-
outt <- eomod %>% nlmixr2::ini(apar)
38+
outt <- eomod %>% rxode2::ini(apar)
3939
outt <- deparse(outt$fun)
4040
outt[1] <- paste(tools::file_path_sans_ext(basename(out)),"<-",outt[1])
4141
writeLines(outt,out)

inst/other/combined.results.html.r

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ lapply(files,function(x){
2828
})
2929
R3port::html_plot(pll,out=paste0(rootl,"/analysis/",mdln,"/05hist.eta.html"),show=FALSE,title="ETA distribution")
3030
}
31-
try(R3port::html_plot(nlmixr2::vpcPlot(res,n=500,show=list(obs_dv=TRUE)),out=paste0(rootl,"/analysis/",mdln,"/06vpc.plot.html"),show=FALSE,title="VPC"))
31+
try(R3port::html_plot(nlmixr2plot::vpcPlot(res,n=500,show=list(obs_dv=TRUE)),out=paste0(rootl,"/analysis/",mdln,"/06vpc.plot.html"),show=FALSE,title="VPC"))
3232

3333
R3port::html_combine(combine=paste0(rootl,"/analysis/",mdln),out="report.html",show=TRUE,
34-
template=paste0(system.file(package="shinyMixR"),"/other/bootstrap.htmltmpl"),toctheme=TRUE,
34+
template=paste0(system.file(package="shinyMixR"),"/other/bootstrap.htmltmpl"),toctheme=TRUE,
3535
rtitle = paste0("report:",x))
3636
})
37-
message("Script done!\n")
37+
message("Script done!\n")

0 commit comments

Comments
 (0)