Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Last active December 26, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/7074303 to your computer and use it in GitHub Desktop.
Save ramnathv/7074303 to your computer and use it in GitHub Desktop.

I modified your code slightly to use the rCharts syntax. You will need to install the dev branch of rCharts from github to run this application.

devtools::install_github("rCharts", "ramnathv", ref = "dev")
shiny::runGist('7074303')

I made three changes to your code:

  1. Replaced library(rHighcharts) with library(rCharts) in both ui.R and server.R
  2. Replaced Line 7 in server.R with a <- Highcharts$new() to make use of rCharts class.
  3. Replaced renderChart with renderChart2 which is a modified version of renderChart and will eventually replace it.
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) )
}
Week Y2002 Y2003 Y2004 Y2005 Y2006 Y2007 Y2008 Y2009 Y2010 Y2011
1 0 0 9 10 12 1 2 47 4 16
2 4 6 10 8 8 1 14 45 7 11
3 6 1 5 14 14 2 14 33 5 8
4 1 0 5 19 12 0 17 22 13 4
5 4 2 2 14 9 0 10 31 5 3
6 8 1 1 16 14 2 7 20 10 5
7 0 1 3 3 7 0 2 13 8 15
8 1 0 0 15 13 1 1 16 13 5
9 0 1 2 7 12 4 3 24 17 3
10 5 0 1 1 10 3 0 25 5 2
11 3 0 5 7 6 0 2 20 1 7
12 1 0 5 6 6 2 3 17 5 3
13 3 1 2 2 2 1 1 7 6 0
14 0 0 20 9 1 2 1 47 12 2
15 1 4 0 0 8 0 2 25 4 4
16 0 3 1 7 2 1 6 13 6 4
17 4 2 6 7 6 3 5 18 2 2
18 1 1 7 2 6 2 3 19 2 3
19 1 0 19 8 7 4 2 10 1 5
20 3 1 7 9 5 2 2 11 7 2
21 2 2 21 10 7 4 2 10 9 7
22 1 1 14 5 2 1 3 9 2 8
23 0 1 6 10 3 4 9 11 9 2
24 0 0 12 8 1 1 1 24 14 1
25 3 1 19 9 6 1 3 13 10 9
26 2 2 11 7 4 0 6 15 3 5
27 4 0 6 12 1 9 2 27 17 9
28 4 8 5 13 5 3 5 26 7 4
29 5 4 5 15 5 0 4 25 18 3
30 2 0 6 6 2 2 0 35 15 4
31 1 0 3 7 0 0 8 24 6 8
32 4 7 8 5 7 2 2 24 9 15
33 3 5 13 15 7 0 3 30 6 8
34 1 1 9 11 5 0 5 20 5 5
35 9 1 11 15 7 2 9 21 5 4
36 2 1 2 4 10 2 6 19 4 8
37 5 0 7 9 8 4 6 12 10 9
38 9 1 4 8 6 2 9 12 3 11
39 2 4 7 16 7 1 9 17 6 5
40 9 2 8 12 6 2 12 7 8 0
41 6 0 1 7 5 0 10 11 7 8
42 0 4 8 17 4 3 21 13 24 15
43 4 0 4 28 5 2 14 13 42 11
44 1 1 3 14 4 4 24 17 33 14
45 8 2 15 22 5 2 51 20 29 20
46 1 4 16 33 3 5 32 25 33 22
47 2 3 13 19 5 5 37 8 22 14
48 0 6 72 33 5 3 25 19 35 21
49 3 12 67 42 4 7 33 16 25 19
50 3 34 34 33 5 6 51 14 23 17
51 2 19 44 35 1 7 31 6 13 32
52 3 24 29 14 3 3 12 10 2 16
53 34 22
library( shiny )
library(rCharts)
shinyUI(
pageWithSidebar(
headerPanel("Disease Surveillance"),
sidebarPanel(
selectInput( "disease" , "Choose the disease:" ,
choices=c("Pertussis","Q Fever","Ehr","Ecoli O157") ) ,
sliderInput( "L" , label="Factor for Control Limits",
min=2 , max=6 , value=3 , step=0.2 , animate=T) ,
selectInput( "year" , "Choose the year:" ,
choices=c("2002","2003","2004","2005","2006",
"2007","2008","2009","2010","2011") ) ,
selectInput( "region" , "Select the region:",
choices=c("State","Eastern")) ,
selectInput("incubation", "Select surveillance period (weeks):",
choices=c("1","2"), selected="1"),
selectInput( "degree" , "How many terms in the trigonometric
regression?" , choices=c("2","4")) ,
selectInput( "its" ,
"Do you want to recompute limits after excluding
points above limits?" ,
choices=c("Yes","No"))
),
mainPanel( chartOutput("myPlot", "highcharts") )
)
)
@ejahanpour
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment