My shiny application (curve fitting) is very slow compare to running in R script. >10times slower . How can I improve?
Server code
library(shiny)
library(minpack.lm)
library(deSolve)
library(ggplot2)
library(FME)
shinyServer(function(input, output) {
#get datafile
datfile<-reactive({
req(input$data_att)
dataspec<-input$data_att
aa<-read.table(dataspec$datapath,header=TRUE, sep=",")
return(aa)
})
#initiate curve fitting with button "Fitting"
curvefitting<-eventReactive(input$Fitting,{
#initial parameters"
parms<-c(ka=1E4,kd=1E-4,bmax=40)
Fit<-modFit(cost,p=parms)
ab<-coef(Fit)
})
output$test<-renderText({curvefitting()})
cost<-function(parms){
out<-ode(y<-c(F=0),times=seq(0,2406,1),model,parms)
dataout<-as.data.frame(out)
modCost(dataout,datfile())
}
# model, Import function due to signal vary in time,
model <- function (time, y,parms) {
with(as.list(c(y,parms)), {
import<-signal(time)
dF <- (ka*import*(bmax-F) -kd*F)
list(dF)
})
}
signal <- function(time) {
times <- seq(0, 2406, 1)
ca0<-rep(0,15)
ca1<-rep(1E-7,210)
ca2<-rep(0 ,158)
ca3<-rep(2E-7 ,210)
ca4<-rep(0 ,158)
ca5<-rep(4E-7 ,210)
ca6<-rep(0,158)
ca7<-rep(8E-7 ,210)
ca8<-rep(0,158)
ca9<-rep(16E-7 ,210)
ca10<-rep(0,710)
concentrations<-list(ca0,ca1,ca2,ca3,ca4,ca5,ca6,ca7,ca8,ca9,ca10)
conc<-Reduce(c,concentrations)
signal <- data.frame(times, conc)
import <- approxfun(signal, rule = 1)
import(time)
}
#end of program
})
UI
library(shiny)
#
shinyUI(fluidPage(
titlePanel("Single Cycle"),
sidebarLayout(
sidebarPanel(
fileInput("data_att","upload the file"),
textOutput("test"),
actionButton("Fitting", "Run Curve fitting")
),
#not used
mainPanel(plotOutput("dissanalys"))
)
)
)