Skip to content

Question: Is there a way to sort table by values displayed in current column selection? #113

@subs4cs

Description

@subs4cs

I asked this question on SO but haven't received any response so thought about extending it to this community.

I'm trying to see if there is a way to sort pivot table by column values.

In the sample program and output snip below, is it possible to have table sorted by 'cyl', 'disp', 'hp' or 'mpg' column?

library(shiny)
library(dplyr)
library(rpivotTable)

ui <- fluidPage(
  rpivotTableOutput(outputId = "rpv")
)

server <- function(input, output) {
  
sorter <- paste0("
    function(attr) {
      var sortAs = $.pivotUtilities.sortAs;
      if (attr == 'x') { return sortAs(['mpg','disp','cyl','hp']); }
    }")
  
  observe({
    output$rpv <- renderRpivotTable({rpivotTable(mtcars %>% gather(x, y, 1:4), rows = c('vs','gear'), cols = c('x'), vals = 'y', sorters = sorter, aggregatorName = "Sum", subtotals = TRUE)})
  })
}

shinyApp(ui = ui, server = server)

rPivotTable Code Output

As an example output, sorting by 'mpg' column would display table such that column values of 'mpg' would be (assuming asc): 299.10, 42.00, 76.50, 180.60, 343.80, 30.40, 61.00, 252.40, 642.90

I think there it could be either

Extension of using 'sorters' parameters e.g. above code shows sorting for column headers
or

Table sort by clicking on header using javascript - not sure if this is even feasible.

Thank you in advance,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions