-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.R
123 lines (90 loc) · 3.58 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
#### Load required libraries
library(shiny)
library(shinydashboard)
library(mrgsolve)
library(dplyr)
library(ggplot2)
library(flux)
library(rmarkdown)
library(cowplot)
library(RColorBrewer)
library(magrittr)
## Shiny server code
shinyServer(function(input, output) {
mod <- mread_cache("popPK")
######################################################################################
################# Run mrgsolve simulation upon button click
# DF_Simulation holds simulated data
DF_Simulation <- eventReactive(input$do, {
simulated_Data <- run_simulation(input,mod)
simulated_Data
})
######################################################################################
################################### Output - Graph
### Calculate prediction intervals based on user input
# prediction_intervals holds simulated prediction intervals
prediction_intervals <- reactive({
df <- pi_function(DF_Simulation(),input)
df
})
# lin_log_plots is a ggplot object of the simulated PK profiles
lin_log_plots <- reactive({
graph_function(prediction_intervals(),input)
})
## Show the plot
output$PKplot <- renderPlot({
lin_log_plots()
})
######################################################################################
################################### Output - Table
############### Calculate summary statistics
# sum_stats holds the summary statistics of the simulation per dose
sum_stats <- reactive({
df <- numeric_stats_function(DF_Simulation(),input)
df
})
## Show the summary stats table
output$sumstattable <- renderTable(sum_stats(),align='c',bordered = TRUE)
#####################################################################################
# Downloadable csv of generated PK dataset
output$downloadData <- downloadHandler(
filename = function() {
paste("Simulated_dataset",input$cmp_name,".csv",sep="") # File name with compound name (if inserted in report tab)
},
content = function(file) {
write.csv(DF_Simulation(), file, row.names = FALSE)
}
)
#####################################################################################
############# Generate .pdf report using rmarkdown
output$report <- downloadHandler(
filename = function() {
paste("PMX_popPK report -", Sys.Date(), ".pdf", sep="") # File name
},
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
tempLogo <- file.path(tempdir(), "Logo.png")
file.copy("./www/Logo.png", tempLogo, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params_for_rmd = list(sumstat=sum_stats(),
graphs=lin_log_plots(),
set_title=input$title,
set_author=input$author,
compound=input$cmp_name,
description=input$description,
dose_units=dose_units(input$units)
)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params_for_rmd,
envir = new.env(parent = globalenv())
)
}
)
})