-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShiny App Reports
223 lines (207 loc) · 7.14 KB
/
Shiny App Reports
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#Libraries
library(shiny)
library(jsonlite)
library(ggplot2)
library(shinydashboard)
# Histogram plot
create_histogram_plot <- function(hist_data, colors, title) {
hist_df <- do.call(rbind, lapply(hist_data, function(h) {
data.frame(Category = as.character(h$Category$`@value`),
Count = as.numeric(h$Count$`@value`))
}))
ggplot(hist_df, aes(x = Category, y = Count, fill = Category)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = colors) +
labs(title = title, x = "Category", y = "Count") +
theme_minimal()
}
# Line chart
create_line_chart <- function(line_data, title) {
x_values <- as.numeric(line_data$X$`@value`)
y_values <- as.numeric(line_data$Y$`@value`)
ggplot(data.frame(X = x_values, Y = y_values), aes(x = X, y = Y)) +
geom_line(color = "blue", size = 1) +
labs(title = title, x = "X", y = "Y") +
theme_minimal()
}
# Create boxplot
create_boxplot <- function(hist_data, title) {
hist_df <- do.call(rbind, lapply(hist_data, function(h) {
data.frame(Category = as.character(h$Category$`@value`),
Count = as.numeric(h$Count$`@value`))
}))
ggplot(hist_df, aes(x = Category, y = Count)) +
geom_boxplot() +
labs(title = title, x = "Category", y = "Count") +
theme_minimal()
}
# Statistical details
extract_statistical_details <- function(json_data) {
if (!is.null(json_data$Histogram)) {
hist_data <- json_data$Histogram
values <- unlist(lapply(hist_data, function(h) as.numeric(h$Count$`@value`)))
stats <- data.frame(
Mean = mean(values, na.rm = TRUE),
Median = median(values, na.rm = TRUE),
SD = sd(values, na.rm = TRUE)
)
return(stats)
}
return(data.frame(Mean = NA, Median = NA, SD = NA))
}
# UI
ui <- dashboardPage(
dashboardHeader(title = "DIZ Reports"),
dashboardSidebar(
sidebarMenu(
menuItem("Upload Files", tabName = "uploads", icon = icon("file-upload")),
menuItem("Visualize Data", tabName = "visualize", icon = icon("chart-bar")),
menuItem("Statistical Details", tabName = "stats", icon = icon("info-circle")),
menuItem("Download Output", tabName = "download", icon = icon("download"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "uploads",
fileInput("file1", label = "University Hospital Dresden Report:"),
fileInput("file2", label = "University Hospital Greifswald Report:"),
fileInput("file3", label = "University Hospital Leipzig Report:"),
selectInput("chart_type", label = "Chart Type:", choices = c("Histogram", "Line Chart", "Boxplot"))
),
tabItem(tabName = "visualize",
uiOutput("plots_output")
),
tabItem(tabName = "stats",
tableOutput("stats_output")
),
tabItem(tabName = "download",
downloadButton("download_plots", "Download Plots"),
downloadButton("download_stats", "Download Statistical Data")
)
)
)
)
# Server
server <- function(input, output) {
# Generate plots
all_plots <- reactive({
plots <- list()
for (i in 1:3) {
file <- input[[paste0("file", i)]]
if (!is.null(file) && file$name != "") {
json_data <- tryCatch(fromJSON(file = file$datapath), error = function(e) NULL)
if (!is.null(json_data)) {
if (input$chart_type == "Histogram") {
hist_data <- json_data$Histogram
if (!is.null(hist_data)) {
plot <- create_histogram_plot(hist_data, rainbow(10), paste("University Hospital", c("Dresden", "Greifswald", "Leipzig")[i]))
plots <- c(plots, list(plot))
} else {
plots <- c(plots, list(ggplot() + ggtitle("No Histogram data found")))
}
} else if (input$chart_type == "Line Chart") {
line_data <- json_data$LineChart
if (!is.null(line_data)) {
plot <- create_line_chart(line_data, paste("University Hospital", c("Dresden", "Greifswald", "Leipzig")[i]))
plots <- c(plots, list(plot))
} else {
plots <- c(plots, list(ggplot() + ggtitle("No Line Chart data found")))
}
} else if (input$chart_type == "Boxplot") {
hist_data <- json_data$Histogram
if (!is.null(hist_data)) {
plot <- create_boxplot(hist_data, paste("University Hospital", c("Dresden", "Greifswald", "Leipzig")[i]))
plots <- c(plots, list(plot))
} else {
plots <- c(plots, list(ggplot() + ggtitle("No Boxplot data found")))
}
}
} else {
plots <- c(plots, list(ggplot() + ggtitle("Invalid JSON data")))
}
}
}
plots
})
# Generate statistical details
all_stats <- reactive({
stats_list <- list()
for (i in 1:3) {
file <- input[[paste0("file", i)]]
if (!is.null(file) && file$name != "") {
json_data <- tryCatch(fromJSON(file = file$datapath), error = function(e) NULL)
if (!is.null(json_data)) {
stats <- extract_statistical_details(json_data)
stats_list[[i]] <- cbind(Hospital = c("Dresden", "Greifswald", "Leipzig")[i], stats)
} else {
stats_list[[i]] <- data.frame(Hospital = c("Dresden", "Greifswald", "Leipzig")[i], Mean = NA, Median = NA, SD = NA)
}
}
}
do.call(rbind, stats_list)
})
# Render the plots
output$plots_output <- renderUI({
plots <- all_plots()
if (length(plots) > 0) {
plot_ui_list <- lapply(1:length(plots), function(i) {
plot_output_id <- paste0("plot_", i)
checkbox_id <- paste0("checkbox_", i)
column(
width = 4,
plotOutput(plot_output_id),
checkboxInput(checkbox_id, label = paste("Show/Hide", c("Dresden", "Greifswald", "Leipzig")[i]), value = TRUE)
)
})
fluidRow(plot_ui_list)
} else {
tagList(
plot(1, type = "n", ann = FALSE), # create an empty plot area
text(1, 1, "No data to display", cex = 1.5) # display a message in the empty plot area
)
}
})
# Render plots based on checkboxes
observe({
plots <- all_plots()
for (i in 1:length(plots)) {
local({
plot_output_id <- paste0("plot_", i)
checkbox_id <- paste0("checkbox_", i)
output[[plot_output_id]] <- renderPlot({
if (input[[checkbox_id]]) {
plots[[i]]
} else {
NULL
}
})
})
}
})
# Render statistical details
output$stats_output <- renderTable({
all_stats()
})
# Download for plots
output$download_plots <- downloadHandler(
filename = function() { paste("plots-", Sys.Date(), ".pdf", sep = "") },
content = function(file) {
plots <- all_plots()
pdf(file)
for (plot in plots) {
print(plot)
}
dev.off()
}
)
# Download for statistical data
output$download_stats <- downloadHandler(
filename = function() { paste("stats-", Sys.Date(), ".csv", sep = "") },
content = function(file) {
stats <- all_stats()
write.csv(stats, file, row.names = FALSE)
}
)
}
# Run the app
shinyApp(ui = ui, server = server)