Skip to content

Commit

Permalink
Fix data preparation UI
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Dec 12, 2023
1 parent f9ffd40 commit f114e2c
Showing 1 changed file with 60 additions and 82 deletions.
142 changes: 60 additions & 82 deletions R/module_prepare.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,53 @@ module_prepare_ui <- function(id) {
# Create a namespace function using the provided id
ns <- NS(id)

fluidRow(
column(
width = 4,
accordion(
id = ns("prepare_accordion"),
open = FALSE,
accordion_panel(
title = "1. Select columns",
value = "select",
checkboxGroupInput(
inputId = ns("select"),
label = NULL,
choices = NULL,
selected = NULL,
width = "100%"
)
),
accordion_panel(
title = tooltip(
span("2. Remove data", icon("circle-question")),
"Remove any non informative data.",
placement = "auto"
),
value = "remove",
## Input: remove zero
checkboxInput(
inputId = ns("remove_zero_row"),
label = "Remove rows with zero",
value = FALSE
),
checkboxInput(
inputId = ns("remove_zero_column"),
label = "Remove columns with zero",
value = FALSE
),
## Input: remove constant
checkboxInput(
inputId = ns("remove_constant_column"),
label = "Remove constant columns",
value = FALSE
),
hr(),
## Input: remove all?
checkboxInput(
inputId = ns("all"),
label = "Remove only if all values meet the condition",
value = TRUE,
width = "100%"
)
),
accordion_panel(
title = tooltip(
span("3. Filter rows", icon("circle-question")),
"Remove data points that fall outside a specification.",
placement = "auto"
),
value = "filter",
uiOutput(outputId = ns("filter"))
)
)
), # column
column(
width = 8,
sidebarLayout(
sidebarPanel(
h5("1. Select columns"),
checkboxGroupInput(
inputId = ns("select"),
label = NULL,
choices = NULL,
selected = NULL,
width = "100%"
),
hr(),
h5("2. Remove data"),
helpText("Remove any non informative data."),
## Input: remove zero
checkboxInput(
inputId = ns("remove_zero_row"),
label = "Remove rows with zero",
value = FALSE
),
checkboxInput(
inputId = ns("remove_zero_column"),
label = "Remove columns with zero",
value = FALSE
),
## Input: remove constant
checkboxInput(
inputId = ns("remove_constant_column"),
label = "Remove constant columns",
value = FALSE
),
## Input: remove all?
checkboxInput(
inputId = ns("all"),
label = "Remove only if all values meet the condition",
value = TRUE,
width = "100%"
),
hr(),
h5("3. Filter rows"),
helpText("Remove data points that fall outside a specification."),
uiOutput(outputId = ns("filter"))
), # sidebarPanel
mainPanel(
## Output: display data
DT::dataTableOutput(outputId = ns("table"))
) # column
) # fluidRow
) # sidebarLayout
}

# Server =======================================================================
Expand All @@ -95,20 +75,24 @@ module_prepare_server <- function(id, x) {
stopifnot(is.reactive(x))

moduleServer(id, function(input, output, session) {
## Select data
## Build UI
observe({
freezeReactiveValue(input, "select")
updateCheckboxGroupInput(
inputId = "select",
label = "Columns to keep:",
label = "Columns to remove:",
choices = colnames(x()),
selected = colnames(x()),
selected = NULL,
inline = TRUE
)
})
data_select <- bindEvent(
reactive({ x()[, input$select, drop = FALSE] }),
input$select
)

## Select data
data_select <- reactive({
assert_csv(x())
j <- !(colnames(x()) %in% input$select)
x()[, j, drop = FALSE]
})

## Remove data
data_clean <- reactive({
Expand Down Expand Up @@ -136,11 +120,10 @@ module_prepare_server <- function(id, x) {

## Filter rows
data_filter <- reactive({
parts <- colnames(data_clean())
each_var <- lapply(
X = parts,
X = colnames(data_clean()),
FUN = function(j, x, val) {
ok <- filter_var(x = x[[j]], val = input[[j]])
ok <- filter_var(x = x[[j]], val = val[[j]])
ok %||% TRUE
},
x = data_clean(),
Expand All @@ -154,12 +137,11 @@ module_prepare_server <- function(id, x) {
## Render filters
output$filter <- renderUI({
req(data_clean())
index <- arkhe::detect(x = data_clean(), f = is.numeric,
margin = 2, negate = TRUE)
quali <- data_clean()[, index, drop = FALSE]
quali <- arkhe::discard(x = data_clean(), f = is.numeric,
margin = 2, verbose = get_option("verbose"))
n <- ncol(quali)

if (n == 0) return(NULL)

parts <- colnames(quali)
ui <- vector(mode = "list", length = n)
for (j in seq_len(n)) {
Expand All @@ -169,10 +151,6 @@ module_prepare_server <- function(id, x) {

ui
})
# FIXME: trouver une meilleure approche que forcer l'execution.
# La difficulté vient de l'usage de renderUI() qui n'est pas évaluée
# tant que l'utilisateur n'affiche pas cette portion d'interface.
outputOptions(output, name = "filter", suspendWhenHidden = FALSE)

## Render table
output$table <- DT::renderDataTable({ data_filter() })
Expand Down

0 comments on commit f114e2c

Please sign in to comment.