Skip to contents

Introduction

The registry is a environment which provides access to multiple blocks as well as some metadata:

  • The block description.
  • The block category.
  • The block package.

In other words, the registry is a “supermarket” for data analysis. As shown below, if you develop your own blocks package and registers blocks on load, these blocks become available to the end user. Therefore this makes it powerful for collaboration between data science teams.

flowchart LR
  subgraph blockr_ggplot2[blockr.ggplot2]
    new_block1[New block]
    new_block2[New block]
  end
  subgraph blockr_echarts4r[blockr.echarts4r]
    new_block3[New block]
    new_block4[New block]
  end
  blockr_ggplot2 --> |register| registry
  blockr_echarts4r --> |register| registry
  subgraph registry[Registry]
    subgraph select_reg[Select block]
      reg_name[Name: select block]
      reg_descr[Description: select columns in a table]
      reg_classes[Classes: select_block, tranform_block]
      reg_category[Category: transform]
      reg_ctor[Construcor: new_select_block]
      reg_package[Package: blockr.dplyr]
    end
    subgraph filter_reg[Filter block]
    end
    filter_reg --x |unregister| trash['fa:fa-trash']
  end

Previewing available blocks

Upon loading, {blockr} registers its internal blocks with register_blockr_blocks(). You won’t have to call this function as it is not exported anyway. This makes the registry environment ready to be queried by available_blocks(). A truncated output example below:

available_blocks()[["dataset_block"]]
function(dataset = character(), package = "datasets", ...) {
  ...
}
<environment: namespace:blockr.core>
attr(,"name")
[1] "dataset block"
attr(,"description")
[1] "Choose a dataset from a package"
attr(,"classes")
[1] "dataset_block" "data_block"    "block"         "vctrs_vctr"    "list"         
attr(,"category")
[1] "data"
attr(,"ctor_name")
[1] "new_dataset_block"
attr(,"package")
[1] "blockr.core"
attr(,"class")
[1] "registry_entry"
names(available_blocks())
#> [1] "csv_block"     "dataset_block" "head_block"    "merge_block"  
#> [5] "rbind_block"   "scatter_block" "static_block"  "subset_block" 
#> [9] "upload_block"

Register a block

To register your own blocks, user facing functions are:

  • register_block() to register a block in the registry. If the block is already registered, it overwrites the existing one.
  • register_blocks() to register multiple blocks.

Note that, if you develop your block outside a package context, you must call register_block (or register_blocks) passing the constructor as a function and not as a string (see below).

Let’s say you want to create a new new_dummy_block which does nothing specific in the ./R/dummy-block.R script:

# ./R/dummy-block.R
new_dummy_block <- function(text = "Hello World", ...) {
  new_data_block(
    function(id) {
      moduleServer(id, function(input, output, session) {
        list(
          expr = reactive(quote(text)),
          state = list(text = text)
        )
      })
    },
    function(id) {
      tagList()
    },
    class = "dummy_block",
    ...
  )
}

register_dummy_blocks <- function() {
  register_blocks(
    c(new_dummy_block),
    name = c("dummy block"),
    description = c("A block that does nothing"),
    overwrite = TRUE
  )
}

register_dummy_blocks()

Finally, we create a .R/zzz.R script where you run this code to register the block(s) whenever the package loads:

# ./R/zzz.R
.onLoad <- function(libname, pkgname) {
  register_dummy_blocks()
  invisible(NULL)
}

If we now query the registry, the new block is available:

available_blocks()[["dummy_block"]]
#> function (text = "Hello World", ...) 
#> {
#>     new_data_block(function(id) {
#>         moduleServer(id, function(input, output, session) {
#>             list(expr = reactive(quote(text)), state = list(text = text))
#>         })
#>     }, function(id) {
#>         tagList()
#>     }, class = "dummy_block", ...)
#> }
#> attr(,"name")
#> [1] "dummy block"
#> attr(,"description")
#> [1] "A block that does nothing"
#> attr(,"classes")
#> [1] "dummy_block" "data_block"  "block"       "vctrs_vctr"  "list"       
#> attr(,"category")
#> [1] "uncategorized"
#> attr(,"class")
#> [1] "registry_entry"

Unregister a block

The counterpart of register_block() is unregister_blocks(). We can remove our new dummy_block from the registry:

unregister_blocks(uid = "dummy_block")

# Check it out
names(available_blocks())
#> [1] "csv_block"     "dataset_block" "head_block"    "merge_block"  
#> [5] "rbind_block"   "scatter_block" "static_block"  "subset_block" 
#> [9] "upload_block"

where uid is/are the block(s) class(s) passed in the constructor.

Create your own registry UI

Within blockr.core, the registry is used to create a dropdown of possible blocks when the user wants to add a block:

selectInput(
  ns("registry_select"),
  "Select block from registry",
  choices = list_blocks()
)

In the blockr.ui package, we designed a more sofisticated UI for the registry. We leverage our scoutbaR package that provides a React-powered contextual menu with search and nicer metadata display. Block categories are first extracted to create the widget sections with scout_section() and passing the right label. Within each section (ie block category) we create one scout_action() per block. Each scout_action() display the block icon for better user experience, the block name and its description.

# pak::pak("scoutbaR")
library(scoutbaR)
blk_icon <- function(category) {
  switch(
    category,
    "data" = "table",
    "file" = "file-import",
    "parse" = "cogs",
    "plot" = "chart-line",
    "transform" = "wand-magic-sparkles",
    "table" = "table"
  )
}

blk_choices <- function() {
  blk_cats <- sort(
    unique(chr_ply(available_blocks(), \(b) attr(b, "category")))
  )

  lapply(blk_cats, \(cat) {
    scout_section(
      label = cat,
      .list = dropNulls(
        unname(
          lapply(available_blocks(), \(choice) {
            if (attr(choice, "category") == cat) {
              scout_action(
                id = attr(choice, "classes")[1],
                label = attr(choice, "name"),
                description = attr(choice, "description"),
                icon = blk_icon(cat)
              )
            }
          })
        )
      )
    )
  })
}

A demo is available below, where you can click on New block or press cmd/ctrl + K to trigger the contextual menu.

Note

The demo below runs with shinylive. Not all feature may work as expected due to compatibility issues with webR.