Skip to contents

Introduction

Data blocks are the starting point of any stack and are required by any subsequent transform or plot blocks.

Package based data block

To add a data set, use the function new_dataset_block(), passing a package name to the package arg that contains any number of datasets:

new_dataset_block <- function(
  selected = character(),
  package = "datasets",
  ...,
  
) {
  # code  
}

The new_dataset_block() function defaults to using the base R datasets package, but you can supply any valid package containing one or multiple data sets. Here, we use the {blockr.data} package to select the "lab" data set:

library(blockr)
custom_data_block <- function(...) {
  new_dataset_block(
    selected = "lab",
    package = "blockr.data",
    ...
  )
}

stack <- new_stack(
  custom_data_block,
  new_select_block
)
serve_stack(stack)
In the registry vignette, we see how to register a block in a cleaner way.

Reading data from files

In addition to loading data from a package, blockr also contains a block to upload data from a file. blockr exposes two extra data blocks which have to be combined with one of the four following data parser blocks:

  • new_xpt_block, reads xpt files (and sas7bdat files).
  • new_rds_block, reads rds files.
  • new_json_block, reads json files.
  • new_csv_block, reads csv files.

Upload data

If you want to load data from any location on your computer, new_upload_block() is what you need. Since the new_upload_block() temporarily moves data into a custom location, for security reasons, it might not be always possible.

library(blockr)

stack <- new_stack(
  new_upload_block,
  new_csv_block,
  new_select_block
)
serve_stack(stack)

Files browser block

new_filesbrowser_block() can read data from the server file system. This means that if no data is available on the server, you won’t see anything. The accessible locations can be customized through the volumes parameter like so:

custom_filesbrowser_block <- function(...) {
  new_filesbrowser_block(
    file_path = character(),
    volumes = c(vol1 = "<PATH1>", vol2 = "<PATH2>"),
    ...
  )
}

In the following demonstration, dummy dataset has been uploaded to the webR file system so you can play with the block. But, you can also upload your own data thanks to the corresponding shinylive feature (upload button).

## file: app.R
library(blockr)

stack <- new_stack(
  new_filesbrowser_block,
  new_csv_block,
  new_select_block
)
serve_stack(stack)

## file: penguins.csv

Note that in a later release, we plan to merge filesbrowser_block() and upload_block() into a single variable block.

Reading data from another stack

new_result_block() allows you to read data from another stack. From the below code, the first stack contains a new_dataset_block() from which we can select some columns with new_select_block(). In the second stack, we can reuse this smaller dataset. If you dynamically add a third stack, you can also select the third stack’s output as input to the second stack (it appears in the result block select input).

library(blockr)
library(blockr.data)

serve_workspace(
  stack1 = new_stack(
    new_dataset_block("lab", "blockr.data"),
    new_select_block(c("STUDYID", "USUBJID"))
  ),
  stack2 = new_stack(new_result_block),
  stack3 = new_stack(new_dataset_block("ae", "blockr.data")),
  title = "My workspace"
)