0

I'm using the DT package and I'd like to show two tables on separate panes with one dataset.

Ideally, I'd like something like facet wrap that would let me make tables based on the plan id. I'd like to have one table that has all the values for the personal plan and another table that has all the values for the team plan. I can go the long way around and make two separate datasets, but I'm hoping there's something that I can do that might be more efficient

This is my data

structure(list(first_month = structure(c(17532, 17532, 17563, 
17563, 17591, 17591, 17622, 17622, 17652, 17652), class = "Date"), 
    plan_id = c("personal", "team", "personal", "team", "personal", 
    "team", "personal", "team", "personal", "team"), new_customers = c(16, 
    32, 27, 33, 19, 41, 36, 46, 48, 46), `1` = c(16, 32, 27, 
    33, 19, 41, 36, 46, 48, 46), `2` = c(13, 29, 24, 30, 15, 
    37, 31, 40, 43, 38), `3` = c(13, 26, 22, 28, 14, 31, 30, 
    40, 36, 35), `4` = c(10, 20, 22, 22, 12, 29, 27, 39, 32, 
    33), `5` = c(10, 18, 20, 20, 11, 25, 22, 36, 27, 27), `6` = c(10, 
    16, 16, 20, 9, 24, 19, 34, 24, 25), `7` = c(10, 12, 13, 18, 
    7, 24, 16, 32, 21, 23), `8` = c(8, 10, 10, 14, 7, 21, 16, 
    30, 19, 21), `9` = c(7, 8, 7, 12, 7, 18, 16, 25, NA, NA), 
    `10` = c(7, 7, 5, 11, 6, 14, NA, NA, NA, NA), `11` = c(5, 
    6, 5, 10, NA, NA, NA, NA, NA, NA), `12` = c(5, 6, NA, NA, 
    NA, NA, NA, NA, NA, NA)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

This is my code

  datatable(monthly_new_customer_cohorts_formatted_as_cohort_analysis_customer_counts,
            class = 'cell-border stripe',
            rownames = FALSE,
            options = list(
              ordering=F,
              dom = 't',
              pageLength = 1000))
Cauder
  • 2,157
  • 4
  • 30
  • 69

1 Answers1

1

Might not be the slickest answer, but hopefully this helps. You can use group_by, nest, mutate and map2 to create separate data-sets based on plan_id, then create a separate DT widget from each.

library(dplyr)
library(tidyr)
library(purrr)

# Separate data for each plan_id value:
widgets <- monthly_new_customer_cohorts_formatted_as_cohort_analysis_customer_counts %>% 
  group_by(plan_id) %>% nest()

# For each data subset, create a separate DT widget:
widgets <- widgets %>% mutate(dt_widget = map2(.y = plan_id, .x = data, .f = function(x,y){
  datatable(x,class = 'cell-border stripe',caption = y,
            rownames = FALSE,options = list(
              ordering=F,
              dom = 't',
              pageLength = 1000),
            height = "100%",width = "100%")}))

Each element of widgets$dt_widget is now a separate datatable widget. Trick now is to have them all in the same viewer:

library(htmltools)
browsable(x = 
  tagList(lapply(
    widgets$dt_widget,function(x) tags$div(x)))
)

Unfortunately I don't think there is a function to give you the nice grid ala ggplot2::facet_wrap (e.g. a 3x3 grid for 9 facets) but you can manually do this by adjusting the style values in the div elements. For example, this allows the tables to go on the same row by reducing the width and adjusting the float:

browsable(
  tagList(list(
    tags$div(
      widgets$dt_widget[[1]],
      style = 'width:49%;display:block;float:left;'
    ),
    tags$div(
      widgets$dt_widget[[2]],
      style = 'width:49%;display:block;float:right;')
    )
  ))
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dan Kennedy
  • 380
  • 1
  • 9