Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
subs4cs opened this issue Jul 31, 2020 · 0 comments

Comments

@subs4cs
Copy link

subs4cs commented Jul 31, 2020

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant