-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
311 lines (257 loc) · 10.5 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# Setup -------------------------------------------------------------------
# Load packages
library(statcheck)
library(shiny)
library(bslib)
library(pdftools)
library(htm2txt)
library(readtext)
library(DT)
library(tools)
# Set options
options(shiny.sanitize.errors = FALSE)
options(shiny.maxRequestSize = 100 * 1024 ^ 2)
# UI ----------------------------------------------------------------------
ui <- navbarPage(
theme = bs_theme(version = 5),
title = "statcheck // web",
collapsible = TRUE,
header = tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "css/styles.css"),
tags$script(
src = "https://kit.fontawesome.com/0c3170759b.js",
crossorigin = "anonymous")
),
tabPanel("Home",
tags$div(class = "container",
tags$div(class = "center",
tags$img(
src = "./img/statcheck-cropped.png",
title = "statcheck",
style = "max-width: 500px"),
tags$p(class = "fw-bold fs-4", "statcheck on the web")
),
tags$p(
"To check a PDF, DOCX or HTML file for errors in statistical
reporting, upload it below. See the FAQ page for more information
about what statcheck can and cannot do."
),
hr(),
fileInput(inputId = "files",
label = "Upload files (pdf, html, or docx):",
multiple = TRUE,
accept = c("pdf", "htm", "html", "doc", "docx")
),
h5("Settings:", class = "settings"),
checkboxInput("one_tail",
label = "Try to identify and correct for one-tailed tests",
value = TRUE,
width = "100%"
),
hr(),
# Only show the download button if there are results
uiOutput("downloadButtonTxt"),
uiOutput("downloadButtonUI"),
HTML("<br><br>"),
conditionalPanel(
condition = "!output.error",
DT::dataTableOutput("table"),
textOutput("sessionInfo")
),
conditionalPanel(
condition = "output.error",
tags$div(
class = "text-danger fw-bold",
textOutput("error")
)
),
conditionalPanel(
condition = "output.error",
tags$div(
class = "mt-3",
textOutput("sessionInfo2")
)
)
)
),
tabPanel("FAQ",
tags$div(class = "container",
includeHTML("html/FAQ.html")
)
),
tabPanel("Contact",
tags$div(class = "container",
includeHTML("html/contact.html")
)
),
tabPanel("Contributors",
tags$div(class = "container",
includeHTML("html/contributors.html")
)
),
tabPanel("Software/Packages",
tags$div(class = "container",
includeHTML("html/software.html")
)
)
)
# Server ------------------------------------------------------------------
server <- function(input, output) {
# Create variables to store statcheck results and error status
values <- reactiveValues(res = NULL, error = NULL)
# Render the error message
output$error <- renderText(values$error)
outputOptions(output, "error", suspendWhenHidden = FALSE)
observe({
files <- input$files
# Reset error messages when a new file is uploaded
values$error <- NULL
# Validate file types
valid_extensions <- c("pdf", "htm", "html", "doc", "docx")
invalid_files <- !tools::file_ext(files$name) %in% valid_extensions
if (any(invalid_files)) {
values$error <- "Please select only PDF, HTML, or Word files."
return(NULL)
}
})
# Render the statcheck results table
output$table <- renderDataTable(
extensions = "Buttons",
server = FALSE,
options = list(
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = list(
list(extend = 'csv', filename = input$file$name),
list(extend = 'excel', filename = input$file$name),
list(extend = 'pdf', filename = input$file$name)
),
text = 'Download table'
)
)
),
{
req(input$files)
files <- input$files # this creates a dataframe with file info
# create empty list to store results of separate files
statcheck_results <- list()
# Use progress indicator
withProgress(message = 'Processing files...', value = 0, {
# loop over files
for(i in 1:nrow(files)){
file <- files[i, ]
# Check whether the user supplied a PDF, HTML, or MS Word file
file_extension <- tools::file_ext(file$name)
# Extract text from the file, depending on the file extension
if (file_extension == "pdf") {
text <- pdftools::pdf_text(file$datapath)
} else if (file_extension %in% c("htm", "html")) {
html <- paste(readLines(file$datapath), collapse = "\n")
text <- htm2txt::htm2txt(html)
} else if (file_extension %in% c("doc", "docx")) {
word <- readtext::readtext(file$datapath)
text <- word$text
}
# store filename to return in final dataframe
names(text) <- file$name
# run statcheck in a try() environment to avoid the app from breaking
# if a paper throws an error
try_statcheck <- try(suppressMessages(
statcheck::statcheck(text, OneTailedTxt = input$one_tail)
))
if("try-error" %in% class(try_statcheck)){
statcheck_results[[i]] <- NULL
} else {
statcheck_results[[i]] <- try_statcheck
}
}
res <- do.call(rbind, statcheck_results)
# Print which statcheck version was run
version <- sessionInfo()$otherPkgs$statcheck$Version
output$sessionInfo <- renderText({
paste0("Statcheck package version: ", version)
})
# create a second session info for second conditional panel
# bit of a hacky solution...
output$sessionInfo2 <- renderText({
paste0("Statcheck package version: ", version)
})
# Check whether any results were found
if (is.null(res)) {
values$error <- "No results found. See the FAQ page for some common
reasons why statcheck doesn't detect some results."
# store empty df in res, in order to generate "empty" statcheck report
values$res <- data.frame()
return(NULL)
}
# replace chi symbol in raw result by letter X to avoid errors in
# compiling latex report
chi2raw_loc <- which(res$test_type == "Chi2")
res$raw[chi2raw_loc] <- gsub("^[^\\(]+", "X2", res$raw[chi2raw_loc])
# Clean up the data frame
res$error <- ifelse(res$error == FALSE, "Consistent", ifelse(
res$decision_error == TRUE, "Decision Inconsistency", "Inconsistency")
)
res <- subset(res, select = c(source, raw, computed_p, error))
# Format the computed p-value column
res$computed_p <- sprintf("%.05f", res$computed_p)
# Create human-friendly column names
names(res) <- c("File", "Statistical reference", "Computed p-value",
"Consistency")
# All went well so store that there is no error in case there previously was
# one
values$error <- NULL
# store result in a reactive value so that it can be accessed outside
# this function as wel
values$res <- res
return(res)
})
}
)
# Conditionally render the download button if statcheck correctly ran
output$downloadButtonTxt <- renderUI({
req(values$res) # Ensure that results are available
if (!is.null(values$res)) {
HTML("<em>Generating the report may take a few moments.</em>")
}
})
output$downloadButtonUI <- renderUI({
req(values$res) # Ensure that results are available
if (!is.null(values$res)) {
downloadButton("report", "Download report")
}
})
# Render the report
output$report <- downloadHandler(
filename = function() {
paste("statcheck_report_", Sys.Date(), ".pdf", sep = "")
},
content = function(file) {
req(values$res) # Ensure `res` is available
# Copy the report template to a temporary location
tempReport <- file.path(tempdir(), "report_template.Rmd")
file.copy("templates/report_template.Rmd", tempReport, overwrite = TRUE)
tempImg <- file.path(tempdir(), "statcheck-cropped.png")
file.copy("www/img/statcheck-cropped.png", tempImg, overwrite = TRUE)
# Collect additional information
file_name <- input$files$name
date <- Sys.Date()
statcheck_version <- sessionInfo()$otherPkgs$statcheck$Version
one_tailed <- ifelse(input$one_tail, "On", "Off")
# Knit the document, passing the `params` list, and evaluate in an
# isolated environment
rmarkdown::render(tempReport, output_file = file,
params = list(results = values$res,
file_name = file_name,
date = date,
statcheck_version = statcheck_version,
one_tailed = one_tailed),
output_format = "pdf_document",
envir = new.env(parent = globalenv()))
}
)
}
# Run application ---------------------------------------------------------
shinyApp(ui = ui, server = server)