Skip to content

Conversation

@syroBx
Copy link
Collaborator

@syroBx syroBx commented Feb 19, 2025

Created combine_tables function to create single cohort table from 2 or more tables for #44 .
And added sakila database to test this function.

Example use of the function:
After load database, create binding_keys

sakila_source <- set_source(as.tblist(sakila))

sakila_source$primary_keys <- primary_keys(
  data_key('actor', 'actor_id'),
  data_key('film', 'film_id'),
  data_key('category', 'category_id'),
  data_key('language', 'language_id')
)

sakila_source$binding_keys <- bind_keys(
  bind_key(update = data_key('actor', 'actor_id'),
           data_key('film_actor', 'actor_id')
  ),
  bind_key(update = data_key('film_actor','film_id'),
           data_key('film','film_id')
  ),
  bind_key(update = data_key('film_category','film_id'),
           data_key('film','film_id')
  ),
  bind_key(update = data_key('category','category_id'),
           data_key('film_category','category_id')
  ),
  bind_key(update = data_key('language','language_id'),
           data_key('film','language_id')
  ),
  bind_key(update = data_key('inventory','film_id'),
           data_key('film','film_id')
  ),
  bind_key(update = data_key('film_actor','actor_id'),
           data_key('actor', 'actor_id')
  ),
  bind_key(update = data_key('film','film_id'),
           data_key('film_actor','film_id')
  )
)

Next create cohort and run function:

coh <- Cohort$new(
  sakila_source,
  step(
    filter(
      type = "range", id = "actor_id", name = "actor.id",
      variable = "actor_id", dataset = "actor", range = c(5, 6)
    )
  ),
  run_flow = TRUE
)

result <- combine_tables(coh,c("actor","category"))

Result:

# A tibble: 2 × 5
  actor_id first_name last_name    last_update         actor_category   
     <dbl> <chr>      <chr>        <chr>               <list>           
1        5 JOHNNY     LOLLOBRIGIDA 2006-02-15 04:34:33 <tibble [13 × 3]>
2        6 BETTE      NICHOLSON    2006-02-15 04:34:33 <tibble [11 × 3]>
result <- combine_tables(coh, c("category","actor","film"))

Result:

# A tibble: 16 × 5
   category_id name        last_update         category_film     category_actor  
         <dbl> <chr>       <chr>               <list>            <list>          
 1           1 Action      2006-02-15 04:46:27 <tibble [4 × 13]> <tibble [2 × 4]>
 2           2 Animation   2006-02-15 04:46:27 <tibble [5 × 13]> <tibble [2 × 4]>
 3           3 Children    2006-02-15 04:46:27 <tibble [2 × 13]> <tibble [2 × 4]>
 4           4 Classics    2006-02-15 04:46:27 <tibble [1 × 13]> <tibble [1 × 4]>
 5           5 Comedy      2006-02-15 04:46:27 <tibble [1 × 13]> <tibble [1 × 4]>
 6           6 Documentary 2006-02-15 04:46:27 <tibble [5 × 13]> <tibble [2 × 4]>
 7           7 Drama       2006-02-15 04:46:27 <tibble [2 × 13]> <tibble [1 × 4]>
 8           8 Family      2006-02-15 04:46:27 <tibble [1 × 13]> <tibble [1 × 4]>
 9           9 Foreign     2006-02-15 04:46:27 <tibble [2 × 13]> <tibble [1 × 4]>
10          10 Games       2006-02-15 04:46:27 <tibble [1 × 13]> <tibble [1 × 4]>
11          11 Horror      2006-02-15 04:46:27 <tibble [3 × 13]> <tibble [1 × 4]>
12          12 Music       2006-02-15 04:46:27 <tibble [2 × 13]> <tibble [1 × 4]>
13          13 New         2006-02-15 04:46:27 <tibble [3 × 13]> <tibble [2 × 4]>
14          14 Sci-Fi      2006-02-15 04:46:27 <tibble [4 × 13]> <tibble [2 × 4]>
15          15 Sports      2006-02-15 04:46:27 <tibble [7 × 13]> <tibble [2 × 4]>
16          16 Travel      2006-02-15 04:46:27 <tibble [5 × 13]> <tibble [2 × 4]>

@syroBx syroBx self-assigned this Feb 19, 2025
syroBx

This comment was marked as duplicate.

#' Create plot to draw connection between binding keys
#'
#' @param x Cohort
plot_binding_keys <- function(x) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function to draw connection between binding_keys.
To work only need Cohort with defined binding_keys.

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

Successfully merging this pull request may close these issues.

1 participant