10 Higher order widgets

You may have noticed when we ran the show_widgets() function in working with widgets that there is a mysterious “Order” column that categorizes widgets as either “first-order” or “second-order”. What’s that all about? In order to answer that, let’s take a look at another example.

10.1 Children of wealth

Many alumni in our database do not have capacity ratings, but come from wealthy families. This would be nice to be aware of if they were ever being solicited. But how could we identify these “children of wealth” using the discovery engine? It’s pretty easy to find wealthy individuals:

# as always, i make sure the disco engine is loaded
library(discoveryengine)

# i'll define wealthy as a capacity of $1 Million+
wealthy = has_capacity(1:7)

But now I’m stuck. I want to find children of anyone on that list. But so far all of the widgets we’ve seen work with specific codes, not other widgets. Technically, all the widgets we’ve seen are first-order widgets. But we do have some widgets that, instead of working on one or more specific codes, works on an existing, already filled out widget. child_of is one such second-order widget:

child_of_wealth = child_of(wealthy)
display(child_of_wealth)
## # A tibble: 1,299 x 1
##    entity_id
##        <dbl>
##  1       155
##  2      1347
##  3      1529
##  4      2497
##  5      3181
##  6      3296
##  7      3308
##  8      3418
##  9      3442
## 10      3897
## # … with 1,289 more rows

Though the syntax looks just like what we’ve been doing all along, note that wealthy in the above is not a code or synonym, but instead is the name that I gave to the definition has_capacity(1:7). This makes child_of very different from other widgets we’ve seen so far. As we’ll see in the next seciton, the ability to use second-order widgets allows us to do some very powerful things.

The result of using a second-order widget, though, is just like using any other widget. You can continue to combine it with other widgets:

display(child_of_wealth %and% lives_in_msa(san_francisco))
## # A tibble: 509 x 1
##    entity_id
##        <dbl>
##  1       155
##  2      1529
##  3      2497
##  4      3296
##  5      3418
##  6      3442
##  7      3897
##  8      4539
##  9      4540
## 10      5084
## # … with 499 more rows
# or even
display(
    child_of(child_of_wealth)
)
## # A tibble: 148 x 1
##    entity_id
##        <dbl>
##  1     22266
##  2     22307
##  3     23574
##  4     25856
##  5     26067
##  6     27804
##  7     28003
##  8     28405
##  9     28586
## 10     28891
## # … with 138 more rows