-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.R
160 lines (137 loc) · 6.71 KB
/
app.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#Welcome to my little shiny app! @ Mihir Iyer
#oye!
# Data Load & Prep --------------------------------------------------------
library(shiny)
library(shinythemes)
library(dplyr)
library(metricsgraphics)
library(markdown)
#read data (rds files are prepared by the Code_Data_Prep.R script see github repo for deets)
mental <- readRDS("Data_Mental_Post.rds")
defs <- readRDS("Data_MeasureDefs.rds")
### SHINY Bits ###
# UI application --------------------------------------------------------
ui <- shinyUI(fluidPage(theme = shinytheme("cosmo"),
tags$head(
tags$style(HTML(".mg-histogram .mg-bar rect {
fill: #006d2c;
shape-rendering: auto;
}
.mg-histogram .mg-bar rect.active {
fill: #31a354;
}"
)
)
),
fluidRow(titlePanel("VA National Mental Health Statistics Explorer"),
style='padding:14px;'
),
fluidRow(column(includeMarkdown("Notes_NEPEC_Description.md"),
width=6,
offset=0.5),
column(includeMarkdown("Notes_Resources.md"),
width=6,
offset=0.5)
),
fluidRow(br(),
br(),
column(wellPanel(uiOutput("categoryBox")),
wellPanel(uiOutput("itemBox")),
conditionalPanel(
condition = "input.item != 'Select a measure'",
wellPanel(tags$label("Measure definition"),
textOutput("defText")
)
),
wellPanel(tags$label("Methodological notes"),
includeMarkdown("Notes_Methods.md")
),
width=3),
column(
conditionalPanel(
condition = "input.item != 'Select a measure'",
h3("Measure Distribution"),
em(h4(textOutput("histTitle"))),
metricsgraphicsOutput("histPlot"),
br(),
tags$label("Summary:"),
verbatimTextOutput("summ"),
tags$label("How to interpret:"),
includeMarkdown("Notes_Interpret.md")
),
width=5),
column(
conditionalPanel(
condition = "input.item != 'Select a measure'",
h3("Medical Center Results"),
em(h4(textOutput("tblTitle"))),
dataTableOutput("rankTable")
),
width=4
)
)
)
)
# SERVER logic ---------------------
server <- function(input, output){
#suppressing warnings. for some reason the app was throwing an explicit id warning message. after researching it seems that suppressing the message won't have any adverse effect. See this for more details:
# https://github.com/hrbrmstr/metricsgraphics/issues/49
options(warn = -1)
#CATEGORY PICKER - create drop-down box to allow picking a measure Category
output$categoryBox <- renderUI(
selectInput(inputId = "category",
label = "Step 1: Select a measure category",
selected = "Select a category",
choices =c("Select a category", sort(unique(mental$Category)))
)
)
#ITEM PICKER - create a drop-down to allow picking an Item, this box is dependent on the Category selection
output$itemBox <- renderUI(
selectInput(inputId = "item",
label = "Step 2: Select a measure",
selected = "Select a measure",
choices= c("Select a measure", unique(mental$Item[which(mental$Category == input$category)]))
)
)
#MEASURE DEFINITION - display the definition of the selected measure
#select definition of the measure for display
output$defText <- renderText(defs$Definition[which(defs$Item == input$item)])
#PLOT SUBTITLE - create custom plot subtitle to indicate whether the measure is reported as a number or pecentage
subTitleText <- reactive({
unique(mental$ValueType[which(mental$Item == input$item)])
})
#for the table
output$tblTitle <- renderText(
paste("(Reported as a ", subTitleText(), ")", sep="")
)
#for the histogram
output$histTitle <- renderText(
paste("(Reported as a ", subTitleText(), ")", sep="")
)
#DATASET - create a reactive dataset based on the selected Item
zedata <- reactive({
filter(mental, Item %in% input$item) %>%
select(c(VISN, Station.Name, Value)) %>%
rename(MedicalCenter = Station.Name)
})
#SUMMARY Stats for selected measure
output$summ<- renderPrint(summary(zedata()$Value))
#HISTOGRAM PLOT - create a metricsgraphics hist plot
output$histPlot <- renderMetricsgraphics({
#conditional statement to display dataTABLE when a measure is selected
if(is.null(input$item)){return()
}else(mjs_plot(zedata()$Value, format="count") %>%
mjs_histogram(bins = 10) %>%
mjs_labs(x=input$item, y="Number of VA Medical Centers")
)
})
#RANKING TABLE - create a table that lists the facilities and their corresponding measure value
output$rankTable <- renderDataTable(
#conditional statement to display dataTABLE when a measure is selected
if(is.null(input$item)){return()
}else(zedata())
, options=list(order=list(2, 'desc'), pageLength = 25)
)
}
# Run the application
shinyApp(ui = ui, server = server)