-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
190 lines (166 loc) · 4.12 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# ---- Load ----
library(shiny)
library(tools)
library(vroom)
library(readxl)
library(shinybusy)
library(shinyjs)
# Note: Do not load reticulate to prevent a default virtual env being set
# ---- Python ----
# Use virtualenv as it is available on shinyapps.io as a default system package:
# https://docs.rstudio.com/shinyapps.io/appendix.html#default-system-packages
# Create virtualenv if not available
if (!(Sys.getenv("VIRTUALENV_NAME") %in% reticulate::virtualenv_list())) {
reticulate::virtualenv_create(
envname = Sys.getenv("VIRTUALENV_NAME"),
python = Sys.getenv("PYTHON_PATH")
)
reticulate::virtualenv_install(
Sys.getenv("VIRTUALENV_NAME"),
packages = "pandas-profiling",
ignore_installed = TRUE
)
}
# Load virtualenv
reticulate::use_virtualenv(
Sys.getenv("VIRTUALENV_NAME"),
required = TRUE
)
# ---- UI ----
ui <-
fluidPage(
useShinyjs(),
# - Logos -
fluidRow(
align = "center",
# - MikeJohnPage -
column(
width = 4,
align = "center",
tags$a(
href = "https://mikejohnpage.com",
target = "_blank",
tags$img(
src = "mikejohnpage.png",
width = 200,
style = "padding-top: 30px;"
)
)
),
# - Blank -
column(
width = 4,
align = "center"
),
# - GitHub -
column(
width = 4,
align = "center",
tags$a(
href = "https://github.com/mikejohnpage/shiny-panda",
target = "_blank",
tags$img(
src = "github.png",
width = 30,
style = "padding-top: 50px;"
)
)
)
),
# - Logo -
fluidRow(
align = "center",
tags$img(
src = "logo.png",
width = 150,
style = "padding-top: 100px; padding-bottom: 10px"
)
),
# - Title -
fluidRow(
align = "center",
tags$h1(
style = "font-size: 75px; padding-top: 10px; padding-bottom: 5px; width: 80%;",
"Shiny Panda"
)
),
# - Instructions -
fluidRow(
align = "center",
tags$h2(
style = "padding-top: 0px; padding-bottom: 50px; width: 50%",
"Upload a file. Get back a Pandas Profiling report. Click the GitHub
logo in the top-right corner of the page to learn more."
)
),
# - Upload Button -
fluidRow(
align = "center",
fileInput(
inputId = "upload",
buttonLabel = "Upload...",
label = NULL,
accept = c(
".csv",
".tsv",
".xls",
".xlsx"
)
)
),
# - Invisible download data -
conditionalPanel(
"false", # always hide the download button
downloadButton("download")
)
)
# ---- Server ----
server <-
function(input, output, session) {
# Read in data
data <-
reactive({
req(input$upload)
switch(
EXPR = file_ext(input$upload$name),
csv = vroom(input$upload$datapath, delim = ","),
tsv = vroom(input$upoad$datapath, delim = "\t"),
xls = read_excel(input$upload$datapath),
xlsx = read_excel(input$upload$datapath)
)
})
# Observe changes in data(). Trigger download when data changes.
observeEvent(
data(),
{
runjs("$('#download')[0].click();")
}
)
output$download <-
downloadHandler(
filename = "report.html",
content = function(file) {
show_modal_gif(
src = "loading.gif",
text = "Generating report, this could take a while...",
height = "270px",
width = "270px"
)
# - Generate Pandas Profiling report -
pandas_profiling <-
reticulate::import(
"pandas_profiling",
convert = FALSE
)
profile <-
pandas_profiling$ProfileReport(
data(),
title = "Pandas Profiling Report"
)
profile$to_file(file)
remove_modal_gif()
}
)
}
# ---- Run App ----
shinyApp(ui, server)