Show and Tell - Dash Pivottable

Hi @Titus1 – you’re correct that the R example loads the tips data as a list-of-lists rather than a data.frame.

If you scroll down in the online help for dashPivotTable, you should see a second sample app which uses the Loblolly pine seedling dataset (see here for more info). In that example, we load dashTable first, and then we can pass data = df_to_list(Loblolly).

The df_to_list function from dashTable handles the conversion for us (this is somewhat analogous to df_to_dict in Python). You can definitely create a dataset in the same format as tips, but it’s probably easier to use this helper function. :smile_cat:

I hope this helps – if you run into trouble or have additional questions, please don’t hesitate to ask.

1 Like

Thanks @rpkyle I didn’t know about Dash for R.

I’m now trying to figure out how to install dashPivotTable in R - I see the two scripts dashPivotTable.R and internal.R - do i just run these in R?

Please excuse my ignorance

Thanks for the help

Edit:

I managed to format my data in python like this:

df = pd.read_csv('mydata.csv')

data = [df.columns.tolist()] + df.values.tolist()

@Titus1 No worries! Here’s the original announcement:

The current version released to CRAN is v0.5.0, but we’ll soon submit v0.8.0 to CRAN with support for pattern matching callbacks and improvements to the callback graph after it’s released to GitHub (within a few weeks).

As for using dashPivotTable in R, there’s a nice example here – and both examples are available within R using help(dashPivotTable):

If case you want to import your own data.frame into dashPivotTable, you can use the df_to_list helper function from dashTable, e.g.

  library(dash)
  library(dashHtmlComponents)
  library(dashPivottable)
  library(dashTable)

  app <- Dash$new()
  app$title("Summary statistics for iris data")

  app$layout(
    htmlDiv(
      list(
        dashPivotTable(
          id = "table",
          data = df_to_list(Loblolly),
          cols = list("Seed"),
          colOrder = "key_a_to_z",
          rows = list("age"),
          rowOrder = "key_a_to_z",
          rendererName = "Grouped Column Chart",
          aggregatorName = "Average",
          vals = list("height")
        ),
        htmlDiv(
          id = "output"
        )
      )
    )
  )

  app$callback(output = output(id="output", property="children"),
              params = list(input(id="table", property="cols"),
                            input(id="table", property="rows"),
                            input(id="table", property="rowOrder"),
                            input(id="table", property="colOrder"),
                            input(id="table", property="aggregatorName"),
                            input(id="table", property="rendererName")),
              function(cols, rows, row_order, col_order, aggregator, renderer) {
                return(
                  list(
                    htmlP(cols, id="columns"),
                    htmlP(rows, id="rows"),
                    htmlP(row_order, id="row_order"),
                    htmlP(col_order, id="col_order"),
                    htmlP(aggregator, id="aggregator"),
                    htmlP(renderer, id="renderer")
                  )
                )
              }
  )

  app$run_server(debug=TRUE)

It is really use full, but I do not want to show grand total for both Columns & Row…
Thanks for help …

This is amazing! One thing I was never able to accomplish is to use text-wrapping when the column name is too long (and I do not want to change it). Is there any option to do such a thing? In any case, thank you for this!