-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
executable file
·135 lines (105 loc) · 5.6 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#####################################
# DOCUMENTATION FOR USING THE #
# SHINY PACKAGE CAN BE FOUND AT #
# http://shiny.rstudio.com/tutorial #
# County Profiles v0.0.1 (server.R) #
# Author & Copyright: Bogdan Rau #
# Web: http://bogdanrau.com #
#####################################
library(shiny)
shinyServer(function(input, output, session) {
data <- data.frame()
output$results <- renderDataTable({
input$getResults ## Take a dependency on input$getResults button
if(input$getResults == '0') { return() } ## Initial load with no button press, show nothing
else {
data <- isolate( ## Create data by concatenating different values from the input fields
read.csv(file = paste("data/", input$year, "/", input$population, "/", input$locationType, "/",
input$coLocation, ".csv", sep=""), check.names = FALSE)
)
print(data, type='html') # Print data to console to show on screen
}
},
options = list( # data table options to remove certain additional/advanced features
column.searchable = 1,
searching = TRUE,
paging = TRUE,
pageLength = 6,
ordering = FALSE,
columnDefs = list(
list(targets = c(2), className = 'alignCenter'),
list(targets = c(1), className = 'highlight')
),
lengthMenu = c(5,10,15,20)
)
)
output$insuranceStatus <- renderPlot({
input$getResults
if(input$getResults == '0') { return() }
else {
insurance <- isolate(read.csv(file = paste("data/", input$year, "/", input$population, "/", input$locationType , "/",
input$coLocation, ".csv", sep=""), check.names = FALSE, nrows=4, skip=3, header = FALSE, colClasses=c(NA, NA, "NULL")))
insurance[['V2']] <- as.numeric(sub("%", "", insurance[['V2']]))
insurance2 <- as.matrix(insurance)
values <- insurance[,'V2']
opar <- par(lwd = 0.1)
mp <- barplot(insurance[,'V2'],
main="Insurance Status",
ylab="Adults aged 18-64 (%)",
ylim= range(pretty(c(0, values))),
xlab="Health Insurance",
names.arg=c("Uninsured", "Employer-Based", "Medi-Cal/HF", "Other"),
col=c("indianred2", "lightskyblue3", "lightskyblue3", "lightskyblue3"),
xpd=FALSE)
text(mp, values, labels=values, pos=3, offset=-1, xpd=FALSE, font=2, col="white")
abline(h=0, col="black", lwd=1)
}}, width = "auto", height = 330)
output$downloadPlot <- downloadHandler(
# Specify the file name
filename = function() {
paste(input$coLocation, "png", sep=".")
},
content = function(file) {
# open the device
png(file, width = 1740, height = 990, res = 220)
# create the plot
input$getResults
if(input$getResults == '0') { return() }
else {
insurance <- isolate(read.csv(file = paste("data/", input$year, "/", input$population, "/", input$locationType , "/",
input$coLocation, ".csv", sep=""), check.names = FALSE, nrows=4, skip=3, header = FALSE, colClasses=c(NA, NA, "NULL")))
insurance[['V2']] <- as.numeric(sub("%", "", insurance[['V2']]))
insurance2 <- as.matrix(insurance)
values <- insurance[,'V2']
opar <- par(lwd = 0.1)
mp <- barplot(insurance[,'V2'],
main="Insurance Status",
ylab="Adults aged 18-64 (%)",
ylim= range(pretty(c(0, values))),
xlab="Health Insurance",
names.arg=c("Uninsured", "Employer-Based", "Medi-Cal/HF", "Other"),
col=c("indianred2", "lightskyblue3", "lightskyblue3", "lightskyblue3"),
xpd=FALSE)
text(mp, values, labels=values, pos=3, offset=-1, xpd=FALSE, font=2, col="white")
abline(h=0, col="black", lwd=1)
}
# close the device
dev.off()
}
)
output$downloadTable <- downloadHandler(
filename = function() {
paste(input$coLocation, "csv", sep=".")
},
content = function(file) {
data <- ## Create data by concatenating different values from the input fields
read.csv(file = paste("data/", input$year, "/", input$population, "/", input$locationType, "/",
input$coLocation, ".csv", sep=""), check.names = FALSE)
write.csv(data, file, row.names=FALSE)
}
)
output$downloadPDF <- renderUI({
downloadUrl <- paste0('http://healthpolicy.ucla.edu/health-profiles/adults/Documents/2012-2013/Counties/', input$coLocation, '.pdf')
tags$a(tags$i(' Download Report (.pdf)', class = 'fa fa-file-pdf-o'), href=downloadUrl, inline = TRUE, class = 'btn btn-default shiny-download-link shiny-bound-output', target = '_blank')
})
})