geposanui/R/methods.R

88 lines
2.5 KiB
R
Raw Normal View History

2021-10-20 15:34:52 +02:00
# Construct UI for the methods editor.
2021-10-19 16:44:29 +02:00
methods_ui <- function(id) {
verticalLayout(
h3("Methods"),
div(style = "margin-top: 16px"),
lapply(methods, function(method) {
verticalLayout(
checkboxInput(
NS(id, method$id),
span(
method$description,
style = "font-weight: bold"
),
value = TRUE
),
sliderInput(
NS(id, sprintf("%s_weight", method$id)),
NULL,
2021-11-16 15:20:42 +01:00
min = -1.0,
max = 1.0,
step = 0.01,
value = 1.0
2021-10-19 16:44:29 +02:00
)
)
2021-10-21 11:43:34 +02:00
}),
actionButton(
2021-11-19 15:38:46 +01:00
NS(id, "reset_button"),
"Reset weights",
2021-10-21 11:43:34 +02:00
class = "btn-primary"
)
2021-10-19 16:44:29 +02:00
)
}
2021-10-20 15:34:52 +02:00
# Construct server for the methods editor.
#
# @param analysis The reactive containing the results to be weighted.
#
# @return A reactive containing the weighted results.
2021-11-17 22:58:55 +01:00
methods_server <- function(id, analysis) {
2021-10-19 16:44:29 +02:00
moduleServer(id, function(input, output, session) {
# Observe each method's enable button and synchronise the slider state.
lapply(methods, function(method) {
2021-11-17 22:58:55 +01:00
observeEvent(input[[method$id]],
{ # nolint
shinyjs::toggleState(sprintf("%s_weight", method$id))
},
ignoreInit = TRUE
)
2021-10-19 16:44:29 +02:00
})
2021-11-19 15:38:46 +01:00
observeEvent(
{ # nolint
analysis()
input$reset_button
},
{ # nolint
for (method in methods) {
updateCheckboxInput(
session,
method$id,
value = TRUE
)
2021-10-19 16:44:29 +02:00
2021-11-19 15:38:46 +01:00
updateSliderInput(
session,
sprintf("%s_weight", method$id),
value = analysis()$weights[[method$id]]
)
}
},
ignoreNULL = FALSE
)
reactive({
2021-10-19 16:44:29 +02:00
weights <- NULL
for (method in methods) {
if (input[[method$id]]) {
weight <- input[[sprintf("%s_weight", method$id)]]
weights[[method$id]] <- weight
}
}
2021-11-17 22:58:55 +01:00
geposan::ranking(analysis(), weights)
2021-10-19 16:44:29 +02:00
})
})
}