-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.R
167 lines (148 loc) · 4.82 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
161
162
163
164
165
166
167
pacman::p_load(
"shiny", "officer", "tidyverse","assertr", "forcats",
"Cairo", "grid", "gridExtra",
"rvg", "glue", "lubridate", "here", "janitor", "withr",
"finalfit", "writexl", "shinyWidgets", "shinydashboard",
"shinydashboardPlus", "plotly", "shinybusy", "flexdashboard",
"readxl"
)
# List of inputs used in the app -----------------------------------------------
inputs <- list(
# folder where data files will be read in from
input_dir = here("files/input"),
# list of aesthetic choices
aesthetics = list(
dashboard_title = NULL,
tab_title = "Key Indicator Visuals",
color = "green"),
# folder where templates are stored
template_source = here("files/input/template/"),
# list of templates
templates = list(
template_one = here("files/input/template/Template_SlideCFR.pptx")
# template_two = here("files/input/template/Template_SlideRepDeath.pptx"),
# template_three = here("files/input/template/Template_SlideEpiCurve.pptx")
),
# list of functions used by app
functions = list(
clean = here("files/input/functions/clean_jhu_coviddata.R"),
preview_rmd = here("files/input/preview_CFRs.Rmd"),
generate_rmd = here("files/input/generate_CFRs.RMD"))
)
# Lists of data files available to use in app ----------------------------------
# Create lists of templates available for data ---------------------------------
template_list <- list.files(inputs$template_source,
full.names = FALSE,
pattern = "Template"
)
# Setup user interface for app -------------------------------------------------
ui <- function() {
ui <- dashboardPage(
skin = inputs$aesthetics$color,
# Title
header = dashboardHeader(
title = as.character(inputs$aesthetics$dashboard_title),
titleWidth = 400
),
# Disable ugly sidebar
sidebar = dashboardSidebar(disable = TRUE),
# Create body of the page
body = dashboardBody(
fluidRow(
# # progress spinner progress icon
add_busy_spinner(
spin = "fading-circle",
timeout = 100,
position = "top-left"
),
# Initialize tabs
# First tab
tabBox(
id = "panels",
tabPanel(
as.character(inputs$aesthetics$tab_title),
# select template
shinyWidgets::pickerInput(
inputId = "select_template",
label = "Select a template file:",
choices = template_list,
options = list(size = 8)
),
width = 4,
tags$br(),
# Figure Preview button
actionButton(
inputId = "figure_preview",
label = "Preview Figure"
),
# Generate slide button
actionButton(
inputId = "generate_slide",
label = "Generate Slide"
),
width = 4,
tags$br()
) # close first tab
), # tab box
box(
title = "Figure Preview",
htmlOutput("plot"), width = 12
)
) # fluid row
) # dashboard body
) # dashboard page
return(ui)
} # ui function
# Initialize connection to the R Shiny server ----------------------------------
server <- function(input, output, session) {
# Update on figure preview ---------------------------------------------------
# if the command is submitted
# If the user selects to preview a figure
observeEvent(input$figure_preview, {
temp <- isolate(input$select_template)
template_dict <- list(
# replace with user indicated name of template
"Template_SlideCFR.pptx" =
list(
in_file_p = inputs$functions$preview_rmd,
out_file = "preview_CFRs.html"
)
)
# Output the plots requested
output$plot <- renderUI({
rmarkdown::render(
input = template_dict[[temp]]$in_file_p,
params = param_list,
output_dir = "./www/",
output_format = "flexdashboard::flex_dashboard",
quiet = FALSE
)
tags$html(
tags$iframe(
seamless = "seamless",
src = template_dict[[temp]]$out_file,
width = "100%",
height = "800px",
id = "reportIframe"
)
)
})
}) # end slide preview loop
# If the "Generate Slide button is pushed,
observeEvent(input$generate_slide, {
temp <- isolate(input$select_template)
template_dict <- list(
# replace with user indicated name of template
"Template_SlideCFR.pptx" =
list(in_file_g = inputs$functions$generate_rmd)
)
# generate the slides with user-provided parameters
rmarkdown::render(
input = template_dict[[temp]]$in_file_g,
params = param_list,
quiet = FALSE
)
})
} # server
# run complete App -------------------------------------------------------------
shinyApp(ui, server) # Shiny App