|
require(rCharts) |
|
shinyServer( function( input , output ) |
|
{ |
|
output$myPlot <- renderChart2( |
|
{ |
|
|
|
a <- Highcharts$new() |
|
readFileName <- paste( input$region, input$disease, "2002-2011weekly.csv" ) |
|
mydata = read.csv(readFileName) |
|
dataMatrix <- as.matrix(mydata)[1:52,-1] |
|
dataMatrix2 <- dataMatrix |
|
numYears <- ncol(dataMatrix) |
|
if (input$incubation == 2) |
|
{ |
|
for (r in 2:52 ){ |
|
for (c in 1:numYears ){ |
|
dataMatrix[r, c] <- dataMatrix2[r ,c]+dataMatrix2[r-1, c] |
|
} |
|
} |
|
} |
|
L <- input$L |
|
degree <- as.numeric(input$degree) |
|
includeObs <- array( TRUE , dim=dim(dataMatrix) ) |
|
limitArray <- getLimits( dataMatrix , includeObs , L , degree ) |
|
LCL <- limitArray[,1] |
|
CL <- limitArray[,2] |
|
UCL <- limitArray[,3] |
|
if ( input$its == "Yes" ) |
|
{ |
|
for (week in 1:52) |
|
{ |
|
for (j in 1:numYears) |
|
{ |
|
if ( dataMatrix[week,j] > UCL[week] ) { includeObs[week,j] <- FALSE } |
|
} |
|
} |
|
limitArray <-getLimits( dataMatrix , includeObs , L , degree ) |
|
LCL <- limitArray[,1] |
|
CL <- limitArray[,2] |
|
UCL <- limitArray[,3] |
|
} |
|
ymax <- max( dataMatrix ) |
|
ymax <- 1.1*max( c(ymax,UCL)) |
|
cat( "ymax =") |
|
cat( ymax ) |
|
a$title(text = paste(input$disease,as.character(input$year))) |
|
a$xAxis(title = list(text= "Week")) |
|
a$yAxis(title = list(text= paste("Counts in",as.character(input$year)))) |
|
a$plotOptions(line = list(marker = list(symbol='circle', radius=2))) |
|
a$data(name = "Disease Counts", dataMatrix[,as.numeric(input$year)-2001] , |
|
type="line", color="blue" ) |
|
a$data(name="LCL", c(LCL,LCL[52]) , type="line", color="red") |
|
a$data(name="UCL", c(UCL,UCL[52]) , type="line", color="red" ) |
|
|
|
return(a) |
|
|
|
|
|
} ) |
|
} ) |
|
|
|
|
|
getLimits <- function( dataMatrix , includeObs , L , degree) |
|
{ x = 1:52 |
|
nyears = ncol( dataMatrix ) |
|
cat("\n") |
|
if ( degree == 2 ) |
|
{ |
|
xs1 = sin(2*pi*x/52); xc1 = cos(2*pi*x/52) |
|
for ( week in x ) |
|
{ |
|
for ( j in 1:nyears ) |
|
{ |
|
if ( ! includeObs[week,j] ) { dataMatrix[week,j] = NA } |
|
} |
|
} |
|
weeklyMeans = apply( dataMatrix[,1:nyears] , 1 , mean , na.rm=TRUE ) |
|
y = weeklyMeans[1:52] |
|
trigReg = lm( y ~ xs1 + xc1 ) |
|
b0 = trigReg[[1]][[1]]; b1 = trigReg[[1]][[2]] |
|
b2 = trigReg[[1]][[3]] |
|
yhat = b0 + b1*xs1 + b2*xc1 |
|
yhat = pmax(0,yhat) |
|
# cat("yhat = ") |
|
# cat(yhat) |
|
} |
|
if ( degree == 4) |
|
{ |
|
xs1 = sin(2*pi*x/52); xc1 = cos(2*pi*x/52) |
|
xs2 = sin(4*pi*x/52); xc2 = cos(4*pi*x/52) |
|
for ( week in x ) |
|
{ |
|
for ( j in 1:nyears ) |
|
{ |
|
if ( ! includeObs[week,j] ) { dataMatrix[week,j] = NA } |
|
} |
|
} |
|
weeklyMeans = apply( dataMatrix[,1:nyears] , 1 , mean , na.rm=TRUE ) |
|
y = weeklyMeans[1:52] |
|
trigReg = lm( y ~ xs1 + xc1 + xs2 + xc2 ) |
|
b0 = trigReg[[1]][[1]]; b1 = trigReg[[1]][[2]] |
|
b2 = trigReg[[1]][[3]]; b3 = trigReg[[1]][[4]] |
|
b4 = trigReg[[1]][[5]] |
|
yhat = b0 + b1*xs1 + b2*xc1 + b3*xs2 + b4*xc2 |
|
yhat = pmax(0,yhat) |
|
# cat("yhat = ") |
|
# cat(yhat) |
|
} |
|
LCL = rep(0,52) |
|
CL = rep(0,52) |
|
UCL = rep(0,52) |
|
for ( week in 1:52 ) |
|
{ |
|
LCL[week] = max( 0, yhat[week]-L*sqrt(yhat[week]) ) |
|
CL[week] = yhat[week] |
|
UCL[week] = yhat[week]+L*sqrt(yhat[week]) |
|
} |
|
return( cbind(LCL,CL,UCL) ) |
|
} |
|
|
|
|
Ramnath,
Thank you for the modifications. The codes are working perfectly on the localhost. However, when I upload it on my Shiny hosting account, the charts are not shown. Do I need to upload javascript files on the hosting account, as well as the rfiles, to solve this problem?
Thank you again!