Skip to content

Commit

Permalink
Fixing sort for descending
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements authored Nov 14, 2024
1 parent 1131867 commit 144e1f2
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions instat/static/InstatObject/R/data_object_R6.R
Original file line number Diff line number Diff line change
Expand Up @@ -1567,19 +1567,10 @@ DataSheet$set("public", "sort_dataframe", function(col_names = c(), decreasing =
message("No sorting to be done.")
}
} else {
# Build the expressions using rlang for sorting columns
col_names_exp <- purrr::map(col_names, function(col_name) {
if (!(col_name %in% names(curr_data))) {
stop(col_name, " is not a column in the data.")
}
if (decreasing) dplyr::desc(rlang::sym(col_name)) else rlang::sym(col_name)
})

# Handle the case where sorting by row names and column names at the same time
if (by_row_names) warning("Cannot sort by columns and row names. Sorting will be done by given columns only.")

# Sort the data based on the expressions
self$set_data(dplyr::arrange(curr_data, !!!col_names_exp))
if (decreasing) self$set_data(dplyr::arrange(curr_data, dplyr::across(dplyr::all_of(col_names), desc)))
else self$set_data(dplyr::arrange(curr_data, dplyr::across(dplyr::all_of(col_names))))
}
self$data_changed <- TRUE
}
Expand Down

0 comments on commit 144e1f2

Please sign in to comment.