BristolMyersSquibb / blockr

Composable, extensible no-code UI
https://bristolmyerssquibb.github.io/blockr/
GNU General Public License v3.0
34 stars 3 forks source link

Result block - generate code #399

Open JohnCoene opened 2 months ago

JohnCoene commented 2 months ago

Given the coming project makes heavy use of generated code (exporting the entire workspace to a document), and result block (all blocks but one will depend on the result of a data block): how do we generate code for this?

DivadNojnarg commented 3 weeks ago

@JohnCoene and @nbenn

I worked on something. In short, we extract the stack name from the result block field value, then extract the corresponding stack from the "reactive" workspace and generate the code from the linked stack:

generate_code.result_block <- function(x) {
  if (!is_initialized(x)) {
    return(quote(data.frame()))
  }
 # TBD: value() does not return the expected result. Can't use it
  val <- x$stack$value

  # When we initialize the workspace reactive_stack_directory does not exist.
  # 
  linked_stack <- if (is.null(attr(get_workspace(), "reactive_stack_directory"))) {
    get_workspace_stack(val)
  } else {
    attr(get_workspace(), "reactive_stack_directory")()[[val]]$stack
  }
  do.call(
    bquote,
    list(
      attr(x, "expr"),
      where = list(stack = generate_code(linked_stack)),
      splice = FALSE
    )
  )
}