How to load a .DLL in app deployed on Shinyapp io

I am fairly new to R shiny app development. My current app allows users to load data and fit fish tag return models via maximum likelihood. I use the package TMB to

  1. compile a C++ .cpp file (function compile)
  2. load the .DLL using the dyn.load(dynlib("DLLName"))
  3. use the TMB MakeADFun function to create the necessary gradient information
    fitmodel<-MakeADFun(datar,parameters,DLL="IRCR",silent=T)
  4. fit the model using nlminb
    fit<-try(nlminb(fitmodel$par,fitmodel$fn,fitmodel$gr,control=list(eval.max=100000,iter.max=mit),
    lower=c(rep(log(flow1),length(Fyr)),rep(log(mlow1),length(Myr)),rep(log(falow1),length(FAyr))), upper=c(rep(log(fupper1),length(Fyr)),rep(log(mupper1),length(Myr)),rep(log(faupper1),length(FAyr)))),silent=T)

Everthing works fine locally.

When I deployed the app to the Shinyappio, I thought I could just include the .DLL in the "/www" folder (and not have to recompile) and access it with dyn.load when needed. However, my app keeps crashing when I go to run a model . I tried a couple of things initially thinking the dyn.load couldn't find the path. But now I don't think that is the problem

Does anyone know if I can actually include a DLL in the deployment? Or would I have to compile it differently? Any help will be greatly appreciated.

I don't know about .DLL's and the issues surrounding their use, but it is always a good idea, to add code to your project to provide some level of logging so points of failure can be more easily identified. and you can use rsconnect showlogs function to get your live apps logs and review them.
Wishing you luck.

Thank you. I didn't know I could access logs.

This answer at StackOverflow provides a hint on how to do this: r - Deploy a shiny app that sources c code via shinyapps.io - Stack Overflow

More specifically for the TMB package - IMHO the only way to get the DLL working is to compile it on shinyapps.io.

I agree compilation could be a time-consuming process impeding the interactivity of the app but you could add something in your code like

if (!file.exists(dynlib("mymodel"))) {
  compile("mymodel")
}

so that the model is only compiled once.

1 Like

Thanks very much for your help. It worked seamlessly.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.