Skip to content

Commit

Permalink
optim(data-viz): avoid rendering plot 2 times when var type is changed
Browse files Browse the repository at this point in the history
Previoulsy, `data_from_file` depended on both `input$categ_variables`
and `input$quant_variables`. This cause the plot to be calculated 2
times because when the user change a variable "type", eg. `Var_1` from
"categorical" to "quantitative" the following happens:
 - `data_from_file` is recalculated because `input$categ_variables`
 had changed
 - an observer update the value of `input$quant_variables`
 - `data_from_file` is again recalculated because `input$quant_variables`
 had changed

Now I have removed this dependency since all variables that are not
categorical are quantitative.

Note: the `input$quant_variables` input is not actually used anymore
and is here only as information for users.
  • Loading branch information
juliendiot42 committed Sep 24, 2024
1 parent cf9c10f commit 6048e3e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/server/server_data_viz.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ observeEvent(input$categ_variables, {
data <- req(raw_data_from_file())
categ_var <- input$categ_variables
quant_var <- colnames(data)[!colnames(data) %in% categ_var]
if (!setequal(quant_var, input$quant_variables)) {
updateSelectInput(session, "quant_variables",
selected = quant_var
)
}

updateSelectInput(session, "quant_variables",
selected = quant_var
)
})


data_from_file <- reactive({

data <- raw_data_from_file()
for (var in input$quant_variables) {
categ_vars <- input$categ_variables
quant_vars <- colnames(data)[!colnames(data) %in% categ_vars]

for (var in quant_vars) {
data[, var] <- as.numeric(data[, var])
}
for (var in input$categ_variables) {
for (var in categ_vars) {
data[, var] <- as.character(data[, var])
}
data
Expand Down

0 comments on commit 6048e3e

Please sign in to comment.