forked from rjhamilton/sunspots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
47 lines (37 loc) · 1.17 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
####
# sunspots Shiny App - server.R
# Bob Hamilton, [email protected]
#
library(shiny)
library(datasets)
data(sunspot.month)
ss <- sunspot.month ## choose sunspots or sunspot.month
minYear <- ceiling(tsp(ss)[1]) ## first full year
maxYear <- floor(tsp(ss)[2] - 11/12) ## last full year
## condition default values
dfltYearRange <- c(1800, 1899)
dfltPlotType <- "p"
dfltBinSize <- 25
shinyServer(
function(input, output, session) {
## reset button
observeEvent(input$reset_input, {
updateSliderInput(session, "yearRange", value=dfltYearRange)
updateRadioButtons(session, "plotType", selected=dfltPlotType)
updateSliderInput(session, "binSize", value=dfltBinSize)
})
## time-series plot
output$tsPlot <- renderPlot({
t1 <- input$yearRange[1]
t2 <- input$yearRange[2] + 11/12
pt <- input$plotType
bs <- input$binSize
y <- window(ss, start=t1, end=t2)
y.sm <- window(filter(ss, rep(1, bs) / bs), start=t1, end=t2)
title <- sprintf("%d years of sunspot activity, %d-%d",
as.integer(t2 - t1 + 1), as.integer(t1), as.integer(t2))
plot(y, type=pt, cex=0.5, xlab="year", ylab="sunspot count", main=title)
lines(y.sm, col="red", lwd=2)
})
}
)